Friday, October 22, 2010

Bookmarking in vim editor

While editing lengthy files in any editor its very helpful to have bookmarking feature. Almost all modern browsers have this feature. While I was using vim editor I found it extremely difficult to move across different lines in a file and going back and vise-verse. Glad that there is something called "mark" in vim which is the bookmarking feature for vim editor. Using this you can bookmark a particular line and later return to the same place quickly. If you are a frequent vim user, this simple technique will be the most useful feature and you will love it.

There are mainly two types of bookmarking techniques in vim, local and global bookmarking.

Local Bookmarking:

Local bookmarking or single-file bookmarking, as the word implies is within a single file. When we want to go to a specific position we can use this. Local bookmarking is always in lowercase. So to create a local bookmark type "m" followed by the bookmark-name in vim's command mode.

m{bookmark-name}

The thing to note here is {bookmark-name} should be always be an alphabet to refer to the name of the bookmark. So if you type "ms" it will create a bookmark on the current line at the current location with name "s". Now to access the bookmark you need to type "'" or "`" followed by the created bookmark-name in vim's command mode.

'{bookmark-name}

`{bookmark-name}

Using both of these commands you can access the bookmarked position. So what is the difference? '{bookmark-name}, which is "single-quote"{bookmark-name} takes you to the beginning of the bookmarked line. Now `{bookmark-name}, which is "backtick"{bookmark-name} takes you to the exact bookmarked position on the line.

Global Bookmarking:


Global bookmarking or multiple-file bookmarking, is 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 bookmarking feature. This is very powerful as you can move across different files without losing context. Global bookmarking is always in upper case.

m{BOOKMARKS-NAME}

Lets see this example

Open multiple files
$ vim /var/www/css/style.css /var/www/css/global.css

style.css file go to a specific line and type mA to create a global bookmark called A

Now :n from style.css file to go to global.css file

Inside global.css file go to a specific line and type mB to create a global bookmark called B

Now type `A, to go back to the bookmarked place in style.css

Now from style.css, type `B, it take you back to global.css

Display All Bookmarks:


To display all the bookmarks you have made use the "marks" in the last line mode of vim.

All bookmarks
:marks

A specific bookmark
:marks {bookmark-name}



Apart from the above bookmarks, you can also notice that there are other marks as well like ‘ (single-quote), ” (double quote), [ , ], ^ and . (period) are created by vim by default and you don’t have any control over it. 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.

Utilities of Bookmarking:


There are much more you can do using marks other than bookmarking. We can use them as boundary when doing stuff over multiple lines. Just see these examples, assume you have marked the beginning of the area with a and the end with b. Now we can do stuff like:

delete from a specific area to another area
:'a,'b d

write from a specific area to another area to a new file
:'a,'b w {filename}

substitute only a specific area
:'a,'b s/this/that/g

Summary:

:marks – Display all the bookmarks

m{bookmark-name} – Creates a bookmark called {bookmark-name}

`{bookmark-name} – Go to the exact location of the bookmark {bookmark-name}

‘{bookmark-name} – Go to the beginning of the line of the bookmark {bookmark-name}

:marks {bookmark-name} – Display the details of the bookmark with name {bookmark-name}

No comments:

Post a Comment