vim Usage
iDog's brief & practical vim guide.
Basic Commands
command | description |
i/I | insert |
a/A | append |
o/O | append/insert new line |
dd/cc | delete/replace curr line |
dw/cw | delete/replace curr word |
| |
h/left arrow | left |
l/right arrow | |
j/upper arrow | |
k/down arrow | |
H | move to top left corner of screen |
L | move to bottum left corner of screen |
0/^ | move to BOL |
$ | EOL |
G | go to EOF |
<num>G | go to line num |
ctrl-b/PgUp | |
ctrl-f/PgDn | |
w/W | beginning of next word/big word |
e/E | end of next word/big word |
b/B | beginning of prev word/big word |
| |
y | yank |
p/P | paste before/after cursor |
| |
</> | left/right indent a selected block |
<</>> | left/right indent curr line |
Useful commands for programmers
command | description |
% | 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>-T | jump back |
| |
gd | go to definition |
K | show man page for the word at cursor |
C | comment 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>
vi –s <script_file>
Quick fix - a simple IDE
command | description |
:make <param> | do make (make file must exist in current dir) |
:cl | list errors & warnings |
:cn | go to next error |
:cp/:cN | go to previous error |
:cnew | restart from the beginning |
Visual Mode
Back to the time of using a good editor in DOS...
command | description |
v | toggle visual mode on/off |
<ctrl>-v | toggle block visual mode on/off |
Split windows
command | description |
:split | split |
:vsplit | split vertically |
<ctrl>-w w or <ctrl>-w <ctrl>-w | move around the panes |
<ctrl>-w <ctrl>-- | max the current pane |
<ctrl>-w h/j/k/l | move cursor to a certain pane |
<ctrl>-w H/J/K/L | move current pane to a direction |
:close | close current pane |
:qall | quit all files |
:wall | save all files |
:wqall | |
:only | close 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 :new | vertical split and start a new file |
:vnew | same 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
- open binary file
- type ':%!xxd' (without quotation marks)
- edit the file
- type ':%!xxd -r' (without quotation marks)
- save file