Giter VIP home page Giter VIP logo

sudoedit.vim's Introduction

image

sudoedit.vim's People

Contributors

blueyed avatar chrisbra avatar inkarkat avatar lespea avatar stardiviner avatar tamamcglinn avatar timss avatar vds2212 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sudoedit.vim's Issues

Undofile does not work older systems

This problem surfaces when using eg. let g:sudoAuthArg="-u <user>".

Steps to reproduce:

  • Run Debian 7 (wheezy).
  • Run VIM 7.3 with patches <=547 installed.
    • Tested with VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Feb 10 2013 02:28:47)
  • .vimrc:
    • set undofile
    • set undodir=~/.vim/undo
    • SudoEdit.vim installed
  • sudo -u vim /tmp/test
    • Write a test file
  • vim /tmp/test
    • :let g:sudoAuth="sudo"
    • :let g:sudoAuthArg="-u nobody"
    • :SudoWrite

Now you have lost your undo. You can verify it by writing more and closing and opening the file, you won't get the undo.

Not sure what causes this. Will be happy to help debugging and testing.

SudoWrite fails to write symlink (silently)

SudoWrite does not work with symlinks:

% echo foo > foo
% sudo chown root foo
% ln -s foo symlink
% vim -c 'norm o2' -c 'SudoWrite' -c q symlink
% cat foo
foo

The expected result would be to have "2" after "foo".

`:write another-file` fails after `:SudoWrite`

TEST CASE:
1. vim /etc/hosts
2. :SudoWrite /tmp/f1
3. :w /tmp/f2

"/tmp/f1" 15L, 298C written
Error occured, when writing undofile

It should write /tmp/f2 instead.

The error related to "undofile" might not be required (I was not seeing it initially).

Ref: 306d3f9

Existing BufReadPre autocommand causes buffer in another window to be scrolled up

set runtimepath+=$PWD/sudoedit.vim

autocmd BufReadPre * echom "AU"
function! Test()
  set foldlevel=99
  normal 100gg
  split
  normal 200gg
  " SudoWrite
endfunction

Run it using vim -Nu minimal.vim autoload/SudoEdit.vim, then :call Test(), and :SudoWrite.
Notice how the buffer in the second window is at line 1, not 100.

I've noticed this for a long time already, and finally debugged it - vim-repeat is a (popular) plugin setting up BufReadPre.

Might be a bug in Vim actually - happens with both Vim (8.1.1073) and Neovim for me.

Buffer position is not being kept after ":SudoWrite"

After :SudoWrite the cursor jumps to the beginning of the file.

This behavior changed recently (I've just noticed it), and might be caused by some other changes I've made to my configuration.

I am will debug this further / provide more information, if you cannot confirm it.

SudoEdit fails when shell is zsh

SudoEdit fails with error

'Error writing to file'

This is because in zsh, >$file fails if $file already exists.

So, the fix is to either use vim functions to write/append to
file or set shell in the beginning of script to bash or so,
latter is what I have done.

Exception when trying to write to a symlink to the file itself

TEST CASE:

1. cd /tmp
2. echo foo > foo
3. ln -sfn foo bar
4. start vim with "foo"
5. :SudoWrite bar

This is the output with g:sudoDebug=1:

verb 1,1w !SUDO_ASKPASS='/usr/lib/openssh/gnome-ssh-askpass' sudo -A  tee >/dev/null 'bar'
tee: bar: Permission denied

shell returned 1
1
verb wundo! /home/user/.local/share/vim/undo/\%tmp\%foo
Skipping undo file write, nothing to undo
Error detected while processing function SudoEdit#SudoDo..<SNR>155_Exception:
line    1:

The files:

lrwxrwxrwx 1 user user 3 Dec 17 17:41 /tmp/bar -> foo
-rw-rw-r-- 1 user user 4 Dec 17 17:41 /tmp/foo

Error reading <afile>! Password wrong?

:e sudo:/etc/rc.conf
shell returned 1
Error detected while processing function SudoEdit#SudoDo..122_SudoRead:
line 9:
Error reading ! Password wrong?

Swap file warning, via `exe f`

After issue #21 is fixed, another one pops up:

There is a Found a swap file by the name ".1.swp". warning via:

line 31: exe "f" fnameescape(a:file)

This causes SudoWrite to abort:

Exception thrown: Vim(file):E325: ATTENTION
function SudoEdit#SudoDo..2_SudoWrite
line 31: End of function

TEST CASE:

  1. echo 1 > 1
  2. vim -u NONE -N -c 'set rtp+=~/.vim/bundle/sudoedit | ru! plugin/SudoEdit.vim | set undofile | exe "norm! r2" | SudoWrite' 1

I have a fix for this already, and will provide a pull request.

Got "NO (-60031)" when calling SudoWrite

When I open a file, modify it, exec :SudoWrite and got an "NO (-60031)" error. I don't know how to get more detail error message but just let g:sudoDebug=1

verb 1,20w !security execute-with-privileges tee >/dev/null  '/private/etc/hosts'
There was an error writing the file!
verb wundo! /Users/leemars/.vim/tmp/undo/\%private\%etc\%hosts
Writing undo file: /Users/leemars/.vim/tmp/undo/%private%etc%hosts

File marked as modified after SudoWrite

When creating a new file using :SudoWrite $filename it is considered to be modified afterwards.

  1. vim
  2. ifoo<Esc>
  3. :SudoWrite /tmp/foo

While /tmp/foo is written correctly, Vim considers it to be modified (see :set modified?).

I have tested it using revision 69e6ccd.

Fails to write to a file which expand() fails to expand

I just had a problem where expand() in SudoEdit#SudoDo would fail (return an empty string).

SudoWrite 1 would fail with "Cannot write file. Please enter filename for writing!".

The directory (/srv/example.com/shared) contains a symlink:

drwxr-xr-x 1 user root     310 Dec 12 22:37 /srv
lrwxrwxrwx 1 root root      28 Jul 26 16:38 /srv/example.com -> /home/www-data/example.com
drwxrwxr-x 1 user www-data 120 Nov 25 22:11 /srv/example.com/shared

Read contents suddenly disappear

Here is screencast for demo. I also run vim without configuration (-u NORC), the result is remain the same.

SudoEdit

My environment

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.6 LTS
Release:        16.04
Codename:       xenial

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jan 02 2017 06:40:50)
Included patches: 1-134
Modified by pi-rho
Compiled by buildd@lcy01-12
Huge version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     -tcl
-balloon_eval    +folding         +mouse_xterm     +termguicolors
-browse          -footer          +multi_byte      +terminfo
++builtin_terms  +fork()          +multi_lang      +termresponse
+byte_offset     +gettext         -mzscheme        +textobjects
+channel         -hangul_input    +netbeans_intg   +timers
+cindent         +iconv           +num64           +title
-clientserver    +insert_expand   +packages        -toolbar
-clipboard       +job             +path_extra      +user_commands
+cmdline_compl   +jumplist        +perl            +vertsplit
+cmdline_hist    +keymap          +persistent_undo +virtualedit
+cmdline_info    +lambda          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       +python/dyn      +vreplace
+cscope          +lispindent      +python3/dyn     +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      +lua/dyn         +rightleft       +writebackup
+diff            +menu            +ruby/dyn        -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xpm
+emacs_tags      -mouseshape      +startuptime     -xsmp
+eval            +mouse_dec       +statusline      -xterm_clipboard
+ex_extra        +mouse_gpm       -sun_workshop    -xterm_save
+extra_search    -mouse_jsbterm   +syntax
+farsi           +mouse_netterm   +tag_binary
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -Wall -Wextra -g -O2 -fPIE -Wformat -Werror=format-security -fstack-protector-all -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E  -Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,-z,nodlopen -Wl,-z,nodump -Wl,-z,noexecstack -Wl,-z,noexecheap -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux   -lacl -lattr -lgpm -ldl   -Wl,-E  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/x86_64-linux-gnu/perl/5.22/CORE -lperl -ldl -lm -lpthread -lcrypt

My configuration for Vim
https://github.com/Gasol/vimrc

Does not work on MSWin and Vim >= 8.2

gives multiple errors, starting with creating the empty error file.

see vim/vim#8600

workaround could be to use :p:8 only on the existing directory and appending the new filename later (which is not 8.3 format and wasn't in the past, because VIM appended the given text).

Write unmodified buffer, if a new name is requested

When trying to write the current buffer into a new filename, SudoEdit refuses to write it, if the buffer is unmodified: "Buffer not modified, not writing!"

I suggest that SudoEdit writes the buffer with the new name in this case.

(a workaround is to use the bang with "SudoWrite" to force writing)

E499: Empty file name for '%' or '#', only works with ":p:h": ^Isil e! %

I am getting the following error when trying to :SudoWrite without a filename:

    Cannot write file. Please enter filename for writing!
    Error detected while processing function SudoEdit#SudoDo..SudoEdit#LocalSettings:
    line   13:
    E499: Empty file name for '%' or '#', only works with ":p:h": ^Isil e! %

I will submit a fix shortly.

E325 and W13 when saving a new file (passed on command line)

I have started vim baz, added some contents and then saved it using SudoWrite directly.

This results in the following error:

Error detected while processing function SudoEdit#SudoDo..SudoEdit#SudoWrite:
line   20:
E325: ATTENTION
Press ENTER or type command to continue
W13: Warning: File "baz" has been created after editing started
[O]K, (L)oad File: 

Vim 7.3.315.

Keep undostack

I've noticed that :SudoWrite messes with the undostack: after :SudoWrite u will jump back to the top of the file, instead of undoing the last edit.

It seems like the writing is added as a separate undo step, and it could be fixed by using :undojoin probably.

Suggestions: remove editing read-only warning, do SudoWrite when saving

I added this to my vimrc, the result is that I no longer get that annoying warning when editing a file where I don't have the correct permissions, and also that my normal write binding automatically calls SudoWrite (mine is nnoremap <leader>fw :set buftype=""<CR>:write!<CR> - we may want to make this work for just regular :w). My suggestion is to include this in SudoEdit.

augroup SudoWriteReadOnly
  autocmd FileChangedRO * set noreadonly | execute "autocmd BufWriteCmd <buffer> SudoWrite"
augroup END

Having a BufReadPre autocommand scrolls other windows on ":edit"

autocmd BufReadPre * echom 'AU'

function! Test()
  call append(0, range(100))
  exe 'w' tempname()
  normal! 50gg
  split
  normal! 100gg
  " edit
endfunction

When calling edit at the end of the test setup (or manually) will scroll the buffer in the other window to the top.

This does not happen without the BufReadPre autocommand.

Filenames containing spaces generate spurious error messages

I know filenames containing spaces are a terrible idea, but alas sometimes software from non-UNIX backgrounds ships with them by default. Whilst attempting to :SudoWrite a file whose filename contains spaces, I receive the following error:

verb try | undojoin | catch /^Vim\%((\a\+)\)\=:E790/ | endtry | 1,12w !SUDO_ASKPASS='/usr/bin/ksshaskpass' sudo -A  tee >/dev/null  '/opt/ti/Code Composer Studio 7.4.0.desktop'
Error detected while processing function SudoEdit#SudoDo[46]..<SNR>163_LocalSettings:
line  114:
E172: Only one file name allowed: edit Code Composer Studio 7.4.0.desktop

Which looks like the extension is misinterpreting a filename with spaces as multiple seperate filenames, unless I'm doing something else wrong? Thanks!

EDIT: Remove spurious errors in debug log.

Cannot open undo file for writing

Trying to write something to a directory where the user does not have write permission (say /etc) you get something like this:

  1. Insert some text
  2. Try to save as /etc/foo
  3. Error:
    E828: Cannot open undo file for writing: /etc/.foo.un~

It still writes the file, but you lose all content from the buffer and have to reopen if you want to edit more.

Error when using just ":w" because s:error_file is not writable

function SudoEdit#SudoDo[23]..<SNR>150_SudoWrite[52]..<SNR>150_Exec
line 14: 1,1088w !SUDO_ASKPASS='/usr/lib/ssh/x11-ssh-askpass' sudo -A  tee >/dev/null 2>'/tmp/nvimxdnM8q/8/error' '/usr/share/awesome/lib/awful/prompt.lua'
>

sh: /tmp/nvimxdnM8q/8/error: Permission denied
shell returned 1
function SudoEdit#SudoDo[23]..<SNR>150_SudoWrite[52]..<SNR>150_Exec

This seems to come from s:error_file being used for both privileged and unprivileged writes, and not being deleted always.

Undofile not being created when using just "SudoWrite"

  1. Start vim and verify that undofile and undodir are setup
  2. Add some contents to the new buffer
  3. :SudoWrite foo
  4. exit vim

"foo" is missing from the undodir - no history has been written.

You need to trigger a regular write (using :w for example) for the undofile being generated.

Buffer might be considered to be not modified, with undo involved: "Buffer not modified, not writing!"

I have noticed several times already, that :SudoEdit says:

Buffer not modified, not writing!

In that case, the buffer is considered to be not modified by Vim, but in fact it is.

I guess that it has something to do with the &undofile handling, because AFAIK undo is involved.

It happens in cases where I edit a file, save it with SudoEdit, then undo the change and then try to save it again:

TEST CASE:

  1. echo 1 > 1
  2. vim -u NONE -N -c 'set rtp+=~/.vim/bundle/sudoedit | ru! plugin/SudoEdit.vim | set undofile | exe "norm! r2" | SudoWrite | exe "norm u" | SudoWrite' 1

Error on last commit

Hi, since the very last commit I've been getting this error when trying to execute SudoWrite:

Error detected while processing function SudoEdit#SudoDo..107_SudoWrite:
line 35:
E117: Unknown function: 107_SetBufName

Thank you very much for this awesome plugin! One of my favorites :)

Requires password twice

First of all, thanks for sharing this great plugin! It is far better than using the following,

cabbr w!! w !sudo tee > /dev/null %

, as it is not OS dependent, far easier to remember and very well documented.

I've just found it on a vi.stackexchange question and played a little with it (on linux). One point that I noticed is that it asks for the password a second time.
This seems to be caused by the wundo call on <sid>LocalSettings().

But I don't understand why it is necessary; using the cabbr above asks for the password only once, and it still writes to the undofile (I've tested restarting Vim and it still remembered the changes). If it is for some very specific corner case, do you think it is reasonable to change s:skip_wundo to a global variable?

Support for "sudo" via netrw's scp method

When trying to ":SudoWrite" a file opened via the netrw plugin (e.g. "scp://host//etc/fstab"), it fails as follows:

Error detected while processing function SudoEdit#SudoDo:
line   17:
There was an error writing the file!

It would be great if SudoWrite could manage this by e.g. logging in via "ssh" and copy/write the file using sudo from there.

Return 'no tty present and no askpass program specified' when I set 'noshelltemp' in vimrc

Hello, Chris!
I'm using SudoEdit.vim in gVim and it always returns 'no tty present and no askpass program specified' when executing ':SudoRead'. Finally I found that when I comment the following in my vimrc, :SudoRead works correctly.

set noshelltemp

So why couldn't I set 'noshelltemp' in my vimrc?

Hope for your reply!

FYI

OS: Arch Linux
Shell: zsh 5.0.5
gVim 7.4.220
Sudo 1.8.10
No graphical askpass program installed.
Latest version of SudoEdit.vim installed.

E828: Cannot open undo file for writing / odd path from undofile()

When trying to SudoWrite a file like /etc/fstab, I am getting the following error:

"/etc/fstab" 26L, 1473C written
Error detected while processing function SudoEdit#SudoDo..SudoEdit#LocalSettings:
line   16:
E828: Cannot open undo file for writing: /home/user/.local/share/vim/undo//etc/fstabetc/etc/fstabfstab

It looks like the undofile() function returns some odd path (in SudoEdit#LocalSettings), but works properly when called manually:

:echo undofile(@%)
/home/user/.local/share/vim/undo/%etc%fstab

Using Vim 7.3.315.

This is related to the fix for issue 3.

Automatically call `:rundo`

Reading the docs for :rundo it seems something similar could be added to plugin/SudoEdit.vim:

    au BufReadPost * call ReadUndo()
    au BufWritePost * call WriteUndo()
    func ReadUndo()
      if filereadable(expand('%:h'). '/UNDO/' . expand('%:t'))
        rundo %:h/UNDO/%:t
      endif
    endfunc
    func WriteUndo()
      let dirname = expand('%:h') . '/UNDO'
      if !isdirectory(dirname)
        call mkdir(dirname)
      endif
      wundo %:h/UNDO/%:t
    endfunc

But changing the paths for the user's undodir variable

Errors during exiting of Vim: E121: Undefined variable: tmp

After using SudoWrite, I see the following error on exit:

Executing VimLeave Auto commands for "*"
autocommand :call SudoEdit#Rmdir(''/tmp/vwpmAmh/6'')

Error detected while processing VimLeave Auto commands for "*":
E121: Undefined variable: tmp
E116: Invalid arguments for function SudoEdit#Rmdir

This is caused because Mkdir(s:error_dir) is called with s:error_dir being escaped already, and the VimLeave autocommand adds additional quotes around it.

I think that Mkdir should take care of shell-escaping the argument.

The plugin fail to work on Windows

On windows there are some problems one of them is that: fnmodify(path, ':p:8') return the part of the path that already exists.
i.e.
if C:\Users\Public\vim_temp_15128.txt does not exist but C:\Users\Public does then: fnmodify('C:\Users\Public\vim_temp_15128.txt', ':p:8') returns C:\Users\Public.

I suppose you are using the 8.3 format to avoid space in the path and to avoid the quote problem in the runas syntax:

runas /noprofile /user:MT-BRU-2-0102\ladmin "C:\Windows\system32\cmd.exe /k copy /Y C:\Users\Public\vim_temp_9676.txt C:\Users\vds\Desktop\hosts"

Maybe the alternative syntax is a fit that doesn't requiers the 8.3 syntax.

runas /noprofile /user:MT-BRU-2-0102\ladmin "C:\Windows\system32\cmd.exe /k copy /Y \"C:\Users\Public\vim_temp_9676.txt\" \"C:\Users\vds\Desktop\hosts\""

I'll create a Pull Request to proposes my changes.

Remark:

To convert to the 8.3 syntax you need to know what are the files in the directory.
Suppose you have in the folder the files:

  • abcdefgh1.txt
  • abcdefgh2.txt
  • abcdefgh3.txt

Convert abcdefg3.txt in 8.3 will depend of the presence of abcdefg1.txt.

Actually I believe the 8.3 name is created when the file is created in the folder or moved into the folder.

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.