http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/
分类: linux
2015-10-21 13:55:55
从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为 “每个程序员都应该知道的 100 个 vim 命令” 这次算是之前那篇文章的改进版,希望你会喜欢。
:e filename | open filename for edition |
:w | save file |
:q | exit vim |
:q! | quit without saving |
:x | write file (if changes has been made) and exit |
:sav filename | saves file as filename |
. | repeats the last change made in normal mode |
5. | repeats 5 times the last change made in normal mode |
k or up arrow | move the cursor up one line |
j or down arrow | move the cursor down one line |
e | move the cursor to the end of the word |
b | move the cursor to the begining of the word |
0 | move the cursor to the begining of the line |
g | move the cursor to the end of the line |
gg | move the cursor to the begining of the file |
l | move the cursor to the end of the file |
:59 | move cursor to line 59. replace 59 by the desired line number. |
20| | move cursor to column 20. |
% | move cursor to matching parenthesis |
[[ | jump to function start |
[{ | jump to block start |
y | copy the selected text to clipboard |
p | paste clipboard contents |
dd | cut current line |
yy | copy current line |
y$ | copy to end of line |
d | cut to end of line |
/word | search word from top to bottom |
?word | search word from bottom to top |
* | search the word under cursor |
/\cstring | search string or string, case insensitive |
/jo[ha]n | search john or joan |
/\< the | search the, theatre or then |
/the\> | search the or breathe |
/\< the\> | search the |
/\< ?.\> | search all words of 4 letters |
/\/ | search fred but not alfred or frederick |
/fred\|joe | search fred or joe |
/\<\d\d\d\d\> | search exactly 4 digits |
/^\n\{3} | find 3 empty lines |
:bufdo /searchstr/ | search in all open files |
bufdo %s/something/somethingelse/g | search something in all the open buffers and replace it with somethingelse |
:%s/old/new/g | replace all occurences of old by new in file |
:%s/onward/forward/gi | replace onward by forward, case unsensitive |
:%s/old/new/gc | replace all occurences with confirmation |
:2,35s/old/new/g | replace all occurences between lines 2 and 35 |
:5,$s/old/new/g | replace all occurences from line 5 to eof |
:%s/^/hello/g | replace the begining of each line by hello |
:%s/$/harry/g | replace the end of each line by harry |
:%s/onward/forward/gi | replace onward by forward, case unsensitive |
:%s/ *$//g | delete all white spaces |
:g/string/d | delete all lines containing string |
:v/string/d | delete all lines containing which didn’t contain string |
:s/bill/steve/ | replace the first occurence of bill by steve in current line |
:s/bill/steve/g | replace bill by steve in current line |
:%s/bill/steve/g | replace bill by steve in all the file |
:%s/^m//g | delete dos carriage returns (^m) |
:%s/\r/\r/g | transform dos carriage returns in returns |
:%s#<[^>]\ >##g | delete html tags but keeps text |
:%s/^\(.*\)\n\1$/\1/ | delete lines which appears twice |
ctrl a | increment number under the cursor |
ctrl x | decrement number under cursor |
ggvgg? | change text to rot13 |
vu | lowercase line |
vu | uppercase line |
g~~ | invert case |
veu | switch word to uppercase |
ve~ | modify word case |
gggug | set all text to lowercase |
gggug | set all text to uppercase |
:set ignorecase | ignore case in searches |
:set smartcase | ignore case in searches excepted if an uppercase letter is used |
:%s/\<./\u&/g | sets first letter of each word to uppercase |
:%s/\<./\l&/g | sets first letter of each word to lowercase |
:%s/.*/\u& | sets first letter of each line to uppercase |
:%s/.*/\l& | sets first letter of each line to lowercase |
:1,10 w outfile | saves lines 1 to 10 in outfile |
:1,10 w >> outfile | appends lines 1 to 10 to outfile |
:r infile | insert the content of infile |
:23r infile | insert the content of infile under line 23 |
:e . | open integrated file explorer |
:sex | split window and open integrated file explorer |
:sex! | same as :sex but split window vertically |
:browse e | graphical file explorer |
:ls | list buffers |
:cd .. | move to parent directory |
:args | list files |
:args *.php | open file list |
:grep expression *.php | returns a list of .php files contening expression |
gf | open file name under cursor |
:!pwd | execute the pwd unix command, then returns to vi |
!!pwd | execute the pwd unix command and insert output in file |
:sh | temporary returns to unix |
$exit | retourns to vi |
:%!fmt | align all lines |
!}fmt | align all lines at the current position |
5!!fmt | align the next 5 lines |
:tabnew | creates a new tab |
gt | show next tab |
:tabfirst | show first tab |
:tablast | show last tab |
:tabm n(position) | rearrange tabs |
:tabdo %s/foo/bar/g | execute a command in all tabs |
:tab ball | puts all open files in tabs |
:new abc.txt | edit abc.txt in new window |
:e filename | edit filename in current window |
:split filename | split the window and open filename |
ctrl-w up arrow | puts cursor in top window |
ctrl-w ctrl-w | puts cursor in next window |
ctrl-w_ | maximize current window vertically |
ctrl-w| | maximize current window horizontally |
ctrl-w= | gives the same size to all windows |
10 ctrl-w | add 10 lines to current window |
:vsplit file | split window vertically |
:sview file | same as :split in readonly mode |
:hide | close current window |
:nly | close all windows, excepted current |
:b 2 | open #2 in this window |
ctrl n ctrl p (in insert mode) | complete word |
ctrl x ctrl l | complete line |
:set dictionary=dict | define dict as a dictionnary |
ctrl x ctrl k | complete with dictionnary |
m {a-z} | marks current position as {a-z} |
' {a-z} | move to position {a-z} |
'' | move to previous position |
:ab mail mail@provider.org | define mail as abbreviation of mail@provider.org |
:set autoindent | turn on auto-indent |
:set smartindent | turn on intelligent auto-indent |
:set shiftwidth=4 | defines 4 spaces as indent size |
ctrl-t, ctrl-d | indent/un-indent in insert mode |
>> | indent |
<< | un-indent |
=% | indent the code between parenthesis |
1gvg= | indent the whole file |
:syntax on | turn on syntax highlighting |
:syntax off | turn off syntax highlighting |
:set syntax=perl |
force syntax highlighting |