VI小抄
Move
- h j k l
- Move to file begin
gg
- Move to file end
G - Move to line 3
:3
Copy & Paste
- Copy
y
- Cut
d
- Paste
p
Search & Replace
- Search
/Apple
- Search by word
/\<Apple\>
- Search ignorecase
/Apple\c
- Replace in lines
:1,10s/Apple/Banana
- Replace in a file
:%s/Apple/Banana/g
- Count items
:%s/Apple/&/gn
- Search in files
vimgrep /Apple/ *.txt
grep -R . --include='*.cxx' -i -e 'Apple' (You can open the quickfix list by :copen)
Align several lines
- Go to first line and enter Visual Mode by CRTL-v
- Move to the last line and select the lines by SHIFT-v
- Align the lines to have 4 spaces in the left by entering :'<,'>left 4
(You may need enable ":set expandtab" to insert tab by spaces.)
Undo & Redo
- Undo
u
- Redo
CTRL-r
New Window & Split Window
- New window
:new
- Switch between windows
CTRL-w CTRL-w
CTRL-w h (or j, k, l)
- Split horizontal window
CTRL-w s
- Split vertical window
CTRL-w v - Close a window
:q - Close other windows
CTRL-w o
Display
- Display line number
:set number
- Set tab stop
:set tabstop=4
- Insert space to tab
:set expandtab (reversely, :set noexpandtab)
- Highlight cursor line and column
:set cursorline
:set cursorcolumn
- Folding with marker
:set foldenable
:set foldmethod=marker
:set foldmarker={{{,}}}
(Then you can wrap the sections with {{{ and }}} to create folders and sub-folders. You can add comments after {{{ to add the folder comment)
Close a folder by zc
Open a folder by zo
Tag
- Generate tag file
ctags --extra=+q -R .
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
- Install taglist (http://vim-taglist.sourceforge.net/installation.html)
- Show tag list
:Tlist
- Go to a tag
:ta class::member
:ta class.member
:ta /disp*
CTRL-] (go to tag)
CTRL-T (go back)
- Set Multiple Tag files
:set tags:=~/a/tags;~/b/tags
:set tags+=/my/path/to/tags
- Set Tag Files Recursively
:set tags=./tags;/.
Cscope
- Generate cscope file
find . -name '*.hxx' -print > cscope.files
find . -name '*.cxx' -print >> cscope.files
cscope -b
- Find functions calling this function
:cs find c foo
- Find a function definition and show the result in a newly splitted window
:scs find g foo
- Show cscope file (up to 8)
:cs show
- Add a cscope file
:cs add yourfile - Add the line to your ~/.vimrc to map "cscope find e" to Ctrl-\
nmap <C-\> :cs find e <C-R>=expand("<cword>")<CR><CR>
Word Completion
- C-x C-n