首页 > 代码库 > VIM命令集

VIM命令集

CommandActionNotes
vim file +54open file and go to line 54any : command can be run using + on command line
vim -O file1 file2open file1 and file2 side by side 
Insertenter insert modeso you can start typing. Alternatively one can use i ora.
Escleave insert modeso you can issue commands. Note in VIM the cursor keys & {Home, End, Page{up,down}} and Delete and Backspace work as expected in any mode, so you don‘t need to go back to command mode nearly as much as the origonal vi. Note even Ctrl+{left,right} jumps words like most other editors. Note also Ctrl+[ and Ctrl+c are equivalent to Esc and may be easier to type. Also Ctrl+o in insert mode will switch to normal mode for one command only and automatically switch back.
:commandruns named command 
:help wordshows help on wordTyping Ctrl+d after word shows all entries containing word
:echo &wordshows value of word 
windows
:eset buffer for current windowyou can optionally specify a new file or existing buffer number (#3 for e.g.). Note if you specify a directory a file browser is started. E.g. :e . will start the browser in the current directory (which can be changed with the :cd command).
:spnew window aboveditto
:vsnew window to leftditto
:qclose current window 
:qaclose all windowsadd trailing ! to force
Ctrl+w {left,right,up,down}move to window 
Ctrl+w Ctrl+wtoggle window focus 
Ctrl+w =autosize windowsto new terminal size for e.g.
:banew window for all buffers":vert ba" tiles windows vertically
buffers
:lslist buffers 
gfopen file under cursor 
:bddelete bufferand any associated windows
:wsave fileNote :up[date] only writes file if changes made, but it‘s more awkward to type
:sav filenamesave file as filenameNote :w filename doesn‘t switch to new file. Subsequent edits/saves happen to existing file
undo/redo
uundo 
Ctrl+rredo 
.repeat 
navigation
ggGoto start of file 
GGoto end of file 
:54Goto line 54 
80|Goto column 80 
Ctrl+gShow file infoincluding your position in the file
gaShow character infog8 shows UTF8 encoding
Ctrl+escroll upCtrl+x needed first for insert mode
Ctrl+yscroll downCtrl+x needed first for insert mode
ztscroll current line to top of window 
wGoto next wordNote Ctrl+{right} in newer vims (which work also in insert mode)
bGoto previous wordNote Ctrl+{left} in newer vims
[{Goto previous { of current scope 
%Goto matching #if #else,{},(),[],/* */must be one on line
zitoggle folds on/off 
bookmarks
m {a-z}mark position as {a-z}E.g. m a
‘ {a-z}move to position {a-z}E.g. ‘ a
‘ ‘move to previous position 
‘0open previous filehandy after starting vim
selection/whitespace
vselect visuallyuse cursor keys, home, end etc.
Shift+vline selectCTRL+v = column select
Deletecut selection 
"_xdelete selectionwithout updating the clipboard or yank buffer. I remap x to this in my .vimrc
ycopy selection 
ppaste (after cursor)P is paste before cursor
"Ayappend selected lines to register ause lowercase a to initialise register
"appaste contents of a 
gqreformat selectionjustifies text and is useful with :set textwidth=70 (80 is default)
=reindent selectionvery useful to fix indentation for c code
>indent sectionuseful with Shift+v%
<unindent sectionremember . to repeat and u to undo
:set list!toggle visible whitespaceSee also listchars in my .vimrc
clipboard shortcuts
ddcut current line 
yycopy current line 
Dcut to end of line 
y$copy to end of line 
search/replace
/regexpsearches forwards for regexp? reverses direction
nrepeat previous searchN reverses direction
*searches forward for word under cursor# reverses direction
:%s/1/2/gcsearch for regexp 1 and replace with 2 in filec = confirm change
:s/1/2/gsearch for regexp 1 and replace with 2 in (visual) selection 
programming
Klookup word under cursor in man pages2K means lookup in section 2
:makerun make in current directory 
Ctrl+]jump to tagCtrl+t to jump back levels. I map these to Alt+?? in my .vimrc
vim -t nameStart editing where name is defined 
Ctrl+{n,p}scroll forward,back through autocompletions for word before cursoruses words in current file (and included files) by default. You can change to a dictionary for e.g: set complete=k/usr/share/dicts/words Note only works in insert mode
Ctrl+x Ctrl+oscroll through language specific completions for text before cursor"Intellisense" for vim (7 & later). :help compl-omni for more info. Useful for python, css, javascript, ctags, ... Note only works in insert mode
external filters
:%!filterput whole file through filter 
:!filterput (visual) selection through filter 
:,!commandreplace current line with command output 
map <f9> :w<CR>:!python %<CR>run current file with external program 


(来源:http://www.pixelbeat.org/vim.tips.html