持久性 vim 全局标记

Persistent vim global marks

我最近在vim中发现了全局标记的用法。它们似乎是一个非常强大的功能,但是当 vim 关闭时它们会被删除。有什么方法可以在 vim 启动时定义 vim 全局标记(例如,通过在 .vimrc 文件中定义它们)?

你需要调查 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,%

通常,全局标记在退出时保存在 viminfo 文件中。它由 viminfo 选项决定。您可以这样检查它的值:

: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)