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.

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

6 Answers 6


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
    Commented Jun 12, 2015 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)
    – Robin
    Commented Jun 11, 2015 at 18:57
  • 5
    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
    Commented Jun 12, 2015 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
    Commented Apr 5, 2018 at 9:37
  • On Ubuntu 20.04 it is not needed to set EDITOR=/usr/bin/vim, default editor is already vim Commented Mar 22 at 13:59

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.)

  • 4
    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
    Commented Jun 16, 2015 at 8:04
  • 1
    This is my favourite solution. I'll report back if I experience any issues running vim as my own user.
    – david_nash
    Commented Jul 5, 2020 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!!.


You can install the plugins and settings you need as root globally.

Check the installed system vimrc file. On Ubuntu 22.04, it's at /etc/vim/vimrc. At the bottom you see this code:

" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
    source /etc/vim/vimrc.local

This tells you there's an option for a global configuration file at /etc/vim/vimrc.local. If you don't already have that file, create it.

I use Vundle for my plugin manager, so I install Vundle under vim's system directory with this command:

 sudo git clone https://github.com/VundleVim/Vundle.vim.git /usr/share/vim/bundle/Vundle.vim

You can now edit the global configuration file.

set nocompatible "required for Vundle installation

" Set the runtime path to include Vundle
set rtp+=/usr/share/vim/bundle/Vundle.vim

" Initialize Vundle and pass a path where global plugins 
" should be installed
call vundle#begin('/usr/share/vim/bundle')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" Install my custom fork of Solarized color scheme
Plugin 'codealfa/vim-colors-solarized'

call vundle#end()  "required
filetype plugin indent on "required

" Set my color scheme globally
colorscheme solarized

" Any other settings I want all users on the system to use for vim
set statusline=%<%f\ b%n\ n%L\ %h%m%r%=%-14.(%l,%c%)\ %P

Save file :w, source :so %, then install plugins :PluginInstall. Source the file again to apply the changes immediately. These settings and plugins are now available when you open vim as root sudo vim file.

You can still install plugins or configure settings for your user only by editing $HOME/.vimrc.

" Call Vundle without specifying a path to install plugins in 
" `$HOME/.vim/bundle`, or specify some other path under your home directory
call vundle#begin()

" Install whatever plugin you want for your user
Plugin 'vim-vdebug/vdebug'

call vundle#end()

" I also add the command given in the accepted answer in case I'm editing 
" a system file and forgot to open vim as root user
command! -nargs=0 Sw w !sudo tee % > /dev/null

Same procedure as above for installing plugins. These are only available for your user.

  • It probably doesn't matter if someone is executing Vim as root, but do you really want to trust your system security to third-party plugin code? Use sudoedit or similar: run your editor with your plugins, and let a different mechanism handle writing the file with the correct privileges.
    – D. Ben Knoble
    Commented Mar 10, 2023 at 19:53

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.
    – Robin
    Commented Jun 15, 2015 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
    Commented Jun 16, 2015 at 8:10

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.