feat: add sudo write
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
vim.api.nvim_create_user_command("W", function()
|
||||||
|
require("utils").sudo_write()
|
||||||
|
end, { desc = "Write file with sudo (tee)" })
|
||||||
|
|
||||||
-- Turn off paste mode when leaving insert
|
-- Turn off paste mode when leaving insert
|
||||||
vim.api.nvim_create_autocmd("InsertLeave", {
|
vim.api.nvim_create_autocmd("InsertLeave", {
|
||||||
pattern = "*",
|
pattern = "*",
|
||||||
|
|||||||
@@ -28,4 +28,25 @@ function M.file_exists(name)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- 无写权限时用 sudo 保存当前文件(对应 :W)
|
||||||
|
function M.sudo_write()
|
||||||
|
local name = vim.api.nvim_buf_get_name(0)
|
||||||
|
if name == "" then
|
||||||
|
vim.notify("W: 当前 buffer 没有文件名", vim.log.levels.ERROR)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, err = pcall(function()
|
||||||
|
vim.cmd("write !sudo tee % > /dev/null")
|
||||||
|
vim.cmd("edit!")
|
||||||
|
end)
|
||||||
|
if not ok then
|
||||||
|
local msg = vim.split(tostring(err), "\n", { plain = true })[1]
|
||||||
|
vim.notify("W: 保存失败 — " .. msg, vim.log.levels.ERROR)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.notify("W: 已保存 " .. name, vim.log.levels.INFO)
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user