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
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
" Initialize Vundle and pass a path where global plugins
" should be installed
" let Vundle manage Vundle, required
" Install my custom fork of Solarized color scheme
call vundle#end() "required
filetype plugin indent on "required
" Set my color scheme globally
" 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
: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
" Call Vundle without specifying a path to install plugins in
" `$HOME/.vim/bundle`, or specify some other path under your home directory
" Install whatever plugin you want for your user
" 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.