Giter Site home page Giter Site logo

suda.vim's Introduction

๐Ÿฅช suda.vim

MIT License Doc

suda is a plugin to read or write files with sudo command.

This plugin was built while :w !sudo tee % > /dev/null trick does not work on neovim.

neovim/neovim#1716

This plugin is strongly inspired by sudo.vim but the interfaces was aggressively modified for modern Vim script.

Usage

Use SudaRead to open unreadable files like:

" Re-open a current file with sudo
:SudaRead

" Open /etc/sudoers with sudo
:SudaRead /etc/sudoers

Or SudaWrite to write unwritable files like:

" Forcedly save a current file with sudo
:SudaWrite

" Write contents to /etc/profile
:SudaWrite /etc/profile

You can change the prompt string with g:suda#prompt.

" 'Password' in french
let g:suda#prompt = 'Mot de passe: '

Smart edit

When let g:suda_smart_edit = 1 is written in your vimrc, suda automatically switch a buffer name when the target file is not readable or writable.

In short,

$ vim /etc/hosts

or

:e /etc/shadow

Will open suda:///etc/hosts or suda:///etc/shadow instead of /etc/hosts or /etc/shadow because that files are not writable or not readable.

Windows

Install mattn/sudo or gerardog/gsudo to enable this plugin in Windows. Make sure that the following shows 1.

: echo executable('sudo')

Use sudo without a password

When let g:suda#noninteractive = 1 is written in your vimrc, suda won't ask you for a password. Use at your own risk.

suda.vim's People

Contributors

aarondill avatar bdeshi avatar doronbehar avatar lambdalisue avatar lumbric avatar notomo avatar sarmong avatar sitiom avatar sstark avatar thinca avatar tweekmonster avatar zeertzjq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

suda.vim's Issues

suda#read throws uncaught exception when opening suda:///not-existing.txt

Hey,

I noticed that if I launch nvim suda:///not-existing.txt and /not-existing.txt doesn't exist, I get an exception:

Error detected while processing function suda#BufReadCmd[5]..suda#read:
line   28:
E605: Exception not caught:

Here's where this happens, for reference:

let result = suda#system(printf(
\ 'cat %s %s',
\ shellescape(fnamemodify(path, ':p')),
\ redirect,
\))
if v:shell_error
throw result
else

cat fails because the file doesn't exist. Technically this error is harmless because if the file doesn't exist the buffer should be empty anyway. Perhaps this 'bug' shouldn't even be fixed?

We could ignore any errors but that won't be reliable if e.g the password was incorrect. As I'm thinking about it, this part of suda should be implemented with extreme care because if no error is thrown, a user might think he opened an empty file, add something according to some manual, save it (without errors) and end up with a totally different file with all the previous content lost.

According to my tests, even a wrong password ends up with an empty buffer and the same exception error is printed. Maybe suda should clearly state to the user what command failed and that the buffer's contents don't correspond to the real contents? Unless of course the file doesn't exist.. Anyway, I wonder what is your opinion on the subject.

Thanks.

Empty "suda:" directory created in $HOME on write

When saving a suda:// file, the file is saved correctly but also creates an empty directory tree in $HOME.

For example, saving suda:///home/<user>/.config/file creates suda:/home/<user>

This is with all defaults, g:suda_smart_edit = 1, and on the latest commit 8b0fc3711760195aba104e2d190cff9af8267052

Thank you!

Error executing system command when using nushell

Hi

I'm facing to an issue when I want to edit a file that needs suda to write and when I'm using Nu shell.

suda prompts for the password when I try to write my file, and when I enter my password, I have the following message :

[suda] Error: ^[[31mnu::parser::parse_mismatch^[[0m\n\n  ^[[31mร—^[[0m Parse mismatch during operation.\n   โ•ญโ”€[^[[36;1;4msource^[[0m:1:8]\n ^[[2m1^[[0m โ”‚ 'sudo' '-p' '' '-S' '--' 'dd' 'if=/tmp/nvim.gly/L1F4En/2' 'of=/etc/default/grub' 'bs=1048576'\n   ยท ^[[35;1m       โ”€โ”€โ”ฌโ”€^[[0m\n   ยท          ^[[35;1mโ•ฐโ”€โ”€ ^[[35;1mexpected operator^[[0m^[[0m\n   โ•ฐโ”€โ”€โ”€โ”€\n

Once formatted :

Error: nu::parser::parse_mismatch

  ร— Parse mismatch during operation.
   โ•ญโ”€[source:1:8]
 1 โ”‚ 'sudo' '-p' '' '-S' '--' 'dd' 'if=/tmp/nvim.gly/L1F4En/2' 'of=/etc/default/grub' 'bs=1048576'
   ยท        โ”€โ”€โ”ฌโ”€
   ยท          โ•ฐโ”€โ”€ expected operator
   โ•ฐโ”€โ”€โ”€โ”€

So it's happening because nushell does not support the executable written with simple quotes string like 'this' (nor double quotes).
image

but Nushell does support executable with backticks, like `this`
image
(I did not execute the command in the picture, take my word for it ๐Ÿ˜… )

Incorrect modified file indicator

I'm using VimR.

When I save current file using :w suda://% the modified file indicator stays the same, i.e. it shows that the file has been modified. It also prompts me to save the file when I try to quit VimR, while it should not.

Smart edit prevents nvim -d from working

Hello !

When let g:suda_smart_edit = 1 is set in my init.vim file, I can't use nvim -d to display a diff between 2 read-only files (or 1 read-only file, and 1 writable file).

Without the option set, everything is fine.

Thanx for the help,
Best regards,
Dupond

On-demand loading

Hello,

Is there a way to load the plugin on-demand? It's an option that is provided by a few plugin managers, e.g. vim-plug, allowing you to load a plugin that way:

Plug 'junegunn/vim-github-dashboard', { 'on': ['GHDashboard', 'GHActivity'] }

The on parameter specifies which function of the plugin triggers the loading.

However, I can't seem to succeed in applying this method to suda, which is a relatively slow plugin to load (compared to the other plugins that I use, I mean; suda takes about 30ms to load according to vim --startuptime).

So the idea would be to only load suda when needed (that is, when a file is saved using this plugin)... Any idea?

Any help would be greatly appreciated!

Best regards,
Dupond

suda conflict with vim-go/coc.nvim when "navigate in golang lib src"

when navigating in golang lib src (such as suda:///usr/lib/go/src/time/time.go), lsp-server return the location, then vim emit a error:

neovim 0.4.3

vim-go: initialized gopls
vim-go: [definition] SUCCESS                                                                                                                                       
vim-go: [definition] SUCCESS
Error detected while processing function <SNR>192_on_stdout[1]..<SNR>192_neocb[58]..325[13]..327[26]..<SNR>190_definitionHandler:
line    2:
E909: Cannot index a special variable
Error detected while processing function <SNR>192_on_stdout[1]..<SNR>192_neocb[58]..325[13]..327[26]..<SNR>190_definitionHandler:
line    2:
E15: Invalid expression: a:msg[0]
Error detected while processing function <SNR>192_on_stdout[1]..<SNR>192_neocb[58]..325:
line   13:
E171: Missing :endif
Error detected while processing function <SNR>192_on_stdout[1]..<SNR>192_neocb:
line   58:
E170: Missing :endwhile

vim has similar output.

my suda config:

let g:suda_smart_edit = 1

disable suda or let g:suda_smart_edit = 0 can get rid of it.

a similar issue from vim-go may have a relationship:
fatih/vim-go#2504

should I report a issue to vim-go also?
thank you.

Error creating a file in a read-only folder with smart edit

Hi,

If let g:suda_smart_edit = 1 is set, then trying to create a file in a read-only folder (e.g. nvim /etc/my_new_file) will first ask for the sudo password, and then display the following error message (translated from french):

Error detected in function suda#BufReadCmd[5]..suda#read :
line 28 :
E605: Exception not caught :
Press ENTER or type a command to continue

Thanx for any help,
Best regards,
Dupond

lost suda#prefix option

hi, could you kindly share the reason behind removing g:suda#prefix? cf4ff1b
it was an useful feature for me and was surprised to find it gone.

vimdiff does not work with suda.vim

nvim -u minimal_init.vim -d <ใƒ•ใ‚กใ‚คใƒซ1> <ใƒ•ใ‚กใ‚คใƒซ2> ใฎใ‚ˆใ†ใซ้–‹ใ„ใŸใจใใซใ€ๆœ€ๅˆใฎใƒ•ใ‚กใ‚คใƒซใŒ้–‹ใ‹ใ‚Œใฆใ—ใพใ„ใ€ๆจชไธฆใณใง่กจ็คบใŒใงใใชใ„ใฎใงใ™ใŒใ€่กจ็คบใ™ใ‚‹ๆ–นๆณ•ใฏใ‚ใ‚Šใพใ™ใงใ—ใ‚‡ใ†ใ‹ใ€‚

neovim ใƒใƒผใ‚ธใƒงใƒณ

$ nvim --version
NVIM v0.5.0-nightly-87-g2bbbb34ce
Build type: Release
LuaJIT 2.1.0-beta3

minimal_init.vim

let g:suda_smart_edit = 1

set runtimepath=$VIMRUNTIME

set packpath=./

ใงใ€ pack/minimal/start/ ้…ไธ‹ใซ clone ใ—ใฆใ„ใพใ™ใ€‚

ใ‚ˆใ‚ใ—ใใŠ้ก˜ใ„ใ„ใŸใ—ใพใ™ใ€‚

Duplicate tag "suda" in doc

Hi, just updated to the newest version and I get this error using the Lazy package manager in neovim.
image

I tried to look throught the latest changes but I couldn't figure out what causes this error.

How do I use this?

The readme says that to eg write with sudo, I should run

:w suda://%

but when I do this with a file in /usr/local/include on macOS, all I get is

"suda:///usr/local/include/zmq.hpp"
E212: Can't open file for writing: no such file or directory
Press ENTER or type command to continue

Also, for opening a file with sudo via

:e suda://%

all I see is an empty file.

Make suda:// buffer more secure

When opening files that only root can read using suda, nvim will still create a world-readable backup file. For files containing sensitive information, this is highly undesirable.

support :saveas

In a readonly directory, with let g:suda_smart_edit = 1

use :edit newfile , then :w work ok
but use :saveas newfile then :w get permission denied

Could you please support this?

make it work with persistent undo

i have set undofile undodir=~/.local/share/nvim/undo set in my nvim config but still persistent undo does not work with files in smart edit or saved via :w sudoa://%. It would be great if that would work though, since that is the strongest reason why I don't use sudoedit.

What effect does `g:suda_startup` has?

I would like to contribute to this plugin a new setting that will enable automatically writing with suda when no permissions to the current user are available. I was reading the code which is written very well and clear, yet I didn't understand the effect g:suda_startup has. I've tested it and whether it set or not, nvim suda:///etc/bla asks for a password.

Thanks.

Cannot activate suda_smart_edit

OS: Linux

Lazy install:
require("lazy").setup({"lambdalisue/suda.vim"})

Parameter set:
vim.g.suda_smart_edit = 1

If I copy the code from the source it works:

vim.api.nvim_exec([[
    augroup suda_smart_edit
    autocmd!
    autocmd BufEnter * nested call suda#BufEnter()
  augroup end]],false)

SudaWrite failure based on cwd?

Was struggling to debug this issue for a while, not sure if this is expected behaviour?

SudaWrite /etc/environment from / ->

image

SudoWrite /etc/environment from /etc -> success

Error writing input to shell-command: EPIPE

:W                                                Error detected while processing function suda#BufWriteCmd[3]..suda#write[18]..suda#system:
line    5:
E5677: Error writing input to shell-command: EPIPE
Press ENTER or type command to continue

Steps to reproduce:
my /etc/hosts . Delete the very last line.

Prompted for password even if non is required

I've sudo set up so that I can sudo by just pressing my Yubikey's button.

However, when I run SudaWrite, I'm always prompted for a password. I can type any gibberish though, and then pressing the key lets sudo operate properly.

How about not asking for a password is sudo does not require one?

How do I install this?

There doesn't seem to be any info on how to actually install this plugin... How do I install it?

suda cannot handle files that include '#' in the name

suda seems to be unable to handle files with '#' in the name.

I am able to reproduce this with a very minimal config, e.g.

call plug#begin('~/.local/share/nvim/plugged')
Plug 'lambdalisue/suda.vim'
call plug#end()
$ touch '#test'
# chown root:root '#test'
$ nvim '#test'

When I open the file:

Error detected while processing BufEnter Autocommands for "*"..function suda#BufEnter:
line   29:
E194: No alternate file name to substitute for '#': keepalt keepjumps edit suda:///home/user/#test

on :SudaWrite:

Error detected while processing function <SNR>8_write:                                                   
line    2:
E194: No alternate file name to substitute for '#': write suda:///home/user/#test

Interestingly, if I call the file '##test', the write succeeds, but a file named '##testtest' is written.

Using nvim v0.9.4. Please let me know if I can provide any more information.

Problems with suda_smart_edit if it's set after the plugin loaded. [[SOLVED]]

I use neovim with lazy.nvim and I initially installed suda.vim like this: { "lambdalisue/suda.vim" } .
When I tried to use the suda_smart_edit functionality by adding vim.g.suda_smart_edit = 1 to my ~/.config/nvim/after/plugin folder the plugin didn't work.

Then I realized I had to set vim.g.suda_smart_edit before loading the plugin so I changed my installation to this:

{
    "lambdalisue/suda.vim",
    init = function()
        vim.g.suda_smart_edit = 1
    end
}

TL; DR

Set the suda_smart_edit global variable before loading suda.vim if you want smart edit functionality.

neovim lsp jump to definition throws error on readonly files when using suda_smart_edit

This has been a long-standing issue of mine, which I finally got around to troubleshooting
I've been using suda_smart_edit = 1 because it is extremely useful when editing system files
However, I noticed when doing scala development, whenever I would use vim.lsp.buf.definition to a dependency file, I would get an error (this happens on neovim 0.8.1, but also happens on every neovim version I can remember):

"~/myproject/.metals/readonly/dependencies/play_2.13-2.8.18-sources.jar/play/api/mvc/Action.scala" 511L, 18648B
Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/uri.lua:86: Invalid buffer id: 26
stack traceback:
        [C]: in function 'nvim_buf_get_name'
        /usr/share/nvim/runtime/lua/vim/uri.lua:86: in function 'uri_from_bufnr'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:254: in function 'get_lines'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:320: in function 'get_line'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:335: in function 'get_line_byte_from_position'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1096: in function 'jump_to_location'
        /usr/share/nvim/runtime/lua/vim/lsp/handlers.lua:359: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp.lua:1383: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
Press ENTER or type command to continue

Running buffers returns:

:buffers
  1  h   "api/app/myproject/controllers/MyController.scala" line 353
 27 %a   "suda:///home/perrin4869/myproject/.metals/readonly/dependencies/play_2.13-2.8.18-sources.jar/play/api/mvc/Action.scala" line 1
Press ENTER or type command to continue

Since I noticed the buffer was renamed to suda:///, I tried disabling the plugin, and the problem was solved.
You can probably reproduce this by doing vim.lsp.buf.definition which would result in opening a readonly file on any lsp server, this is not specific to scala (but probably common in scala due to metals creating readonly dependency files).
I really don't have much of a clue how this could be solved, but I thought I'd report it

Duplicate buffers with smart edit

Adding

let g:suda_smart_edit = 1

has a strange behaviour: Editing a file that I have no write access, eg /etc/hosts from an open nvim (ie with :e /etc/hosts) works fine, but from outside (ie with nvim /etc/hosts from the terminal) leads to two open buffers, the suda and the non-suda one.

Prompt on new buffer with g:suda_smart_edit and g:suda#nopass

A prompt is displayed when creating a new buffer / non-existent file that requires sudo using the following config:

set runtimepath =$NVIM_PLUGINS/suda.vim
let g:suda_smart_edit = 1
let g:suda#nopass = 1

I think v:shell_error is set to "1" because the file doesn't exist.

Cannot retry inputting password

  1. Create a file owned by root: sudo touch foo.txt
  2. Open an another terminal (to end the sudo's session)
  3. Open vim
  4. :Suda foo.txt
  5. Edit something.
  6. :w
  7. Input an incorrect password.
  8. Error occurs without retry.
Password: ***Error detected while processing BufWriteCmd Autocommands for "suda://*"..function suda#BufWriteCmd[14]..BufWriteCmd Autocommands for "suda://*"..function suda#BufWriteCmd[3]..suda#write:
line   33:
E605: Exception not caught: Sorry, try again.^@Sorry, try again.^@^@sudo: no password was provided^@sudo: 2 incorrect password attempts^@

At this time, suda.vim inputs incorrect password severary. (See the error message.)

Problem with -p flag

There is a problem with the -p flag (which is supposed to open multiple files in different tabs).

If I use nvim -p file1 file2 then everything works as expected if the files do not require superuser permission sto write to.
But is file1 requires superuser permissions the file2 is never opened in a separate tab.
If any file is passed as a command line argument that needs superuser permissions, the files passed after that argument are not opened in separate tabs.

suda_smart_edit too slow

I like use let g:suda_smart_edit = 1

but when open a file (1000-2000 lines) , always block cursor 5 seconds or more

Is there a way to speed up? or disable it for some filetypes

Make a prompt title configurable

Hi,

Thank you for this great plugin. I use it with neovim, and it works just fine !

Would it be possible to have a setting to localize the "Password:" prompt ? Maybe something like :
let g:suda#password_prompt = 'Mot de passe :'

Thanks !

RFC on Idea: Making suda completly automatic

Continuing from #6, I would like to suggest a PR for suda.vim that will make it completely automatic. After reading the code, I've realized that it'd require to change it's internals a lot, therefor I would like to ask for comments on the idea:

What if suda.vim wouldn't work only on files with the suda:// prefix and it would automatically detect if there are read and write permissions and ask for a password for writing and reading it accordingly? For example:
If a normal user would run the command nvim /etc/sudoers, the following will happen:

  • nvim (with suda.vim installed) will attempt to read the requested file.
  • It will find out that read permissions are not available.
  • Using the user's sudo password, It would put the contents of suda:///etc/sudoers in the current buffer.
  • It will set an autocmd that will make every write operation write to suda:///etc/sudoers and not /etc/sudoers and afterwards update the contents of the /etc/sudoers buffer with the updated content.

On the other hand, when only write permission is missing, only the last action will be performed.

I'd love to hear your thoughts before I'll begin working on this,
Thanks.

Why do you set bufhidden to `delete`?

Hey,

I noticed you do this:

setlocal bufhidden=delete

Commit: f70355e .

This really bothers me when I open multiple files and some are with the suda:// prefix and switch between the buffers, the suda:// buffers just disappear.

In order to mitigate this behavior I've commented this line on my clone. Just wondering what was the rational behind this?

Prompt to re-read file after SudaWrite

This can be done by simply calling :checktime after SudaWrite.
I don't know if we should add this call elsewhere, because I only use this one command. So I created an issue instead of a pull request.
image

Cache credentials like sudo

The following shell code will probably only ask the user for their password once (at least with default sudo configurations):

sudo id
sudo id

However doing :w suda://% twice will always ask for the users password twice.

Is it possible to make suda cache credentials like sudo?

[suda] Vim(let):E730: Using a List as a String

Hello.
I'm using vim(VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jan 29 2024 09:41:19)) on openwrt.
I installed suda.vim on vim, and I tried to SudaWrite I've got this message.

[suda] Vim(let):E730: Using a List as a String   

Any help would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.