How To Add Bookmarks Inside Vim Editor



Photo courtesy of secretlondon123

This is a guest post written by SathiyaMoorthy



This article is part of the ongoing Vi / Vim Tips and Tricks series. Mark is a bookmarking feature inside Vim editor, where you can bookmark a particular line and later return to it quickly. If you are using Vim editor regularly, this simple technique of using bookmarks will sky-rocket your productivity.



There are two types of bookmarks in Vim — local and global.



1. Vim Local Bookmark

Within a single file when you want to go to a particular position or line, then you can use local marking. If the bookmark name is a lower case letter, then that is local mark.

How to Create Bookmark Inside Vi?

Type m{macro-name}. macro-name is an alphabet to refer to the name of the bookmark.



If you type “ma” , it will create bookmark on the current line at the current location with name “a”. In the following example, typing ma has created a bookmark at the exact location where the cursor is highlighted.

ma

Fig 1: Bookmark current location inside Vim using ma

Two Methods To Access Bookmarked Line Inside Vi

Method 1: `{macro-name}

backtick followed by the macro name. Move to the exact bookmark location. This will jump to the exact character location within the line from where it was bookmarked earlier.



For example, if you type `a , it will take you to the bookmark with name “a”. i.e It will take you to the place where the cursor is high-lighted in the above Fig 1.

`a

Method 2: ‘{macro-name}

single-quote followed by the macro name. Move to the beginning of the bookmarked line.



For example, if you type ‘a , it will take you to beginning of the line that has the bookmark with name “a”. It will take you to the the beginning of the “CustomLog logs/access_log combined” line in the above Fig 1.

'a

2. Vim Global Bookmark

When you have multiple files open, if you want to go to a particular position in any one of the open files, then you can use Global mark feature of Vim. If the bookmark name is an upper case letter, then that is a Global Bookmark.



Following sequence of steps will explain how to use global bookmark while editing multiple files.

Open multiple files: # vim /etc/passwd /etc/group While editing /etc/passwd file go to a specific line and type mP to create a global bookmark called P Type :n from /etc/passwd file to jump to /etc/group file in vim. While editing /etc/group file go to a specific line and type mG to create a global bookmark called G Type `P (back-tick followed by upper-case P), which will take you to the bookmark in /etc/passwd From /etc/passwd, type `G (back-tick followed by upper-case G), which will take you to the bookmark in /etc/group.

3. How to Display All Bookmarks

If you’ve created several bookmarks and don’t remember them, you can easily get a list of all the bookmarks by typing :marks as shown below.

:marks mark line col file/text a 15 9 ypserver 192.168.1.119 b 11 18 domain THEGEEKSTUFF G 56 0 group P 45 0 passwd



This indicates that I have created the following bookmarks:

a – local bookmark with name “a” at line 15 and col 9. This also give the text of line#15 . This is from the current open file, which is yp.conf

b – local bookmark with name “b” at line 11 and col 18. This also gives the text of line#18. This is from the current open file, which is yp.conf

G – global bookmark with name “G” at line 56 and col 0 of “group” file

P – global bookmark with name “P” at line 45 and col 0 of “passwd” file.



Apart from the above bookmarks, anytime you type :marks inside Vim, you may get the following lines. These marks ‘ (single-quote), ” (double quote), [ , ], ^ and . (period) are created by vim by default and you don’t have any control over it.

:marks mark line col file/text ' 8 12 #^IUse broadcast on the local net for domain NISDOMAIN " 1 0 # /etc/yp.conf - ypbind configuration file [ 11 0 domain THEGEEKSTUFF ] 11 19 domain THEGEEKSTUFF ^ 11 19 domain THEGEEKSTUFF . 11 18 domain THEGEEKSTUFF



For example, the mark . (period) indicates the last position where a change was made. So, you can always do `. (back-tick followed by period), which will take you to the position where the last change was done.

4. Quick Summary of Vim Bookmark Commands

ma – Creates a bookmark called a

`a – Jump to the exact location (line and column) of the bookmark a

‘a – Jump to the beginning of the line of the bookmark a

:marks – Display all the bookmarks

:marks a – Display the details of the bookmark with name a

`. – Jump to the exact location (line and column) where the last change was performed

`. – Jump to the exact location (line and column) where the last change was performed ‘. – Jump to the beginning of the line where the last change was performed

Recommended Reading

Vim 101 Hacks, by Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.

If you enjoyed this article, you might also like..