8 Interesting And Really Helpful Vim Editor Features

By | 11/07/2013

In one of our earlier article on vim, we discussed about macros and bookmarks. Besides these features, there are many other features that are very helpful while text and code editing in Vim editor. In this article, I will present eight interesting and really helpful features that I use every time I work on Vim editor

 

Interesting Vim editor features

Here is a list of 8 features :

1. Auto indent everything within a code block using =%

Suppose we have following code:

vim1

You can see that the code is not well indented. Now, in Vim editor, yo indent this code, what you can do is, you can bring cursor on the starting brace of the block and hit = followed by shift+6 ie %. This should indent all the code inside this block.

Here is the output at my end :

vim2

So you can see that all the code inside the block got indented.

NOTE – If you are inside the block somewhere and still want to indent complete block then use =}

2. Auto indent specific number of line using =[num]

In example 1, we learned how to indent complete block of code. But, there could be situations in which you would like to indent only specific number of lines and not the complete block. In that case, you can hit = followed by the number (followed by the down arrow key). For example =6 will indent 6 lines.

Here is what happened when I used =6 while my cursor was at the starting brace :

vim3

So, you can observe that 6 lines got indented. But, if you place the cursor on a code line and use =6 then it indents 7 lines (an extra line is indented). I am not sure why this happens.

Here is an example of this :

vim4

So you can see that 7 lines were indented when cursor was on the first line after brace and I pressed =6.

NOTE – You can indent the current line using == or using >> (for indenting) and << (for un-indenting).

3. Indent to the end of file using =G

There could be situations in which you would want to indent complete file rather than a code block or a single line. In that case, you can place cursor at a position and hit =G.

Here is an example :

vim5

So you can see that all the lines (below the line where the cursor was present) were indented.

NOTE – Please note tha G is in capital (ie shift+g).

4. Search a word using *

Most of you would be aware of simple search in vim that we can do by hitting / followed by typing the word to search followed by hitting enter and finally followed by hitting n to search forward. So you can see so many ‘followed by’ steps here. :-)

If one instance of the word (to search) is present right in front of your eyes and you just want to see its other occurrences then this can easily be done by placing cursor under that word and then hitting * (ie shift+8).

For every hit on * key, Vim will take you to next occurrence of the word. So, the real advantage is that you need not type the word (as explained in para 1 above). Also, it will search that particular word only (just like ‘search whole word only’ option that you see in GUI based searches).

NOTE* only searches in forward direction. Use # to search in backward direction.

5. Upgrade privileges from within vim editor using :w !sudo tee %

This happens to me most of times. I open a file, edit it and when I try to save it using :w, I get an error that root permissions are required to update this file. Sometimes, with the number of changes that you have done, it practically becomes a nightmare to do :q! (ignore changes and quit).

In this scenario, you can upgrade the privileges from within the vim editor using :w !sudo tee %

Here is an example :

vim6

So you can easily enter your password here and then do a comfortable :wq

6. Use dot (.) command to repeat the previous command

If you would like to repeat the previous command then you can press dot (.) on every line on which you want to repeat the previous command.

For example, if I indent a line using == and then I realize that I want to indent 2 other lines at some other location in code then I can simply go to those lines and press dot (.)

This will repeat the == command on these lines.

7. Traverse the code using ^ , & and %

You can check for closing braces by placing cursor at the beginning braces and pressing %. This is especially useful while checking opening and closing braces of functions as well as multiple ( and ) braces in if conditions.

You can use ^ to go to beginning of a line and $ to go to end of a line.

8. Try visual mode using v

You might have used yy to copy multiple lines of a file in vim editor. When I use this feature, it works mostly fine but there are instances when I miscalculated number of lines and end up copying wrong number of lines. I am not at all blaming yy but blaming myself for this.

To solve this problem, vim offers visual mode where you can actually select any number of lines just like you would do it with mouse on a GUI based editor.

Here is an example :

Suppose I have to select all the code inside main function and I do not want to use yy as I do not like counting lines. So, what I do is, I place the cursor at very first line inside main() function and press v

vim8

You can observe the text –VISUAL– at the bottom of the window. This means that vim has entered into visual mode. Next what you do is, hold bottom arrow key to select the next lines until the end of this function. Use right arrow key to select the code in right direction.

vim9

Once selected, you can :

  • hit y to copy selected code
  • hit p to paste the copied code

Once in visual mode, instead of using arrow keys, you can also:

  • Use % to select blocks
  • Use ^ to select a line in left direction
  • Use $ to select a line in right direction

To learn interesting and useful bash command line tricks. Read our article on 20 interesting and helpful command line tricks.

2 thoughts on “8 Interesting And Really Helpful Vim Editor Features

  1. Innocent Bystander

    Did you use VIM to write this blog? If not then can you answer:
    1. Why didn’t you use vim?
    2. How did you manage to go back and forth, add, insert, search, text correction to finalize your text?

    Reply

Leave a Reply

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