The vim Features You Probably Aren’t Usingm

Today i gonna show you some of beautiful features which will enhance programmers efficiency.  If you know basic usage of vim but think you may be missing some of essential features of GUI based editor in vim, these page is for you.

1. Word Suggestion

Writing programs some times you need suggestion of variable/function name from IDE. This is possible from 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.

As being programmer, this is  very useful when you are  entering the names of variables in a program.

Any word completion

Any word completion

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.

2. Find next/previous occurrence of word under cursor

Some times it is very useful if you can find next or previous occurrence of word in your file. This is possible from vim. In command mode, hit ‘#’ or ‘*’. In command mode, press ‘*’ or ‘#’  to find previous or next occurrence of the word which is under cursor.

 3. Undo  recent changes

To undo the changes ‘u’ command is used. In command mode, hitting ‘u’ will undo most last change.

4. % key

This is 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 start or end of a C-style comment: /* */.
  • To jump to a matching C/C++ preprocessor conditional: #if, #ifdef, #else, #elif, #endif.

In command mode, press ‘%’ to find the matching brace/comment/preprocessed of brace which is under cursor.

5. Repeat 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 of any programmer if to open multiple file concurrently. Vim supports this too.

Vim’s ability to split its window into multiple panes using the “:sp" or “:vsp" commands.

To open a different file in a new split you can specify the filename as part of the command. For Horizontal split “:sp <filename>” is used and for vertical split “:vsp <filename>” is used.

Horizontal split

Horizontal split

Vertical split

Vertical split

To move cursor from one window to another window “CTRL+w CTRL+w” is used in normal mode.

This will change active file in round robin manner.  “CTRL+w <arrow key>” is also used to move the

cursor from one window to another window.

7. Open same file in split window

Some time there are certain need to to open same file in another split window. to do this in normal mode “CTRL+W v” and “CTRL+w s” is used to open same file in vertical and horizontal split of presently opened file.

8.  Set abbreviation

Abbreviations are just short cuts, you type which expands to normal version of them self. Vim provides a flexibility to defile new abbreviations. In normal mode, command “ab” is used to set new abbreviations.

To defile new abbreviation:

:ab def #define

Now, when you type the abbreviation in vim, as soon as  you hit space bar it will be expanded to the fill text.So by the typing of “def” in file, vim will expand this to “#define”, which can makes programmers life easier.

9. Open file under cursor

Often a file contains the name of a second file, and you would like to open the second file. In this condition,  “gf” command can help you a lot. On the “gf”command, vim will recognize file name and find same file name in path. When the cursor is on a local variable or function, this command will jump to its declaration. To retrun to previous file/cursor location CTRL+o is used.

10. Execute terminal command from vim

While using vim sometimes you will feel, you need to close the file(vim) and execute commands in terminal.In this post I will tell how to  execute commands inside vim without closing the file.

Now you are inside the vim editor has opened somefile.

Press ESC: and type ! command ,Output of the command will be listed in terminal and after pressing the enter again you will be redirected to text editor.


Execute shell commands without closing vim

Execute shell commands without closing vim

The above image shows syntax fro executing ls command on shell through vim.

11. View file in hex mode

Many firmware developer needs to view file in hex mode. vim gives a great feature to switch to hex mode when editing a file.

To enter in to hex mode, open a file in vi as usual, hit escape and type :%!xxd to switch into hex mode

To come out of hex mode hit escape again and type :%!xxd -r to exit from hex mode.

Feel Free to ask if you have any doubt !


Leave a comment

Filed under vim

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s