Vim Shortcuts for Developers

Most of us like to work in multiple files simultaneously. For navigating through the different files in a project VIM provide us with multiple features.The split window command is one of them. Lets talk about it.

 Navigation

Command Description
gg Beginning of file
G End of file
Ctrl+g Show file info
Shift+v line select
:%s/1/2/gc search for regexp 1 and replace with 2 in file
:s/1/2/g search for regexp 1 and replace with 2 in (visual) selection
:sp new window above
u undo
Ctrl+r redo
* searches forward for word under cursor

Copy and Paste in visual mode

1. Select the portion of text you want to copy:

Place the cursor at the beginning of the text portion

Esc v

scroll down till the last of the text you want to copy#

2. Do the actual copy

Press y(yank)

3. Now place the cursor where you want to paste the text

Press Esc p

Vim Split Window Commands


:sp

Syntax

:sp

filename -> open filename in the new view port.

Variants:

1. To move directly to the first instance of a keyword:

:sp +/

searchstringfilename

2.To change the size(number of lines) of a view port:

:10 spfilename

Syntax:number of line you want sp

3.To assign an equal number of lines to each viewport.

Ctrl-w =

4.To move between the viewports :

Ctrl-w jto move down

Ctrl-w kto move up.

5.To open a new file and diff it with the current one:

:vimsplit {filename}

To do the same, but split vertically:

:very vimpslit {filename}

6. To split the window vertically:

:Ctrl-W V

7.To move Viewports:

ctrl-w J (to move current viewport down)

ctrl-w K (up)

To split the Vim window horizontally: :sp To split the Vim window vertically: :vsp To move between Vim viewports: Ctrl-w Ctrl-w To move one viewport down.(you can also use up/down arrow) Ctrl-w j Ctrl-w k moves one viewport up. Ctrl-w h moves one viewport to the left. Ctrl-w l moves one viewport to the right. Ctrl-w = tells Vim to resize viewports to be of equal size Ctrl-w – reduce active viewport by one line. Ctrl-w + increase active viewport by one line. Ctrl-w q will close the active window. Ctrl-w r will rotate windows to the right. Ctrl-w R will rotate windows to the left.


VIM with cscope

Go to the root directory where your source code is kept.
$cscope -Rbq. This will create a cscope out file called cscope.out.
Now open a file with VIM and do the following

:cs add cscope.out. This will bind the cscope out file with VIM. e.g. :cs add ~/cscope.out
Now do :cs to see what are the items search you can do with cscope. It should look like below:

cscope commands: add : Add a new database (Usage: add file|dir [pre-path] [flags]) find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name)


c: Find functions calling this function

d: Find functions called by this function

e: Find this egrep pattern

f: Find this file

g: Find this definition

i: Find files #including this file

s: Find this C symbol

t: Find assignments to

help : Show this message (Usage: help)

kill : Kill a connection (Usage: kill #)

reset: Reinit all connections (Usage: reset)

show : Show connections (Usage: show)

e.g.

:cs show

# pid    database name                       prepend path
0 15504  /home/NDS-UK/bhadrav/cscope.out

:cs kill 0

:cs add ~/cscope.out


Syntax

:csope find search-type search-string:cs f search-type search-string

As you can see from the above list of options, you can do a lots of different searches with VIM.

Examples

search the functions calling the function called abc : cs f c abc. Where cs stands for cscope, f is for find, c is to indicate that we are interested in the functions which are calling abc, and the last the string, i.e. the name of the function of interest.
search the reference of the symbol xyz : cs f s xyz
Find the e grep pattern “audiodecoder->dec_started” : cs f e audiodecoder->dec_started
Find this definition – cal_splice_audio_esfilter_detach : cs f g cal_splice_audio_esfilter_detach
Find this text string cal_splice_audio_esfilter_detach – :cs f t cal_splice_audio_esfilter_detach
Find this file cdi_tuner.c – : cs f f cdi_tuner.c
Find this C symbol decoder – : cs f s decoder

To see the help in cscope:

:help cscope-howtouse

Example:

find . -name *.cc > cscope.files find . -name *.c >> cscope.files find . -name *.hh >> cscope.files find . -name *.h >> cscope.files cscope -b -q -R ctags -R *

For script to setup you cscope database please check here:

Setup cscope for your project

vimgrep Command

  • Suppose you have to search the SET_PORT_PARAM string in the include folder which contains the header files and you are only interested searching the string in the header files. So give the following command:
:vimgrep SET_PORT_PARAM ./include/*.h
  • To search a function/string named CreateNetDev (in src folder and in c files only) you can also issue the following command:

:vimgrep /CreateNetDev/g ./src/*.c3.

  • To see the search results in a separate window:

:cw

Interactive Find and Replace in Vim Editor:

:%s/awesome/wonderful/gc You can perform interactive find and replace using the ‘c’ flag in the substitute, which will ask for confirmation to do substitution or to skip it as explained below. In this example, Vim editor will do a global find the word ‘awesome’ and replace it with ‘wonderful’.

But it will do the replacement only based on your input as explained below. Substitute either word1(good) or word2(nice) with a new word(awesome) using regular expression :%s/(good|nice)/awesome/gc

Vim with ctags

Get inside the root directory of the source code and do


$ ctags -R *.


This will create a tag file in the root the directory from where you are typing this command. Open a file you want to work with with absolute path from the root directory like vim ballistro/abc/graphics/xyz.c.

To check a symbol reference press : Esc- Ctrl+ ] and to to go back type Esc- Ctrl+ t If you move any other directory(other than the directory where you created your tag file then you have to specify the location of the tag file, i.e.

:se tags=../../../tags

Vim with Bookmarks

Go to the point which you want to book mark. Do Esc ma. Here the book mark is a. When at some other point you want to get back to book mark a, do Esc ‘a. Book marks with small alphabets are local to the file. So if you do Esc ‘a from any other file VIM would not understand it as it is local to the file you applied on. For global book mark use capital alphabets. Like Esc mA. Now A is your global book mark. So next time you do a Esc ‘A from any other file than where you applied the book mark, VIM will close the current file and open the file where your book mark is and place you where you had set your book mark.

Miscellaneous Tips

To display the line numbers in the source file

:se nu

To disable the line numbers display

:se nonu