简体   繁体   English

如何告诉 VIM 将 viminfo 文件存储在其他地方?

[英]How to tell VIM to store the viminfo file somewhere else?

Is it possible to tell vim to save its viminfo file somewhere else?是否可以告诉 vim 将其 viminfo 文件保存在其他地方? Such as in the.vim folder比如在.vim文件夹中

Try adding set viminfo+=n~/.vim/viminfo to your ~/.vimrc file.尝试将set viminfo+=n~/.vim/viminfo添加到~/.vimrc文件中。 From :help 'viminfo' ::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.
set viminfo='1000,nc:\\users\\abcdef\\_viminfo

This works for me in Windows 7.这在 Windows 7 中对我有用。

I had a similar problem but had no write permission to my home folder (thus could not create the ~/.vimrc ).我遇到了类似的问题,但对我的主文件夹没有写权限(因此无法创建~/.vimrc )。

I solved it by modifying (through the Administrator) the _vimrc in C:\Program Files\VIM to include the line:我通过修改(通过管理员) C:\Program Files\VIM中的 _vimrc 来解决它:

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.以防万一有人遇到这个问题并且这个答案不起作用。

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.我在尝试更改 vimrc 中 viminfo 文件的位置时遇到了同样的问题。 At last setting the value of viminfofile option worked for me.最后设置viminfofile选项的值对我有用。

Added the following to my vimrc:在我的 vimrc 中添加了以下内容:

set viminfofile=D:\vim\viminfo

It works for me on windows with vim 8.2它适用于 windows 和 vim 8.2

TL;DR TL;博士

This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment .这似乎是vim中的一个错误,其中set nocompatible不是幂等的并且不遵循 最小惊讶原则

As a workaround, either:作为一种解决方法,或者:

  1. Ensure that you set nocompatible (or the equivalent set nocp ) only once , and at the top of your vimrc .确保只设置一次set nocompatible (或等效的set nocp ),并且在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):来自:help compatible (emahsis 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.小心:设置或重置此选项可能会产生很多意想不到的效果:映射以另一种方式解释,撤消行为不同等。如果您在 vimrc 文件中设置此选项,您可能应该把它放在最开始。

Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo :请注意&viminfo没有在副作用中列出,但是以下几行清楚地显示了&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: 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:我提出了两个关于此的 GitHub 问题:

See also Vi StackExchange's question: Can't move viminfo file - &viminfo reverts upon loading vim which I raised before seeing this one.另请参阅 Vi StackExchange 的问题: Can't move viminfo file - &viminfo reverts on loading vim我在看到这个之前提出的问题。

The ordering of settings in the.vimrc affects the use of:set viminfo. .vimrc 中设置的顺序会影响:set viminfo 的使用。 The setting of viminfo should be after the setting of nocompatible. viminfo 的设置应该在 nocompatible 的设置之后。 After reordering my.vimrc, the above solution to have the viminfo file be located in the.vim directory worked for me.重新排序 my.vimrc 后,上述将 viminfo 文件位于 .vim 目录中的解决方案对我有用。

In gvim8.2 on Windows10, following option works to change path of viminfo:在 Windows10 上的 gvim8.2 中,以下选项可用于更改 viminfo 的路径:


set viminfofile=$VIM/.viminfo

Write this in.vimrc.把这个写在.vimrc 中。

It could to change viminfo file path to under the $VIM.它可以将 viminfo 文件路径更改为 $VIM 下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM