我最近发现了vim中全局标记的用法。它们似乎是一个非常强大的功能,但是当vim关闭时它们会被删除。是否有一种方法可以在vim启动时定义vim全局标记(例如通过在.vimrc文件中定义它们)?
通常情况下,全局标记会在退出时保存到 viminfo 文件中。这由 viminfo
选项所控制。您可以像这样检查其值:
Normally, global marks are saved in the viminfo file on exit. It is ruled by the viminfo
option. You can check its value like this:
:set viminfo?
如果它是空的,您可以在.vimrc
中设置一个通用值:
set viminfo='100,<50,s10,h
退出时应该保存全局标记。
f0
参数(因为它禁用了全局标记的保存)。自动保存通常是最好的解决方案,但如果您愿意,也可以在vimrc中设置全局标记:
function! SetGMark(mark, filename, line_nr)
let l:mybuf = bufnr(a:filename, 1)
call setpos("'".a:mark, [l:mybuf, a:line_nr, 1, 0])
endf
call SetGMark('A', '~/file.txt', 10)
您需要查看 viminfo
:help viminfo
将会提供一般信息,:help 'viminfo'
将告诉您需要设置的选项。(在这种情况下引号很重要)
If you exit Vim and later start it again, you would normally lose a lot of
information. The viminfo file can be used to remember that information, which
enables you to continue where you left off.
This is introduced in section 21.3 of the user manual.
The viminfo file is used to store:
- The command line history.
- The search string history.
- The input-line history.
- Contents of non-empty registers.
- Marks for several files.
- File marks, pointing to locations in files.
- Last search/substitute pattern (for 'n' and '&').
- The buffer list.
- Global variables.
基本上,确保viminfo文件中不包含禁用跨会话保存文件标记的f0
。
我的viminfo设置包含:
:set viminfo='100,<50,s10,h,%
map ,e :e ~/.vimrc<cr>
的方法,以便能够从 vim 中的任何位置打开/编辑我的 .vimrc 文件。这种方法不使用marks
("comma" 表示 <leader>)。关于 <leader> 的更多信息可以在此处阅读 - https://dev59.com/NXI-5IYBdhLWcg3wm5zN - Robert Ranjan