summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua')
-rw-r--r--lua/config/sudo.lua45
1 files changed, 45 insertions, 0 deletions
diff --git a/lua/config/sudo.lua b/lua/config/sudo.lua
new file mode 100644
index 0000000..b9c9299
--- /dev/null
+++ b/lua/config/sudo.lua
@@ -0,0 +1,45 @@
+-- ~/.config/nvim/lua/config/sudo.lua
+-- :Sudow - Write the current buffer with sudo privileges
+
+vim.api.nvim_create_user_command("Sudow", function()
+ local filepath = vim.fn.expand("%:p")
+ if filepath == "" then
+ vim.notify("No file to write", vim.log.levels.ERROR)
+ return
+ end
+
+ -- Prompt for password
+ local password = vim.fn.inputsecret("Mot de passe: ")
+ if password == "" then
+ vim.notify("Cancelled", vim.log.levels.WARN)
+ return
+ end
+
+ -- Write buffer contents to a temp file
+ local tmpfile = vim.fn.tempname()
+ vim.cmd("silent write! " .. vim.fn.fnameescape(tmpfile))
+
+ -- Use sudo to copy temp file to target
+ local cmd = string.format(
+ "echo %s | sudo -S cp %s %s 2>&1",
+ vim.fn.shellescape(password),
+ vim.fn.shellescape(tmpfile),
+ vim.fn.shellescape(filepath)
+ )
+ local output = vim.fn.system(cmd)
+ local success = vim.v.shell_error == 0
+
+ -- Clean up temp file
+ os.remove(tmpfile)
+
+ if success then
+ -- Reload buffer to clear modified flag
+ vim.cmd("edit!")
+ vim.notify("Written with sudo: " .. filepath, vim.log.levels.INFO)
+ else
+ vim.notify("sudo write failed: " .. output, vim.log.levels.ERROR)
+ end
+end, { desc = "Write current buffer with sudo" })
+
+-- Allow lowercase :sudow to expand to :Sudow
+vim.cmd("cnoreabbrev sudow Sudow")