使用標記移動

標記就像書籤; 他們幫助你找到你已經去過的地方。

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}

另外,字元 (){} 是跳轉到與其正常模式命令相同位置的標記 - 也就是說,'} 在正常模式下與 } 做同樣的事情。