从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为 “每个程序员都应该知道的 100 个 vim 命令” 这次算是之前那篇文章的改进版,希望你会喜欢。基础[table=98%]
[tr][td]:e filename[/td][td]Open filename for edition[/td][/tr]
[tr][td]:w[/td][td]Save file[/td][/tr]
[tr][td]:q[/td][td]Exit Vim[/td][/tr]
[tr][td]:q![/td][td]Quit without saving[/td][/tr]
[tr][td]:x[/td][td]Write file (if changes has been made) and exit[/td][/tr]
[tr][td]:sav filename[/td][td]Saves file as filename[/td][/tr]
[tr][td].[/td][td]Repeats the last change made in normal mode[/td][/tr]
[tr][td]5.[/td][td]Repeats 5 times the last change made in normal mode[/td][/tr]
[/table]在文件中移动[table=98%]
[tr][td]k or Up Arrow[/td][td]move the cursor up one line[/td][/tr]
[tr][td]j or Down Arrow[/td][td]move the cursor down one line[/td][/tr]
[tr][td]e[/td][td]move the cursor to the end of the word[/td][/tr]
[tr][td]b[/td][td]move the cursor to the begining of the word[/td][/tr]
[tr][td]0[/td][td]move the cursor to the begining of the line[/td][/tr]
[tr][td]G[/td][td]move the cursor to the end of the line[/td][/tr]
[tr][td]gg[/td][td]move the cursor to the begining of the file[/td][/tr]
[tr][td]L[/td][td]move the cursor to the end of the file[/td][/tr]
[tr][td]:59[/td][td]move cursor to line 59. Replace 59 by the desired line number.[/td][/tr]
[tr][td]20|[/td][td]move cursor to column 20.[/td][/tr]
[tr][td]%[/td][td]Move cursor to matching parenthesis[/td][/tr]
[tr][td][[[/td][td]Jump to function start[/td][/tr]
[tr][td][{[/td][td]Jump to block start[/td][/tr]
[/table]剪切、复制和粘贴[table=98%]
[tr][td]y[/td][td]Copy the selected text to clipboard[/td][/tr]
[tr][td]p[/td][td]Paste clipboard contents[/td][/tr]
[tr][td]dd[/td][td]Cut current line[/td][/tr]
[tr][td]yy[/td][td]Copy current line[/td][/tr]
[tr][td]y$[/td][td]Copy to end of line[/td][/tr]
[tr][td]D[/td][td]Cut to end of line[/td][/tr]
[/table]搜索[table=98%]
[tr][td]/word[/td][td]Search word from top to bottom[/td][/tr]
[tr][td]?word[/td][td]Search word from bottom to top[/td][/tr]
[tr][td][/td][td]Search the word under cursor[/td][/tr]
[tr][td]/\cstring[/td][td]Search STRING or string, case insensitive[/td][/tr]
[tr][td]/jo[ha]n[/td][td]Search john or joan[/td][/tr]
[tr][td]/< the[/td][td]Search the, theatre or then[/td][/tr]
[tr][td]/the>[/td][td]Search the or breathe[/td][/tr]
[tr][td]/< the>[/td][td]Search the[/td][/tr]
[tr][td]/< |.>[/td][td]Search all words of 4 letters[/td][/tr]
[tr][td]//[/td][td]Search fred but not alfred or frederick[/td][/tr]
[tr][td]/fred|joe[/td][td]Search fred or joe[/td][/tr]
[tr][td]/<\d\d\d\d>[/td][td]Search exactly 4 digits[/td][/tr]
[tr][td]/^\n{3}[/td][td]Find 3 empty lines[/td][/tr]
[tr][td]:bufdo /searchstr/[/td][td]Search in all open files[/td][/tr]
[tr][td]bufdo %s/something/somethingelse/g[/td][td]Search something in all the open buffers and replace it with somethingelse[/td][/tr]
[/table]替换[table=98%]
[tr][td]:%s/old/new/g[/td][td]Replace all occurences of old by new in file[/td][/tr]
[tr][td]:%s/onward/forward/gi[/td][td]Replace onward by forward, case unsensitive[/td][/tr]
[tr][td]:%s/old/new/gc[/td][td]Replace all occurences with confirmation[/td][/tr]
[tr][td]:2,35s/old/new/g[/td][td]Replace all occurences between lines 2 and 35[/td][/tr]
[tr][td]:5,$s/old/new/g[/td][td]Replace all occurences from line 5 to EOF[/td][/tr]
[tr][td]:%s/^/hello/g[/td][td]Replace the begining of each line by hello[/td][/tr]
[tr][td]:%s/$/Harry/g[/td][td]Replace the end of each line by Harry[/td][/tr]
[tr][td]:%s/onward/forward/gi[/td][td]Replace onward by forward, case unsensitive[/td][/tr]
[tr][td]:%s/ $//g[/td][td]Delete all white spaces[/td][/tr]
[tr][td]:g/string/d[/td][td]Delete all lines containing string[/td][/tr]
[tr][td]:v/string/d[/td][td]Delete all lines containing which didn’t contain string[/td][/tr]
[tr][td]:s/Bill/Steve/[/td][td]Replace the first occurence of Bill by Steve in current line[/td][/tr]
[tr][td]:s/Bill/Steve/g[/td][td]Replace Bill by Steve in current line[/td][/tr]
[tr][td]:%s/Bill/Steve/g[/td][td]Replace Bill by Steve in all the file[/td][/tr]
[tr][td]:%s/^M//g[/td][td]Delete DOS carriage returns (^M)[/td][/tr]
[tr][td]:%s/\r/\r/g[/td][td]Transform DOS carriage returns in returns[/td][/tr]
[tr][td]:%s#<[^>]+>##g[/td][td]Delete HTML tags but keeps text[/td][/tr]
[tr][td]:%s/^(.)\n\1$/\1/[/td][td]Delete lines which appears twice[/td][/tr]
[tr][td]Ctrl+a[/td][td]Increment number under the cursor[/td][/tr]
[tr][td]Ctrl+x[/td][td]Decrement number under cursor[/td][/tr]
[tr][td]ggVGg?[/td][td]Change text to Rot13[/td][/tr]
[/table]大小写[table=98%]
[tr][td]Vu[/td][td]Lowercase line[/td][/tr]
[tr][td]VU[/td][td]Uppercase line[/td][/tr]
[tr][td]g~~[/td][td]Invert case[/td][/tr]
[tr][td]vEU[/td][td]Switch word to uppercase[/td][/tr]
[tr][td]vE~[/td][td]Modify word case[/td][/tr]
[tr][td]ggguG[/td][td]Set all text to lowercase[/td][/tr]
[tr][td]gggUG[/td][td]Set all text to uppercase[/td][/tr]
[tr][td]:set ignorecase[/td][td]Ignore case in searches[/td][/tr]
[tr][td]:set smartcase[/td][td]Ignore case in searches excepted if an uppercase letter is used[/td][/tr]
[tr][td]:%s/<./\u&/g[/td][td]Sets first letter of each word to uppercase[/td][/tr]
[tr][td]:%s/<./\l&/g[/td][td]Sets first letter of each word to lowercase[/td][/tr]
[tr][td]:%s/./\u&[/td][td]Sets first letter of each line to uppercase[/td][/tr]
[tr][td]:%s/.*/\l&[/td][td]Sets first letter of each line to lowercase[/td][/tr]
[/table]读写文件[table=98%]
[tr][td]:1,10 w outfile[/td][td]Saves lines 1 to 10 in outfile[/td][/tr]
[tr][td]:1,10 w >> outfile[/td][td]Appends lines 1 to 10 to outfile[/td][/tr]
[tr][td]:r infile[/td][td]Insert the content of infile[/td][/tr]
[tr][td]:23r infile[/td][td]Insert the content of infile under line 23[/td][/tr]
[/table]文件浏览器[table=98%]
[tr][td]:e .[/td][td]Open integrated file explorer[/td][/tr]
[tr][td]:Sex[/td][td]Split window and open integrated file explorer[/td][/tr]
[tr][td]:Sex![/td][td]Same as :Sex but split window vertically[/td][/tr]
[tr][td]:browse e[/td][td]Graphical file explorer[/td][/tr]
[tr][td]:ls[/td][td]List buffers[/td][/tr]
[tr][td]:cd …[/td][td]Move to parent directory[/td][/tr]
[tr][td]:args[/td][td]List files[/td][/tr]
[tr][td]:args *.php[/td][td]Open file list[/td][/tr]
[tr][td]:grep expression *.php[/td][td]Returns a list of .php files contening expression[/td][/tr]
[tr][td]gf[/td][td]Open file name under cursor[/td][/tr]
[/table]和 Unix 系统交互[table=98%]
[tr][td]:!pwd[/td][td]Execute the pwd unix command, then returns to Vi[/td][/tr]
[tr][td]!!pwd[/td][td]Execute the pwd unix command and insert output in file[/td][/tr]
[tr][td]:sh[/td][td]Temporary returns to Unix[/td][/tr]
[tr][td]$exit[/td][td]Retourns to Vi[/td][/tr]
[/table]对齐[table=98%]
[tr][td]:%!fmt[/td][td]Align all lines[/td][/tr]
[tr][td]!}fmt[/td][td]Align all lines at the current position[/td][/tr]
[tr][td]5!!fmt[/td][td]Align the next 5 lines[/td][/tr]
[/table]Tabs/Windows[table=98%]
[tr][td]:tabnew[/td][td]Creates a new tab[/td][/tr]
[tr][td]gt[/td][td]Show next tab[/td][/tr]
[tr][td]:tabfirst[/td][td]Show first tab[/td][/tr]
[tr][td]:tablast[/td][td]Show last tab[/td][/tr]
[tr][td]:tabm n(position)[/td][td]Rearrange tabs[/td][/tr]
[tr][td]:tabdo %s/foo/bar/g[/td][td]Execute a command in all tabs[/td][/tr]
[tr][td]:tab ball[/td][td]Puts all open files in tabs[/td][/tr]
[tr][td]:new abc.txt[/td][td]Edit abc.txt in new window[/td][/tr]
[/table]分屏显示[table=98%]
[tr][td]:e filename[/td][td]Edit filename in current window[/td][/tr]
[tr][td]:split filename[/td][td]Split the window and open filename[/td][/tr]
[tr][td]ctrl-w up arrow[/td][td]Puts cursor in top window[/td][/tr]
[tr][td]ctrl-w ctrl-w[/td][td]Puts cursor in next window[/td][/tr]
[tr][td]ctrl-w_[/td][td]Maximize current window vertically[/td][/tr]
[tr][td]ctrl-w|[/td][td]Maximize current window horizontally[/td][/tr]
[tr][td]ctrl-w=[/td][td]Gives the same size to all windows[/td][/tr]
[tr][td]10 ctrl-w+[/td][td]Add 10 lines to current window[/td][/tr]
[tr][td]:vsplit file[/td][td]Split window vertically[/td][/tr]
[tr][td]:sview file[/td][td]Same as :split in readonly mode[/td][/tr]
[tr][td]:hide[/td][td]Close current window[/td][/tr]
[tr][td]:-nly[/td][td]Close all windows, excepted current[/td][/tr]
[tr][td]:b 2[/td][td]Open #2 in this window[/td][/tr]
[/table]自动完成[table=98%]
[tr][td]Ctrl+n Ctrl+p (in insert mode)[/td][td]Complete word[/td][/tr]
[tr][td]Ctrl+x Ctrl+l[/td][td]Complete line[/td][/tr]
[tr][td]:set dictionary=dict[/td][td]Define dict as a dictionnary[/td][/tr]
[tr][td]Ctrl+x Ctrl+k[/td][td]Complete with dictionnary[/td][/tr]
[/table]Marks[table=98%]
[tr][td]m {a-z}[/td][td]Marks current position as {a-z}[/td][/tr]
[tr][td]’ {a-z}[/td][td]Move to position {a-z}[/td][/tr]
[tr][td]‘’[/td][td]Move to previous position[/td][/tr]
[/table]缩写[table=98%]
[tr][td]:ab mail mail@provider.org[/td][td]Define mail as abbreviation of mail@provider.org[/td][/tr]
[/table]文本缩进[table=98%]
[tr][td]:set autoindent[/td][td]Turn on auto-indent[/td][/tr]
[tr][td]:set smartindent[/td][td]Turn on intelligent auto-indent[/td][/tr]
[tr][td]:set shiftwidth=4[/td][td]Defines 4 spaces as indent size[/td][/tr]
[tr][td]ctrl-t, ctrl-d[/td][td]Indent/un-indent in insert mode[/td][/tr]
[tr][td]>>[/td][td]Indent[/td][/tr]
[tr][td]<<[/td][td]Un-indent[/td][/tr]
[tr][td]=%[/td][td]Indent the code between parenthesis[/td][/tr]
[tr][td]1GVG=[/td][td]Indent the whole file[/td][/tr]
[/table]语法高亮[table=98%]
[tr][td]:syntax on[/td][td]Turn on syntax highlighting[/td][/tr]
[tr][td]:syntax off[/td][td]Turn off syntax highlighting[/td][/tr]
[tr][td]:set syntax=perl[/td][td]Force syntax highlighting[/td][/tr]
[/table]
via catswhocode
不了解,就会几个!也不想记,还是图形化的软件好用!
大多数不会:6_127: