Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > vim Usage

vim Usage

iDog's brief & practical vim guide.

Basic Commands

commanddescription
i/Iinsert
a/Aappend
o/Oappend/insert new line
dd/ccdelete/replace curr line
dw/cwdelete/replace curr word
  
h/left arrowleft
l/right arrow 
j/upper arrow 
k/down arrow 
Hmove to top left corner of screen
Lmove to bottum left corner of screen
0/^move to BOL
$EOL
Ggo to EOF
<num>Ggo to line num
ctrl-b/PgUp 
ctrl-f/PgDn 
w/Wbeginning of next word/big word
e/Eend of next word/big word
b/Bbeginning of prev word/big word
  
yyank
p/Ppaste before/after cursor
  
</>left/right indent a selected block
<</>>left/right indent curr line

Useful commands for programmers

commanddescription
%match (), <>, [], {}, #if…
  
#go to previous occurrence of the current word (under cursor)
*go to next occurrence of the current word
  
[[go to previous ‘{‘ which is at col 1
]]go to next ‘}’ which is at col 1
  
{go to start of previous paragraph
}go to start of next paragraph
  
<ctrl>-]jump
<ctrl>-Tjump back
  
gdgo to definition
Kshow man page for the word at cursor
Ccomment out curr line with //, then move to next line

Generate tags for jumping operations

ctags [options] <filenames>

Examples:

ctags –R | create tags recursively from the current dir.
Ctags -R --exclude “/aaa/bbb” --exclude “/ccc/ddd” “/my/root/dir”

If vi is started from a dir without the tags file, it cannot load the tags. In this case, we have following solutions:

  • Make a script file, with the following commands
:set tags=<tag_file_with_path>
  • Load the script file:
vi –s <script_file>

Quick fix - a simple IDE

commanddescription
:make <param>do make (make file must exist in current dir)
:cllist errors & warnings
:cngo to next error
:cp/:cNgo to previous error
:cnewrestart from the beginning

Visual Mode

Back to the time of using a good editor in DOS...

commanddescription
vtoggle visual mode on/off
<ctrl>-vtoggle block visual mode on/off

Split windows

commanddescription
:splitsplit
:vsplitsplit vertically
<ctrl>-w w or <ctrl>-w <ctrl>-wmove around the panes
<ctrl>-w <ctrl>--max the current pane
<ctrl>-w h/j/k/lmove cursor to a certain pane
<ctrl>-w H/J/K/Lmove current pane to a direction
:closeclose current pane
:qallquit all files
:wallsave all files
:wqall 
:onlyclose all other panes
:vertical diffsplit <filename>split vertically and do diff with curr file. Tip: use tab key for file name auto-completion
> vimdiff <file1> <file2>same as above
:new 
:vertical :newvertical split and start a new file
:vnewsame as above

Other Notes

Get result of a command to the current edited file

:read !<command>

Eg:

:read !ls ~/
:read !cat ~/.bashrc

Replace

:%s/\<word\>/WORD/gc

\< indicates the beginning of word; \> end of word; c: prompt for each replace

:%s/^/my_str/g

Insert my_str at the beginning of each line.

:%s/$/my_str/g

Append my_str after each line.

Config file for vim

vimrc.txt

:set ai/noai - alignment
:set sm - show matching () [] {} while typing
:set hlsearch - hilight the search result
:set laststatus=2 - show status bar
:set guifontset=-*-fixed-medium-r-normal--16-*-*-*-c-*,*-r-*
:set backup - backup file
:set nobk - don't backup file

Also, sometimes you will find that Backspace key acts as Del key, and <Shift> - Backspace acts as the normal Backspace key. In this case, if you type <Ctrl> - Backspace & <Ctrl> - <Shift> - Backspace, and set the desired in terminal configuration (depends on which terminal software you use), it will be fixed.

Edit binary files

  1. open binary file
  2. type ':%!xxd' (without quotation marks)
  3. edit the file
  4. type ':%!xxd -r' (without quotation marks)
  5. save file