17

All the vim related files installed on /usr/share/vim/vim74.

cd /home/debian8/
ls  .vim*
.viminfo  .vimrc

To create .vim directory to install some plugin for customization.

mkdir  .vim
mkdir  .vim/plugin
mkdir  .vim/autoload

If .viminfo and .vimrc are moved into .vim directory directly, then the configuration for vim can't be read.

How can .vimrc and viminfo be relocated into the .vim directory?

2
  • 2
    why do you want to? That's not where they are supposed to go.
    – Herb
    Mar 25, 2017 at 3:29
  • @HerbWolfe That's not really true: see :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.
    – Rich
    Apr 5, 2017 at 9:07

5 Answers 5

10

Note: @Rich's answer is more correct, but this is still a valid way to set a "default" arbitrary path for vimrc.

The easiest way to have a custom vimrc location is to use the VIMINIT environment variable:

export VIMINIT="source ~/.vim/vimrc"

See :h VIMINIT

It's ignored when using vim -u ~/some/other/vimrc

As for viminfo, you need to add this to your vimrc file:

set viminfo+=n~/.vim/viminfo

See :h 'viminfo'

19

You don't need any shell configuration to store your .vimrc file in the .vim directory. You just need to remove the . or _ from the filename.

Vim looks in the following locations by default on startup:

On Unix (including OS X/macOS):

~/.vim/vimrc

On Windows:

%HOMEPATH%\vimfiles\vimrc

See section II of :help .vimrc for the documentation on this.

As @Tommy A has already explained, you can set the location of the viminfo file by adding the following command to your vimrc:

set viminfo+=n~/.vim/viminfo
1
  • 1
    Totally right. I missed that part of the docs because I'm using Neovim.
    – Tommy A
    Apr 5, 2017 at 12:54
13

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.

3
  • 2
    This is a great answer, but it's an answer to a different question. This question is about how to move the .vimrc and .viminfo file into the .vim directory (which is itself in the normal location). The existing answers don't discuss .vim/autoload because the OP doesn't want to move that directory.
    – Rich
    May 23, 2019 at 10:30
  • 3
    Thanks, I understand that. But to be honest, when I was trying that myself, googling, I ended up here. So, lets say, even if this answer is not the "accepted" one, it will still help other googlers that ended up here as myself. (edited the intro anyway, to avoid confusion)
    – DrBeco
    May 23, 2019 at 14:18
  • 1
    It helped me, thanks beco! Feb 22, 2022 at 14:25
2

I use symbolic link.

ln -s ~/.vim/vimrc ~/.vimrc

0

For me hard links are the best for this purpose,

ln ~/.vimrc .vim/.vimrc

This will let you edit any of them and if you have been using git for storing .vim We can just create after pulling it from github.

2
  • What is the advantage of using a hard link over a symbolic link in this case? Nov 29, 2017 at 0:17
  • 1
    @MartinTournoji For filesystem that does not support symbolic link, such as NTFS before Windows Vista. Aug 12, 2020 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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