≡ Menu

How To Add Bookmarks Inside Vim Editor

Building with Bookmarks
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

Vi Bookmark a Line inside file

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.

  1. Open multiple files: # vim /etc/passwd /etc/group
  2. While editing /etc/passwd file go to a specific line and type mP to create a global bookmark called P
  3. Type :n from /etc/passwd file to jump to /etc/group file in vim.
  4. While editing /etc/group file go to a specific line and type mG to create a global bookmark called G
  5. Type `P (back-tick followed by upper-case P), which will take you to the bookmark in /etc/passwd
  6. 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 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.

Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • housetier February 10, 2009, 1:38 pm

    Thank you very much! Although I use vim for some time now, I still learn something new from sites like this.

    I especially liked the short exercise 🙂

  • yoander February 11, 2009, 11:01 am

    Nice tip!

  • Narendra.s.v February 16, 2009, 10:02 am

    Ya finally got what i want 🙂 . Thanks dude

    keep posting like this

  • bartman March 2, 2009, 6:51 pm

    I was a bit confused because Mark is vim script on vim.org, that is completely different then :marks.

    http://www.vim.org/scripts/script.php?script_id=1238

  • eMancu January 24, 2010, 1:32 pm

    This is what I was looking for..
    The only thing that I need for my vim is a nice configuration for fuzzyfinder!

  • Daniel d'Andrada March 12, 2010, 3:07 am

    Good post. Thanks!

  • Alkis July 29, 2011, 5:12 am

    Greate article.
    Is there also a way to remove bookmarks?

  • Dann September 10, 2012, 9:08 am

    What are ALLOWABLE bookmark names? You use one character after m, so is there a max number of 26 bookmarks, is it per-file?

    Otherwise it is helpful and I’m on the road to being a better vim-er. Thanks.

  • Anonymous September 17, 2015, 10:48 pm

    Ramesh,
    I have a one quick question, in my Vim editor, I see all the marks . I do use marking for many years. Currently, I am facing an issue, where, I see the marks using :marks., but when I do
    ma
    y’a
    The block is not yanking/copying is not happening? do you know why? what reason it could be ?

  • Ramesh Shankar September 17, 2015, 10:49 pm

    Ramesh,
    I have a one quick question, in my Vim editor, I see all the marks . I do use marking for many years. Currently, I am facing an issue, where, I see the marks using :marks., but when I do
    ma
    y’a
    The block is not yanking/copying is not happening? do you know why? what reason it could be ?

  • Paul January 6, 2016, 5:14 am

    :delmarks a to clear bookmark a.
    :delmarks! to clear all a-z bookmarks.
    :delmarks A-Z0-9 to clear all non-a-z bookmarks