From c50c1b913017701566efa284c096686fc97f0023 Mon Sep 17 00:00:00 2001 From: bh Date: Sun, 15 Mar 2026 14:36:31 +0800 Subject: Add custom sudo write command --- init.lua | 1 + lazy-lock.json | 2 ++ lua/config/sudo.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 lua/config/sudo.lua diff --git a/init.lua b/init.lua index 689c875..e3fc185 100644 --- a/init.lua +++ b/init.lua @@ -12,6 +12,7 @@ -------------------------------------------------------- require("config.lazy") +require("config.sudo") -------------------------------------------------------- -- 🪶 Theme diff --git a/lazy-lock.json b/lazy-lock.json index 3774e1c..2d61723 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -18,6 +18,7 @@ "lualine.nvim": { "branch": "master", "commit": "47f91c416daef12db467145e16bed5bbfe00add8" }, "neoscroll.nvim": { "branch": "master", "commit": "f957373912e88579e26fdaea4735450ff2ef5c9c" }, "nightfox.nvim": { "branch": "main", "commit": "ba47d4b4c5ec308718641ba7402c143836f35aa9" }, + "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nvim-autopairs": { "branch": "master", "commit": "7a2c97cccd60abc559344042fefb1d5a85b3e33b" }, "nvim-cmp": { "branch": "main", "commit": "d97d85e01339f01b842e6ec1502f639b080cb0fc" }, "nvim-dap": { "branch": "master", "commit": "5860c7c501eb428d3137ee22c522828d20cca0b3" }, @@ -29,6 +30,7 @@ "nvim-web-devicons": { "branch": "master", "commit": "8dcb311b0c92d460fac00eac706abd43d94d68af" }, "onedark.nvim": { "branch": "master", "commit": "213c23ae45a04797572242568d5d51937181792d" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, + "sudo.nvim": { "branch": "main", "commit": "eed34f8636f0dd1bac89c81f6f96a86ca513f5f2" }, "telescope.nvim": { "branch": "master", "commit": "a0bbec21143c7bc5f8bb02e0005fa0b982edc026" }, "tokyonight.nvim": { "branch": "main", "commit": "5da1b76e64daf4c5d410f06bcb6b9cb640da7dfd" }, "undotree": { "branch": "master", "commit": "0f1c9816975b5d7f87d5003a19c53c6fd2ff6f7f" }, 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") -- cgit v1.2.3