使用标记移动

标记就像书签; 他们帮助你找到你已经去过的地方。

TLDR

使用 m{a-zA-Z} 将它们设置为正常模式,然后使用'{a-zA-Z}(单引号)或 `{a-zA-Z}(反引号)以正常或可视模式跳转到它们。小写字母用于缓冲区内的标记,大写字母和数字是全局的。使用:marks 查看你当前设置的标记,有关更多信息,请参阅:help mark

设置一个标记

Vim 的内置帮助说:

m{a-zA-Z}               Set mark {a-zA-Z} at cursor position (does not move
                        the cursor, this is not a motion command).

标记将跟踪它所在的行和列。没有视觉确认设置了标记,或者标记是否具有先前值并且已被覆盖。

跳到一个标记

Vim 的内置帮助说:

Jumping to a mark can be done in two ways:  
1. With ` (backtick):     The cursor is positioned at the specified location
                          and the motion is exclusive.
2. With ' (single quote): The cursor is positioned on the first non-blank
                          character in the line of the specified location and
                          the motion is linewise.

Backtick 使用列位置,而单引号则不使用。如果你愿意,之间的区别只是允许你忽略标记的列位置。

除普通模式外,你还可以在可视模式下在非全局标记之间跳转,以允许根据标记选择文本。

全局标记

全局标记(大写字母)允许在文件之间跳转。这意味着,例如,如果标记 Afoo.txt 中设置,然后从 bar.txt(在我的文件系统中的任何位置)设置,如果我跳转到标记 A,我的当前缓冲区将被替换为 foo.txt。Vim 会提示保存更改。

跳转到另一个文件中的标记被视为移动,并且视觉选择(以及其他内容)将不会像跳转到缓冲区内的标记那样工作。

要返回上一个文件(在本例中为 bar.txt),请使用:b[uffer] #(即:b#:buffer#)。

注意:

特殊标志

Vim 会自动设置某些标记(你可以自己覆盖,但可能不需要)。

例如(从 Vim 的帮助中解释):

`[` and `]`: jump to the first or last character of the previously changed or 
             yanked text.  {not in Vi}

`<` and `>`: jump to the first or last line (with `'`) or character (with 
             <code>`</code>) of the last selected Visual area in the current 
             buffer.  For block mode it may also be the last character in the 
             first line (to be able to define the block).  {not in Vi}.

更多,来自 Vim 的内置帮助:

''  ``              To the position before the latest jump, or where the
                    last "m'" or "m`" command was given.  Not set when the
                    :keepjumps command modifier was used.
                    Also see restore-position.

'"  `"              To the cursor position when last exiting the current
                    buffer.  Defaults to the first character of the first
                    line.  See last-position-jump for how to use this
                    for each opened file.
                    Only one position is remembered per buffer, not one
                    for each window.  As long as the buffer is visible in
                    a window the position won't be changed.
                    {not in Vi}.

'.  `.              To the position where the last change was made.  The
                    position is at or near where the change started.
                    Sometimes a command is executed as several changes,
                    then the position can be near the end of what the
                    command changed.  For example when inserting a word,
                    the position will be on the last character.
                    {not in Vi}

'"  `"              To the cursor position when last exiting the current
                    buffer.  Defaults to the first character of the first
                    line.  See last-position-jump for how to use this
                    for each opened file.
                    Only one position is remembered per buffer, not one
                    for each window.  As long as the buffer is visible in
                    a window the position won't be changed.
                    {not in Vi}.

'^  `^              To the position where the cursor was the last time
                    when Insert mode was stopped.  This is used by the
                    gi command.  Not set when the :keepjumps command
                    modifier was used.  {not in Vi}

另外,字符 (){} 是跳转到与其正常模式命令相同位置的标记 - 也就是说,'} 在正常模式下与 } 做同样的事情。