From 30e73220a8e81c87f7660d03e444416be4e376e0 Mon Sep 17 00:00:00 2001 From: bh Date: Sun, 15 Mar 2026 14:46:37 +0800 Subject: Move sudo script to lua/scripts --- lua/config/sudo.lua | 45 --------------------------------------------- lua/scripts/sudo.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 lua/config/sudo.lua create mode 100644 lua/scripts/sudo.lua (limited to 'lua') diff --git a/lua/config/sudo.lua b/lua/config/sudo.lua deleted file mode 100644 index b9c9299..0000000 --- a/lua/config/sudo.lua +++ /dev/null @@ -1,45 +0,0 @@ --- ~/.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") diff --git a/lua/scripts/sudo.lua b/lua/scripts/sudo.lua new file mode 100644 index 0000000..b9c9299 --- /dev/null +++ b/lua/scripts/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") -- cgit v1.2.3