Using Markers in Vim

Written by Mark Sanborn: Jun 9, 2008

Vim is a powerful text editor that pretty much rivals anything out there. Once you get used to the commands and hotkeys you can wiz through config files. Vim is also very popular for programming, not only for its productivity reasons but also because it incorporates syntax highlighting, aliases, code folding, line numbers, and more.

It also makes perfect sense to use vim when editing remote files for two reasons. The first being that vim can connect remotely through FTP, SFTP, or SCP. The second is because vim is almost always installed by default on Linux systems. This means if you have SSH access to your web host you will be able to modify files with vim directly through SSH.

Today we are going to discuss vim markers.

Markers are like little bookmarks within your text files allowing you to jump around your document/code. Since vim uses modes and pretty much any action can be incorporated we can also use markers to:

  • Flip back and fourth through text
  • Delete large chunks of text
  • Copy/Cut large chunks of text
  • Paste large chunks of text
  • Marking code for folding

Usage

  • mk (set marker to k, You can use up to 26 markers a-z
  • ‘k move to the begging of the line of marker location ‘k’
  • `k move to marker ‘k’

Examples

Flip back and fourth through Text

To flip back and fourth through text you start by setting your first marker. You can set the marker to, ‘k’ by doing, ‘mk’ in normal mode. Just in case you forgot, you can get into normal mode by pressing esc. Then go to another part of the document you want to refer to and set the marker to, ‘n’. As you did before the command would be, ‘mn’. Now when you whenever you want to flip to the k marker you can use, ‘k**' or '**n’ for the ‘n’ marker.

Delete large chunks of data.

Since vim’s command structure is so easy you can probably guess what command will make this happen. What we need to do is first set the marker at the bottom of the text we want to delete. This marker is going to tell vim’s delete command to stop. We can then position our cursor at the top of the text we want to delete and type, ‘d’k’. This will tell vim to delete everything from the current position to the marker ‘k’.

Copying large chunks of data

Of course this exact method can be used to copy the text. Vim’s key for grabbing text is ‘y’. It stands for yank. To yank the text between our cursor and our marker we can issue a command like, ‘y’n’.

Marking code for folding

Vim will fold large chunks of code into one line. We can fold code between the current position and our marker by using, ‘zf’n’. The ‘z’ is the folding command ‘f’ tells it to fold and the ‘‘n’ tells vim when to stop. To quickly open the fold and look at it you can use, ‘zo’. To then close it you can use, ‘zc’. A vim fold will include the number of lines and the first line. It might look something like this:

+-- 23 lines: function date_i18n( )-------

Note: Markers are not permanent, if a line or character associated with a marker is deleted, the marker will be canceled.

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Share: