Linux Vim Editor Tips and Tricks – Save Time Through Macros and Bookmarks

By | 20/05/2013

Formatting

Suppose we have an unformatted database file which looks like

Index Name Location Preference

1 Rupali CA Veg

2 Himanshu CA Non-Veg

3 Reetika Pune Non-Veg

4 Allan PA Non-Veg

5 Olivia NY Veg

6 Arry CHD Non-Veg

To format it in proper columns, type on the vim prompt

:%!column -t

It is amazing to see how columns get formatted instantly

Index  Name      Location  Preference

1      Rupali    CA        Veg

2      Himanshu  CA        Non-Veg

3      Reetika   Pune      Non-Veg

4      Allan     PA        Non-Veg

5      Olivia    NY        Veg

6      Arry      CHD       Non-Veg

We also have the ‘sort’ vim utility to sort the database, and here we are doing sorting with respect to second column.

:%!sort -k2

Now, the data looks like

4      Allan     PA        Non-Veg

6      Arry      CHD       Non-Veg

2      Himanshu  CA        Non-Veg

5      Olivia    NY        Veg

3      Reetika   Pune      Non-Veg

1      Rupali    CA        Veg

Bookmarks

While reading/editing a file in a Vim editor, we can even bookmark a particular line and instantly come back to it at any point of time. To bookmark a particular line, move the cursor to that line and press the key m following by a character which identifies the bookmark.

For example, type mr.  This bookmarks the current line and identifies it by character r.

Now, if you type the following at the vim command prompt

’r

This will bring the cursor/control back to the bookmarked line.

Leave a Reply

Your email address will not be published. Required fields are marked *