vim Using Marks to Move Around


Example

Marks are like bookmarks; they help you find places you've already been.

TLDR

Set them in normal mode with m{a-zA-Z}, and jump to them in normal or visual mode with '{a-zA-Z} (single quote) or `{a-zA-Z} (backtick). Lowercase letters are for marks within a buffer, and capital letters and digits are global. See your currently set marks with :marks, and for more info see :help mark.

Set a mark

Vim's built-in help says:

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

The mark will keep track of which line and column it was placed at. There is no visual confirmation that a mark was set, or if a mark had a previous value and has been overwritten.

Jump to a mark

Vim's built-in help says:

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 uses the column position, while Single-quote does not. The difference between simply allows you to ignore the column position of your mark if you want.

You can jump between non-global marks in visual mode in addition to normal mode, to allow for selecting text based on marks.

Global Marks

Global marks (capital letters) allow for jumping between files. What that means is if, for example, mark A is set in foo.txt, then from bar.txt (anywhere in my filesystem), if I jump to mark A, my current buffer will be replaced with foo.txt. Vim will prompt to save changes.

Jumping to a mark in another file is not considered to be a movement, and visual selections (among other things) will not work like jumping to marks within a buffer.

To go back to the previous file (bar.txt in this case), use :b[uffer] # (that is, :b# or :buffer#).

Note:

Special marks

There are certain marks that Vim sets automatically (which you are able to overwrite yourself, but probably won't need to).

For example (paraphrased from Vim's help):

`[` 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}.

More, from Vim's built-in help:

''  ``              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}

Additionally, the characters (,),{, and } are marks which jump to the same position as would their normal-mode commands – that is, '} does the same thing in normal mode as }.