I found the following command, which saves the current file with sudo, meaning you can open a file you don't have write access to (as long as you do have read access), in the amazing Ultimate vimrc by amix:

command W w !sudo tee % > /dev/null

If you stick this in your vimrc, then type :W in any open file, vimgives you 2 options: Load, which reloads the file with the new changes, and OK, which does not reload the file, but keeps the changes to the current buffer.

Is there an equivalent command for saving and quitting at the same time? It would be to :x what this :W command is to :w.

  • I couldn't find a relevant tag for the sudo part. sudo is not a tag, nor root, nor privileged.
    – trysis
    Nov 22, 2015 at 17:04

1 Answer 1


I don't think there is a save/quit automated, but as a reference to several nix-oriented tasks, there is a plugin that covers much of it (e.g. SudoWrite). (plugin: eunuch)

To combine the two tasks without using sudo you can use :wq of course.
I attempted using :w !sudo tee % | q which failed.
Using the eunuch plugin instead allows: SudoWrite | q which successfully writes and then quits.

Thus, installing that plugin and then using :command! W SudoWrite | q does the intended sudo-write and then quit.

  • better use my SudoWrite plugin, which works on Windows and supports undofiles Nov 24, 2015 at 18:08
  • Windows has something equivalent to sudo ?
    – Cometsong
    Nov 24, 2015 at 18:09
  • Why shouldn't it have? Nov 24, 2015 at 18:10
  • Yeah I spose any admin user is similar to root. Cool.
    – Cometsong
    Nov 24, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.