Sudoedit Vim force write (update) without quit

sudoeditvim

I'm doing some scripting with Vim and I've just started using sudoedit.

Problem is, when I :w it writes to the temp file, so any testing of the script can't happen unless I quit the editor.

How can I force an update of the original, or am I missing the point of sudoedit?

Best Answer

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? )
Related Question