26

Is it possible to tell vim to save its viminfo file somewhere else? Such as in the .vim folder

7 Answers 7

26

Try adding set viminfo+=n~/.vim/viminfo to your ~/.vimrc file. From :help 'viminfo':

    n       Name of the viminfo file.  The name must immediately follow
            the 'n'.  Must be the last one!  If the "-i" argument was
            given when starting Vim, that file name overrides the one
            given here with 'viminfo'.  Environment variables are expanded
            when opening the file, not when setting the option.
4
  • Which doesn't work? The option or the specific value I suggested? Feel free to give it any reasonable path that makes sense for your platform.
    – sarnold
    Jun 9, 2011 at 2:19
  • it doesnt work as in, after I added that into my vimrc file, viminfo still gets saved directory under my home folder... Unless I set it manually everytime I open vim, that would work, but not if I have it in vimrc
    – Jimmy Lu
    Jun 9, 2011 at 2:43
  • 1
    @BeyondSora, @sarnold Reread help that is quoted here: «the name must immediately follow the 'n'». Purge semicolon out of the answer.
    – ZyX
    Jun 9, 2011 at 10:37
  • 1
    @ZyX, many thanks! I had just tested with a directory without write access, and was happy to get an error when it didn't work. Sigh. Lesson learned. :)
    – sarnold
    Jun 10, 2011 at 0:30
6
set viminfo='1000,nc:\\users\\abcdef\\_viminfo

This works for me in Windows 7.

3
  • 1
    Yes, I tried set viminfo+='1000,n~/.vim/viminfo instead and it worked.
    – kenmux
    Jul 11, 2016 at 3:57
  • 1
    Worked for me on Windows 10 with Vim 8.0.x. It even works with a variable, e.g.: set viminfo='1000,n$VIM/viminfo.
    – benichka
    Feb 19, 2018 at 6:56
  • 2
    What does the 1000 do? Apr 7, 2019 at 13:53
4

I had a similar problem but had no write permission to my home folder (thus could not create the ~/.vimrc).

I solved it by modifying (through the Administrator) the _vimrc in C:\Program Files\VIM to include the line:

let $HOME = $USERPROFILE

I placed it at the beginning of the file and it worked well. Just in case someone comes across this question and this answer didn't work.

2

I know its quite a while now, but I am just mentioning this for future readers.

I was facing the same issue while trying to change the location for viminfo file in vimrc. At last setting the value of viminfofile option worked for me.

Added the following to my vimrc:

set viminfofile=D:\vim\viminfo

It works for me on windows with vim 8.2

0

TL;DR

This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment.

As a workaround, either:

  1. Ensure that you set nocompatible (or the equivalent set nocp) only once, and at the top of your vimrc.

  2. Don't set it again if it's already set:

    if &compatible | set nocompatible | endif " Avoid side effects if `nocp` already set
    

Explanation and bug illustration

From :help compatible (empahsis mine):

This is a special kind of option, because when it's set or reset, other options are also changed as a side effect. CAREFUL: Setting or resetting this option can have a lot of unexpected effects: Mappings are interpreted in another way, undo behaves differently, etc. If you set this option in your vimrc file, you should probably put it at the very start.

Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo:

set nocompatible
set viminfo+=nWatch-my-viminfo-file-location-be-ignored
echom &viminfo
set nocompatible " do side effects even though nocomptible is already set
echom 'After 2nd "set nocompatible":'
echom &viminfo

Output:

'100,<50,s10,h,nWatch-my-viminfo-file-location-be-ignored
After 2nd "set nocompatible":
'100,<50,s10,h

vim --version | head -1

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 05 2016 16:48:20)


Resolution

I have raised two GitHub issues regarding this:

See also Vi StackExchange's question: Can't move viminfo file - &viminfo reverts upon loading vim which I raised before seeing this one.

0

The ordering of settings in the .vimrc affects the use of :set viminfo. The setting of viminfo should be after the setting of nocompatible. After reordering my .vimrc, the above solution to have the viminfo file be located in the .vim directory worked for me.

0
0

In gvim8.2 on Windows10, following option works to change path of viminfo :


set viminfofile=$VIM/.viminfo

Write this in .vimrc .

It could to change viminfo file path to under the $VIM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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