超过130个你需要了解的vim命令

来源:互联网 发布:淘宝中石油7.3折加油卡 编辑:程序博客网 时间:2024/05/22 00:52

http://developer.51cto.com/art/201308/406941.htm

 

 

1970年开始,vi vim就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为每个程序员都应该知道的 100 vim命令”这次算是之前那篇文章的改进版,希望你会喜欢。

 

 

 

基础

 

:efilename        Open filename foredition

:w        Savefile

:q        ExitVim

:q!        Quitwithout saving

:x        Writefile (if changes has been made) and exit

:savfilename        Saves file as filename

.        Repeatsthe last change made in normal mode

5.        Repeats5 times the last change made in normal mode

在文件中移动

 

k or UpArrow        move the cursor up oneline

j or DownArrow        move the cursor down oneline

e        movethe cursor to the end of the word

b        movethe cursor to the begining of the word

0        movethe cursor to the begining of the line

G        movethe cursor to the end of the line

gg        movethe cursor to the begining of the file

L        movethe cursor to the end of the file

:59        movecursor to line 59. Replace 59 by the desired line number.

20|        movecursor to column 20.

%        Movecursor to matching parenthesis

[[        Jumpto function start

[{        Jumpto block start

剪切、复制和粘贴

 

y        Copythe selected text to clipboard

p        Pasteclipboard contents

dd        Cutcurrent line

yy        Copycurrent line

y$        Copyto end of line

D        Cutto end of line

搜索

 

/word        Searchword from top to bottom

?word        Searchword from bottom to top

*        Searchthe word under cursor

/\cstring        SearchSTRING or string, case insensitive

/jo[ha]n        Searchjohn or joan

/\<the        Search the, theatre or then

/the\>        Searchthe or breathe

/\<the\>        Search the

/\<¦.\>        Search all words of 4letters

/\/        Searchfred but not alfred or frederick

/fred\|joe        Searchfred or joe

/\<\d\d\d\d\>        Searchexactly 4 digits

/^\n\{3}        Find3 empty lines

:bufdo/searchstr/        Search in all openfiles

bufdo%s/something/somethingelse/g        Searchsomething in all the open buffers and replace it with somethingelse

替换

 

:%s/old/new/g        Replaceall occurences of old by new in file

:%s/onward/forward/gi        Replaceonward by forward, case unsensitive

:%s/old/new/gc        Replaceall occurences with confirmation

:2,35s/old/new/g        Replaceall occurences between lines 2 and 35

:5,$s/old/new/g        Replaceall occurences from line 5 to EOF

:%s/^/hello/g        Replacethe begining of each line by hello

:%s/$/Harry/g        Replacethe end of each line by Harry

:%s/onward/forward/gi        Replaceonward by forward, case unsensitive

:%s/*$//g        Delete all white spaces

:g/string/d        Deleteall lines containing string

:v/string/d        Deleteall lines containing which didn’t contain string

:s/Bill/Steve/        Replacethe first occurence of Bill by Steve in current line

:s/Bill/Steve/g        ReplaceBill by Steve in current line

:%s/Bill/Steve/g        ReplaceBill by Steve in all the file

:%s/^M//g        DeleteDOS carriage returns (^M)

:%s/\r/\r/g        TransformDOS carriage returns in returns

:%s#<[^>]\+>##g        DeleteHTML tags but keeps text

:%s/^\(.*\)\n\1$/\1/        Deletelines which appears twice

Ctrl+a        Incrementnumber under the cursor

Ctrl+x        Decrementnumber under cursor

ggVGg?        Changetext to Rot13

大小写

 

Vu        Lowercaseline

VU        Uppercaseline

g~~        Invertcase

vEU        Switchword to uppercase

vE~        Modifyword case

ggguG        Setall text to lowercase

gggUG        Setall text to uppercase

:setignorecase        Ignore case insearches

:setsmartcase        Ignore case insearches excepted if an uppercase letter is used

:%s/\<./\u&/g        Setsfirst letter of each word to uppercase

:%s/\<./\l&/g        Setsfirst letter of each word to lowercase

:%s/.*/\u&        Setsfirst letter of each line to uppercase

:%s/.*/\l&        Setsfirst letter of each line to lowercase

读写文件

 

:1,10 woutfile        Saves lines 1 to 10 inoutfile

:1,10 w >>outfile        Appends lines 1 to 10 tooutfile

:rinfile        Insert the content ofinfile

:23rinfile        Insert the content ofinfile under line 23

文件浏览器

 

:e.        Open integrated file explorer

:Sex        Splitwindow and open integrated file explorer

:Sex!        Sameas :Sex but split window vertically

:browsee        Graphical file explorer

:ls        Listbuffers

:cd..        Move to parent directory

:args        Listfiles

:args*.php        Open file list

:grep expression*.php        Returns a list of .phpfiles contening expression

gf        Openfile name under cursor

Unix系统交互

 

:!pwd        Executethe pwd unix command, then returns to Vi

!!pwd        Executethe pwd unix command and insert output in file

:sh        Temporaryreturns to Unix

$exit        Retournsto Vi

对齐

 

:%!fmt        Alignall lines

!}fmt        Alignall lines at the current position

5!!fmt        Alignthe next 5 lines

Tabs/Windows

 

:tabnew        Createsa new tab

gt        Shownext tab

:tabfirst        Showfirst tab

:tablast        Showlast tab

:tabmn(position)        Rearrange tabs

:tabdo%s/foo/bar/g        Execute a commandin all tabs

:tabball        Puts all open files in tabs

:newabc.txt        Edit abc.txt in newwindow

分屏显示

 

:efilename        Edit filename incurrent window

:splitfilename        Split the window andopen filename

ctrl-w uparrow        Puts cursor in top window

ctrl-wctrl-w        Puts cursor in nextwindow

ctrl-w_        Maximizecurrent window vertically

ctrl-w|        Maximizecurrent window horizontally

ctrl-w=        Givesthe same size to all windows

10ctrl-w+        Add 10 lines to currentwindow

:vsplitfile        Split window vertically

:sviewfile        Same as :split in readonlymode

:hide        Closecurrent window

:­nly        Closeall windows, excepted current

:b2        Open #2 in this window

自动完成

 

Ctrl+n Ctrl+p (ininsert mode)        Complete word

Ctrl+xCtrl+l        Complete line

:setdictionary=dict        Define dict as adictionnary

Ctrl+xCtrl+k        Complete with dictionnary

Marks

 

m{a-z}        Marks current position as{a-z}

'{a-z}        Move to position {a-z}

''        Moveto previous position

缩写

 

:ab mailmail@provider.org        Define mail asabbreviation of mail@provider.org

文本缩进

 

:setautoindent        Turn on auto-indent

:setsmartindent        Turn on intelligentauto-indent

:setshiftwidth=4        Defines 4 spaces asindent size

ctrl-t,ctrl-d        Indent/un-indent ininsert mode

>>        Indent

<<        Un-indent

=%        Indentthe code between parenthesis

1GVG=        Indentthe whole file

语法高亮

 

:syntaxon        Turn on syntax highlighting

:syntaxoff        Turn off syntax highlighting

:setsyntax=perl        Force syntaxhighlighting

原文链接:http://www.catswhocode.com/blog/130-essential-vim-commands

 

译文链接:http://www.oschina.net/news/43167/130-essential-vim-commands

0 0