Sudoedit Vim force write (update) without quit

sudoedit allows you to edit a file with an editor running on your own user id. It copies the file to a temporary file which your editor can then write into. As soon as the editor is closed, the edited file is copied back.

There is no built-in possibility to automatically write changes back while the editor is still running.

So you need either

  • run the editor on the other user id (e.g. sudo vi /file/to/edit )
  • copy the file manually back in a (separate) shell (sudo cp /tmp/... /file/to/edit) or from inside vim :!sudo cp % /file/to/edit. From vim you can also start a shell with :sh or put vim in the background with Ctrl+Z and restore it with fg.
  • use https://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work
  • create your own version of sudoedit which writes changes back as soon as the temporary files is changed. This should be easily doable with some scripting. Inotify can help you to detect changes (see for example Can a bash script be hooked to a file? )

You are missing the point of sudoedit, but the manual doesn't explain it so fret not.

Most editors have shell escapes, for example :shell in vim which would allow you to gain a root shell even though you might not have such permission under sudo. This is called "privilege escalation" and is a Bad Thing.

The sudoedit command does the equivalent of:

# cp source-file /tmp/some-temporary-name
$ ${EDITOR} /tmp/some-temporary-name
# cp /tmp/some-temporary-name source-file
# rm /tmp/some-temporary-name

Note that the editor is run as you, not root, so you cannot use the editor for privilege escalation.

If you have very permissive sudo rights (as is often the case on single user machines depending on the distribution) you can

$ sudo vim source-file

to get the behavior you want.

Tags:

Vim

Sudoedit