目录
- which vim
- Exiting
- Navigating
- Editing
- Clipboard
- Operators
- Text objects
- Find and replace
- Repeat
- Register
- Buffer
- Window
- Tab
- Dir
- Other
- Nerdtree
- OtherPlugins
which vim
$ which vim/usr/local/bin/vim
系统的vim没有系统剪贴板功能, 所以用brew
安装一个支持系统剪贴板的.
$ vim --version | grep "clipboard"+clipboard +jumplist +persistent_undo +virtualedit-ebcdic -mouseshape +statusline -xterm_clipboard
Exiting
<ESC> # to normal mode:q # quitshift z z # quit:wq / :x # save and close:qa # close all:q! # close file, abandon changes
,w # write (my Leader is ,),q # quit
Navigating
h j k l # move one character
<C-U> / <C-D> # move half page<C-F> / <C-B> # move one page
b / w # previous / next worde / ge # previous / next end of word
0 # start of line^ # start of line (after whitespace)$ # end of line
gg # first lineG # Last line:n # to line nnG # to line n
zz # center this lineH # move to top of screenM # move to middle of screenL # move to bottom of screen
Editing
a # appendA # append on taili # insertI # insert on heado # next lineO # previous lines # delete char and insertS # delete line and insertC # delete until end of line and insertr # replace one characterR # enter Replace mode
Clipboard
x # delete characterdd # delete line (cut)yy # yank line (copy)p # pasteP # paste before
Operators
d # deletey # yank (copy)c # change (delete then insert)> # indent right< # indent left= # auto indent~ # swap case for current characterg~ # swap casegU # uppercasegu # lowercase! # filter through external program
cc # delete line and to insert modedd # delete lineyy # copy lineguu/gugu # lowercase allgUU/gUgU # uppercase allg~~/g~g~ # swap case all>> # line right indent<< # line left indent== # line auto indent
Text objects
p # paragraphw # words # sentence[ ( { < # a [], (), or {} blockb # a block [(B # a block in [{t # a XML tag block' " ` # A quoted string
Find and replace
fn # find next n in this lineFn # find previous n in this linetn # till next n in this lineTn # till previous n in this line
/foo # find foo/foo\c # find foo FOO (ignore case)
?foo # find foo previous?foo\c # find foo FOO previous
n / N # next / previous one
* # find cursor word (foo -> foo but not foobar)g* # find cursor word (foo -> foo, foobar)
# # find cursor word previous
:{range}s/{old}/{new}/{flag}
:s/foo/bar/g # current line substitute:%s/foo/bar/g # all file:'<,'>s/foo/bar/g # visual mode selection:5,12s/foo/bar/g # line 5~12:.,$s/foo/bar/g # current line to end line:.,+2s/foo/bar/g # current line and next 2 lines
# flag: g(global) i(ignore case) c(need confirm)
:%s/foo/bar # replace once:%s/foo/bar/i # same as :%s/foo\c/bar:%s/foo/bar/gc # will popup confirm
# replace with bar (y/n/a/q/l/^E/^Y)?# yes no all quit line ~
:set hls # highlight search:set ic # ignore case:set is # show partial matches for a search phrase:set nu # show line numbers:set et|retab # convert tabs to spaces
:set noic:set nohls:set nois:set nonu:set et|retab!
# example
yiw # yank inner word 'first'# move cursor to 'second'viwp # select 'second' and paste with 'first'# move cursor to 'third'viw"0p # select 'third' and paste with first
yiw # yank inner word 'first'# move cursor to 'second'ciw<C-R>0<ESC> # change 'second' replace with first# move cursor to 'third'. # repeat the operation
Repeat
. # repeat last changeu # undo<C-r> # redo
; # line find repeat, # line find previous repeat
n # find repeatN # find previous repeat
& # substitute repeatu # undo
@{reg} # macro repeat
Register
There are ten types of registers:
- The unnamed register ""
- 10 numbered registers “0 to “9
- The small delete register ”-
- 26 named registers “a to “z or “A to “Z
- three read-only registers ”:, ”., ”%
- alternate buffer register ”#
- the expression register ”=
- The selection and drop registers ”*, ”+ and ”~
- The black hole register ”_
- Last search pattern register “/
:reg # show named registers and what's in them"+y # copy to system clipboard"+p # paste from system clipboardp # just paste for """0p # 0 always keep copyed
Buffer
vim file1 file2 # open:e[dit] file3 # open
:ls, :buffers # list buffers:bn[ext] # list next buffer:bp[revious] # list previous buffer:b {number, expression} # jump to buffer
:sb 3 # split window to open buffer 3:vertical sb 3 # split vertical window to open buffer 3
Window
vim -O file1 file2
:sp[lit] {file} # horizontal split:new {file} # horizontal split:vs[plit] {file} # vertical split:clo[se] # close current window
Ctrl+w s # split current windowCtrl+w v # split vertical current windowCtrl+w q # close current windowCtrl+w n # open new empty windowCtrl+w o # close all other window
Ctrl+w h # left windowCtrl+w j # bottom windowCtrl+w k # up windowCtrl+w l # right windowCtrl+w w # traverse windows
Ctrl+w H # move left windowCtrl+w J # move bottom windowCtrl+w K # move up windowCtrl+w L # move right window
Ctrl+w + # increase heightCtrl+w - # decrement heightCtrl+w = # set same height
Tab
vim -p file1 file2 file3 # open in tabs
:tabe[dit] {file} # edit specified file in a new tab:tabc[lose] # close current tab:tabc[lose] {i} # close i-th tab:tabo[nly] # close all other tabs (show only the current tab)
:tabn # go to next tab:tabp # go to previous tab:tabfirst # go to first tab:tablast # go to last tab
gt # go to next tabgT # go to previous tab{i}gt # go to tab in position i
Dir
:echo @% # src/file1:echo expand('%:t') # file1:echo expand('%:p') # /home/FaiChou/Desktop/Project/A/src/file1:echo expand('%:p:h') # /home/FaiChou/Desktop/Project/A/src:echo expand('%:p:h:t') # src
# p(path) h(head) t(tail)
:w %.bak # backup current to current.bak:e %:p:h/main.h # open current path's main.h"%p # insert current path
:!ls # bash ls:!rm foo.txt # bash rm
Other
When writing in insert mode, it’s possible to paste a register at the current location without leaving insert mode. To do this, press
CTRL-R
then type the name of a register. For example,CTRL-R
a will insert the contents ofa
.
ggvGy # copy allggyG # copy all:%y # copy all
dt" # delete till next "di( # delete inner ()da( # delete a ()
yi" # copy inner ""ya" # copy a "" (include "")yaw # copy a wordyas # copy a sentenseyap # copy a paragraph
ds" # delete surround ""cs"' # change surrond " to '
:.,+9s/new/<C-R>0/g # replace new to regster0 from current to next 9 lines
:2,4j # join line 2-4
# insert mode shortcuts<C-W> # delete before cursor words<C-[> # quit insert mode<C-C> # quit insert mode<C-R> 0 # insert copied<C-N> n n n # auto complete<C-P> p p p # auto complete<C-J> # new line<C-M> # new line
# Leaderlet mapLeader = ","nnoremap <leader>w :w<CR>nnoremap <leader>q :q<CR>
# number plus and subtract
<C-a> # add 1<C-x> # minus 110<C-a> # add 10
# macro
1. abc # cursor in 1, need insert zzz to 22. def3. ghi4. jkl
qa # start record, to register aj # remove to next line<C-a> # add 1q # quit record3@a # do macro 6 times
:'<,'>normal @a # macro in visual
# match block% # match ([{}])
Nerdtree
?: toggle help
# Fileo: open in prev windowgo: previewt: open in new tabT: open in new tab silentlyi: open splitgi: preview split
# Directory
o: open & close nodeO: recursively open nodet: open in new tabT: open in new tab silentlyx: close parent of nodeX: close all child nodes of current node recursively
# Filesystem
u: move tree root up a dirU: move tree root up a dir but leave old root openr: refresh cursor dirR: refresh current rootm: Show menu
# Tree navigation
P: go to rootp: go to parentK: go to first childJ: go to last child
# Other
q: Close the NERDTree windowA: Zoom (maximize-minimize)
OtherPlugins
# rename.vim:rename abc.js
# vim-mkdir:e a/b/c/d.js (where you open vim)
# ctrlp<c-d> toggle file or path search<c-j>/<c-k>,<up>/<down> switch<enter>/<c-t> open / open in tab