The drive for this article is mainly my developer teammates who keep struggling to edit and browse code in Linux environment, everybody wants a Linux version of Source Insight but unfortunately it's not there. So in this article I'm gonna key in some basic commands that I typically use during my code analysis / development / browsing. Of course there are innumerable tutorials on the net to browse through, this one's limited to commands that I use...
Code browsing:
For effective code browsing, I use combination of vim, ctags and cscope. Goto the top level source tree and execute the following commands to create the symbol database.
$ cd kernel
$ ctags -RV * && ocs
The above commands take a while depending on the size of your project and create 3 files: tags (contain the tags which vim will use to jump to), cscope.out and cscope.tmpls (these two files will contain cross references to symbols which cscope will use to find a particular symbol) which you don't need to worry about. It'll be used by vim and cscope and anyway cleaned during clean build. I usually use two terminals when I'm browsing the code, one always runs cscope and the other vim.
To open a particular file do this from the directory where you created the tag file.
$ vim arch/arm/mach-omap2/control.c
ctrl+] : Jump to definition of a particular function or a macro or a symbol, place the cursor on that press.
:tsel : If you have multiple definitions of the same symbol, you this command to choose the symbol of your interest.
ctrl+t : Will navigate you back to the place where you jumped to a particular symbol
gf : If you want to jump to a particular file (e.g. a header file), place the cursor on that line and press gf to switch to that file.
ctrl+6 : Will return you back to the file from where you jumped to using gf.
:buffers : If you have made multiple jumps using gf, this command will list you all the buffers (locations).
:buf n : With the list of buffers from the above command, you can jump to a particular buffer (location) using this command. n is the number which the above command displays.
/string : Forward search a string
?string : Reverse search a string
n/N : Find next (find again) in the forward/reverse direction
* : When you keep the cursor on a particular word and press asterisk (*) it finds the next instance of the string.
# : When you keep the cursor on a particular word and press hash (#) it finds the previous instance of the string.
% : Match braces and also #if and corresponding #else or #endif
:g/ : This is a very useful command, it lists all the lines containing string in the entire file.
:g/string/d : Deletes all the lines containing string in the entire file.
:g/string/t$ : Copies all the lines containing string to the end of file
ctrl+v : You can select column using this option, yet another very useful command.
yy : Yank line (copy line)
dd : Delete (cut) line
ndd : Delete n lines
x : Delete (cut) a letter
nx : Delete (cut) n letters
p : Paste
yl : Yank a letter
ynl : Yank n letters
yw/dw: Yank/delete word on right
yb/db : Yank/delete word on left
ynw/dnw : Yank/delete n words on right
ynb/dnb : Yank/delete n words on left
. : Repeat the recent command
ctrl+f : Page down
ctrl+b : Page up
u : Undo
ctrl+r : Redo
J : Join two lines
^ : Goto the beginning of the line
$ : Goto end of line
gg : Go to the beginning of the file
G : Go to the end of file
H : Go to the first line of the page
L : Go to the last line of the page
M : Go to the middle line of the page
o/O : Insert new line below/above the current line and enter edit mode
A : Edit at the end of the line
I : Edit at the beginning of the line
C : Delete from the cursor location to end of line and enter insert mode
S : Delete the current line and enter edit mode
d$ : Same as C but will not enter insert mode
split : Splits the file in two windows horizontally
vsplit : Splits the file in two windows vertically
! : Execute a shell command without quitting vim
My .vimrc file (which usually resides in home directory and contains all the vim settings looks like this)
set nocompatible
set autowrite
set backspace=2
set backup
set backupdir=~/.VIM,~/tmp
set cindent
set filetype=make
set hlsearch
set history=50
set ignorecase
set incsearch
set laststatus=2
set matchpairs=(:),{:},[:],<:>
set matchtime=7
set ruler
set shiftwidth=8
set softtabstop=8
set showfulltag
set showmatch
set smarttab
set smartcase
set smartindent
set syntax=make
set title
set viminfo='50,\"50,%,n~/VIM/viminfo
set visualbell
set whichwrap=b,s,h,l<,>,[,]
set wildmenu
set nowrap
set notitle
set background=dark
set background=light
set ic
set nu
Cscope or the ocs does not need any description, as such. Run ocs, look for the c symbol you want to find in the entire workspace and you can navigate to each of them.