The Complete Magazine on Open Source

Eleven Features of Vim That You Probably Aren’t Using

, / 125 0

Vim opninig images

For developers and other users of Vim, here’s a list of useful features and commands of Vim editor for same.

If you have a basic knowledge of Vim and are interested in exploring some advanced ways to use it, but think you may be missing some essential features of the GUI-based editor, this article is for you.
1. Word suggestion
Sometimes, when writing programs, you do need suggestions of variable/function names from the IDE. This is possible with Vim. In insert mode, type the first couple of characters of a word, then press:

  • Ctrl-N to insert the next matching word
  • Ctrl-P to insert the previous matching word

For a programmer, this is very useful when entering the names of variables in a program.
Incidentally, if you really want CTRL-P or CTRL-N to scan a dictionary file, then try :set dictionary=/usr/. Multiple dictionaries can be used as well.


Figure 1 : Completes any word

2. Find the next or previous occurrence of the word under the cursor
Sometimes, it’s useful to find the next or previous occurrence of a word in your file. This is possible with Vim. In command mode, hit ‘#’ or ‘*’ to find the previous or next occurrence of the word that is under the cursor.
3. Undo recent changes
To undo changes, the ‘u’ command is used. In command mode, hitting ‘u’ will undo the last change.
4. The % key
This is a really awesome feature for programmers. The % key can be used for the following:

  • To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ( [ { }] )
  • To jump to the start or end of a C-style comment: /* */.
  • To jump to a matching C/C++ pre-processor conditional: #if, #ifdef, #else, #elif, #endif.
    In command mode, press ‘%’ to find the matching brace/comment that is under the cursor.

Figure 2 : Horizontal split

5. Repeat the previous action
The ‘.’ command repeats the last change made in normal mode. For example, if you press dw to delete a word, you can then press ‘.’ to delete another word.
6. Open multiple windows in Vim
It is a basic requirement for any programmer to open multiple files concurrently. Vim supports this too.
Vim has the ability to split its window into multiple panes using the :sp or :vsp commands.
To open a different file in a newly split window, you can specify the filename as part of the command. For a horizontal split, use :sp <filename> and for a vertical split, use :vsp <filename>.
To move the cursor from one window to another, use TRL+w CTRL+w in normal mode. This will change the active file in a round robin manner. CTRL+w <arrow key> is also used to move the cursor from one window to another.


Figure 3 : Vertical Split

7. Open the same file in a split window
Sometimes, there is a need to open the same file in another split window. To do this in the normal mode, use CTRL+W v and CTRL+w s to open the same file in a vertical and horizontal split of the file that is open.
8. Set abbreviations
Abbreviations are just short cuts that you type, which expand to the full, normal version of themselves. Vim provides the flexibility to define new abbreviations. In normal mode, use the command ab to set new abbreviations.
To define a new abbreviation, type:

:ab def #define

When you type the abbreviation in Vim, as soon as you hit the space bar, it will be expanded to the full text form. So, by typing ‘def’ in the file, Vim will expand this to ‘#define’, which can make a programmer’s life easier.
9. Open a file under the cursor
Often, a file contains the name of a second file, and you would like to open that second file. Here, the ‘gf’ command can be of great help. On using it, Vim will recognise the file name and find the same file name in the path. When the cursor is on a local variable or function, this command will jump to its declaration. To return to the previous file/cursor location, use CTRL+o.
10. Execute the terminal command from Vim
While using Vim, you sometimes feel like you need to close the file (Vim) and execute commands in a terminal. So let me tell you how to execute commands inside Vim without closing the file.
Let’s assume you are inside the Vim editor and it has opened somefile.
Press ESC and type :! followed by the command. The output of the command will be listed in the terminal and after pressing Enter, you will be redirected to the text editor.
Figure 4 shows the syntax for executing the ls command on the shell through Vim.


Figure 4 : Execute shell commands without closing Vim

11. View files in hex mode
Many firmware developers need to view a file in hex mode. Vim comes with a great feature to switch to hex mode when editing a file. To enter into hex mode, open a file in vi as usual, hit Escape and type :%!xxd to switch to hex mode.
To come out of hex mode, hit Escape again and type :%!xxd -r