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
endif
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.
sudo vim file
is better written assudo -e file
(provided that you set the environment variableEDITOR=vim
).