It's really frustrating when I need to edit a file as root and use sudo vim somefile.conf and I can't use any of my keybindings and settings and plugins.

Is there a way to get all my customizations when I need to edit as root?

I am using vim-plug for my plugins.

  • 5
    On a side note: sudo vim file is better written as sudo -e file (provided that you set the environment variable EDITOR=vim).
    – lcd047
    Jun 11 '15 at 4:31
  • @lcd047 : you should make that comment into an answer Sep 7 '17 at 7:36

Rather than opening vim as root, you can simply save as root by redirecting to tee. Here is an example:

:w !sudo tee % > /dev/null

This may be useful to alias in your .vimrc, I use :Sw:

command! -nargs=0 Sw w !sudo tee % > /dev/null
  • 4
    This is useful for the I did a bunch of editing and then realized I should have used sudoedit, but for the general case I would suggest using sudo -e/sudoedit.
    – jamessan
    Jun 12 '15 at 17:26

Two things:

  1. Use $ sudo -e file to edit file with $EDITOR. Add export EDITOR=/path/to/vim if $EDITOR is not already set.

  2. Going vanilla is an excellent way to fight your plugin addiction.

  • 1
    -1 for suggesting plugin addiction recovery (J/K, no downvote really) Jun 11 '15 at 18:57
  • 4
    Note that this doesn't run vim as root, like sudo vim does. The editing happens as your normal user and then sudo handles updating the original file.
    – jamessan
    Jun 12 '15 at 2:35
  • Note: there are two similar options to sudo: -E and -e. -E means "preserver environment", and this is not the one you want. -e, in turn, means "don't run a command, but edit a file with a defined procedure".
    – iksemyonov
    Apr 5 '18 at 9:37

It looks like all you have to do is symlink root's ~/.vimrc and ~/.vim/ to your's.

Just run as root:

ln -s /home/<user>/.vimrc /root/.vimrc
ln -s /home/<user>/.vim/ /root/.vim/

And that should do it!

(P.S. I figured this out just now and wanted to share with any other noobs. Please let me know if there's a better way.)

  • 3
    This can often cause problems in that some vim related files will get written as root in your home directory and later you will encounter errors trying to run your own vim configuration as your own user.
    – Caleb
    Jun 16 '15 at 8:04
  • This is my favourite solution. I'll report back if I experience any issues running vim as my own user.
    – david_nash
    Jul 5 '20 at 23:45

If you know ahead of time that you need to be editing a file as root, using the commands sudoedit <file> or sudo -e <file> will get you setup in a special environment for doing so with your usual editor preferences. A copy of the file is made and your editor is launched to edit it (as your user). When you exit the file is copied back to it's original location as root.

That is the safest and recommended way, but in the event that a) you don't realize ahead of time you will need to write as root or b) want to edit and save incremental changes in-place, you will need something else. To this end the solution using sudo tee proposed by smpl is definitely on the right track, but there are some minor improvements that can be made to handle edge cases. Notably the difference in handling the sudo password prompt in a GUI vs. console interfaces is worth some attention.

An easier way to set this up is to use the vim-eunuch plugin. It has this particular issue and a few other handy things rolled up into a handy package. Usage is as simple as calling :SudoWrite. I even use a slightly handier access mapping:

cmap w!! call SudoWrite()

That should get you the ability to write a any file as root using :w!!.


Another option is to run sudo -E vim somefile.conf to edit, which will preserve your environment, hence your .vimrc and plugins will be used.

  • Hey, good trick! That's way useful for Vi and lots of other commands. Jun 15 '15 at 17:43
  • 3
    This has the same side effects that linking your configuration into root's home directory: namely that any vim operations or plugins that write state data will do so in your home directory. These files end up being owned by root and will cause errors later as your normal user will not be able to continue using them.
    – Caleb
    Jun 16 '15 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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