What if you want to move everything?
Although the answers here are correct, they miss something. The OP's question didn't ask about how to change everything, just .vimrc and .viminfo.
Then to achieve this we need special attention to the autoload folder and to the various plugins folders.
I wanted to move all vim configurations to the more (now) standard directory .config that is supposed to be used by all applications, even than some of them do not use it and pollute the HOME folder.
To be able to move all stuff vim needs to .config/vim, you need to make the following changes:
on your ~/.bashrc, add: export VIMINIT="source ~/.config/vim/vimrc"
- This is to instruct
vim where to find your new vimrc configuration file
on your ~/.config/vim/vimrc file, before your plugin manager, set the autoload directory adding: set runtimepath+=~/.config/vim,~/.config/vim/after
This will instruct vim to run files and scripts found in those paths. The current runtimepath can be seen with the command set runtimepath? and it will probably show something like ~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim80,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after. You need to add the new folders in that list, without compromising the old folders, as they may have lots of default scripts. You don't need ~/.vim and ~/.vim/after, as they don't exist, but no harm letting the path as is, and just add what you need.
Edited: In case your spell checker can't find the path, it is because the first directory in the list runtimepath is important and yours is probably out of order (you can check with :set runtimepath?). In this case you need to enforce the order by using the following 3 lines instead of the above:
let rtp=&runtimepath set runtimepath=~/.config/vim let &runtimepath.=','.rtp.',~/.config/vim/after'
also on your vimrc file, add: set viminfo+=n~/.config/vim/viminfo
- This will tell
vim to write the old ~/.viminfo file in the new location, now without the prefixed dot.
Almost done. Lets talk about plugins now, with, of course, a great plugin manager. If you never tried it, it is time to free yourself from the plugin madness and let a manager do the job.
- If you don't have one installed, use the following lines in your
~/.config/vim/vimrc file to download and auto-install it:
if empty(glob('~/.config/vim/autoload/plug.vim')) silent !curl -fLo ~/.config/vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim autocmd VimEnter * PlugInstall --sync | source $MYVIMRC endif
This will download and install your plugin manager. After that, the following lines will download and install a plugin of your choice (examplified with drbeco/vimtemplates and ajh16/VimCompleteMe plugins. You can add more as you wish.):
call plug#begin('~/.config/vim/plugged') Plug 'drbeco/vimtemplates' Plug 'ajh17/VimCompletesMe' call plug#end()
This call will create folders like ~/.config/vim/plugged/vimtemplates and ~/.config/vim/plugged/VimCompletesMe, and so on, for each plugin.
So now, even the plugins are all organized inside ~/.config/vim, and that really cleans the HOME directory.
Bonus: swap, backup and undo files
- Move also
swapfiles adding to your now ~/.config/vim/vimrc file: set directory=~/.config/vim/swap//,.,~/tmp,/var/tmp,/tmp - You need to create the directory with:
mkdir ~/.config/vim/swap - You can also avoid them by using:
set noswapfile
- Move the backup files to a central location adding to your
vimrc file: set backupdir=~/.config/vim/backup//,.,~/tmp,~/ - You need to create the directory with:
mkdir ~/.config/vim/backup - Or just avoid them by using:
set nobackup
- Move the
undo files adding to your vimrc the line: set undodir=~/.config/vim/undo//,. - You need to create the directory with:
mkdir ~/.config/vim/undo - Or avoid them using:
set noundofile
Note: the // at the end instructs vim to save also the directory path in the filename. Something like home%documents%letter2mama.tex.swp will be used.
Note 2: edited explanation about runtimepath as the spell checker may get confused otherwise.
Extra bonus: some plugins may need help
If you use some plugins that insist to add files to ~/.vim, you may need to read the doc for each one and come up with a solution. For example:
- Coc - For
VimPlug manager, install with Plug 'neoclide/coc.nvim', {'branch': 'release'}; the problem is that, without proper setup it attempts to read coc-settings.json from ~/.vim. There are two solutions: - Add
export VIMCONFIG="$HOME/.config/vim" to your /etc/profile (or zprofile for zsh); or - Add
g:coc_config_home="~/.config/vim" to your /etc/vim/vimrc.local config file
:help .vimrc. One good reason to do so (at least for .vimrc) is so you have your entire vim config in a single directory, making it easier to put in source control or for syncing.