现在的位置: 首页 > 综合 > 正文

VIM 常用命令集锦

2013年10月06日 ⁄ 综合 ⁄ 共 9447字 ⁄ 字号 评论关闭

VIM 常用命令集锦

 gf 打开的当前光标下的文件        

 Ctrl +e  Ctrol+y  按照上/下行写

vim中其它的补全方式包括:

整行补全                        CTRL-X CTRL-L
根据当前文件里关键字补全        CTRL-X CTRL-N
根据字典补全                    CTRL-X CTRL-K
根据同义词字典补全              CTRL-X CTRL-T
根据头文件内关键字补全          CTRL-X CTRL-I
根据标签补全                    CTRL-X CTRL-]
补全文件名                      CTRL-X CTRL-F
补全宏定义                      CTRL-X CTRL-D
补全vim命令                     CTRL-X CTRL-V
用户自定义补全方式              CTRL-X CTRL-U
拼写建议                        CTRL-X CTRL-S

%  在代码块之间跳转, 一般是()或[]或{}

C-w p  上次编辑的窗口

C-w w  多窗口间交换

C-b C-f 下一页

g/pattern/d 是找到pattern, 删之

v/pattern/d 是找到非pattern,删

C-W +   增加split当前窗口大小

C-W - 减小split当前窗口大小

df<char>  向前删除包含char

dt<char> 向前删除不包含char


100 Vim commands every programmer should know
=============================================

Published on June 30th, 2008 by Jean-Baptiste Jung. 118 Comments -
Since the 70â²s, Vi is one of the programmerâs best friend. Nevermind youâre new to Vi or not, hereâs a big list of 100 useful commands, organized by topic, which will make your coder life better.

Basics
======
:e filename	Open filename for edition
:w	Save file
:q	Exit Vim
:w!	Exit Vim without saving

Search
======
/word	Search word from top to bottom
?word	Search word from bottom to top
/jo[ha]n	Search john or joan
/\<the	Search the, theatre or then
/the\>	Search the or breathe
/\<the\>	Search the
/\<.\{4}\>	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

Replace
=======
:%s/old/new/g	Replace all occurences of old by new in file
:%s/old/new/gw	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/\r//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 Case
Vu	Lowercase line
VU	Uppercase line
g~~	Invert case
vEU	Switch word to uppercase
vE~	Modify word case
ggguG	Set all text to lowercase
: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

Read/Write files
================
: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

File explorer
============
:e .	Open integrated file explorer
:Sex	Split window and open integrated file explorer
: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
Interact with Unix
:!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

Alignment
========
:%!fmt	Align all lines
!}fmt	Align all lines at the current position
5!!fmt	Align the next 5 lines

Tabs
====
: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

Window spliting
==============
: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_	Maximise current window
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

Auto-completion
===============
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

Marks
=====
mk	Marks current position as k
~K	Moves cursor to mark k
dâ¢k	Delete all until mark k

Abbreviations
=============
:ab mail mail@provider.org	Define mail as abbreviation of mail@provider.org

Text indent
==========
: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

Syntax highlighting
==================
:syntax on	Turn on syntax highlighting
:syntax off	Turn off syntax highlighting
:set syntax=perl	Force syntax highlighting

打开编辑:
    【e filename】编辑指定文件。
切换到插入模式:
i   插入到当前光标下的字母的前面
a   插入到当前光标下的字母的后边
o   在当前行的下一行插入一个新行
O   在当前行的上一行插入一个新行

移动光标:
    「h」、「j」、「k」、「l」,分别控制光标左、下、上、右移一格
   按「ctrl」+「b」:屏幕往“后”移动一页。
  按「ctrl」+「f」:屏幕往“前”移动一页。
  按「ctrl」+「u」:屏幕往“后”移动半页。
  按「ctrl」+「d」:屏幕往“前”移动半页。
  按数字「0」:移到光标所在行的“行首”,就是第0列。
  按「^」:移动到光标所在行的“行首”,不计算前面的空格。
  按「$」:移动到光标所在行的“行尾”。
  按「G」:移动到文章的最后。
  按「gg」:移动到文章的开头。
  按「w」:光标跳到下个字的开头。
  按「e」:光标跳到下个字的字尾。
  按「b」:光标回到上个字的开头。
  按「#l」:光标移到该行的第#个位置,如:5l,56l。
    按 [f]: 行内向前查找某个字符。
    按 [F]: 行内向后查找某个字符。
删除文字:
  「x」:每按一次,删除光标所在位置的“后面”一个字符。
  「#x」:例如,「6x」表示删除光标所在位置的“后面”6个字符。
  「X」:大写的X,每按一次,删除光标所在位置的“前面”一个字符。
  「#X」:例如,「20X」表示删除光标所在位置的“前面”20个字符。
  「dd」:删除光标所在行。
  「#dd」:从光标所在行开始删除#行。
复制:
  「yw」:将光标所在之处到字尾的字符复制到缓冲区中。
  「#yw」:复制#个字到缓冲区
  「yy」:复制光标所在行到缓冲区。
  「#yy」:例如,「6yy」表示拷贝从光标所在的该行“往下数”6行文字。
  「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与“y”有关的复制命令都必须与“p”配合才能完成复制与粘贴功能。
替换:
  「r」:替换光标所在处的字符。
  「R」:替换光标所到之处的字符,直到按下「ESC」键为止。
撤销和重做:
  「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次“u”可以执行多次回复。
    [C-r]: redo.重做命令。
更改:
  「cw」:更改光标所在处的字到字尾处。
  「c#w」:例如,「c3w」表示更改3个字。
跳至指定的行:
  「ctrl」+「g」列出光标所在行的行号。
  「#G」:例如,「15G」,表示移动光标至文章的第15行行首。
A) 列出行号
 「set nu」:输入「set nu」后,会在文件中的每一行前面列出行号。
B) 跳到文件中的某一行
 「#」:「#」号表示一个数字,在冒号后输入一个数字,再按回车键就会跳到该行了,如输入数字15,再回车,就会跳到文章的第15行。
C) 查找字符
 「/关键字」:先按「/」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往后寻找到您要的关键字为止。从开头找。
 「?关键字」:先按「?」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往前寻找到您要的关键字为止。从末尾找。
D) 保存文件
 「w」:在冒号输入字母「w」就可以将文件保存起来。
E) 离开vi
 「q」:按「q」就是退出,如果无法离开vi,可以在「q」后跟一个「!」强制离开vi。
 「qw」:一般建议离开时,搭配「w」一起使用,这样在退出的时候还可以保存文件。
区块选择:
    v :字符选择,会将光标经过癿地方反白选择!
    V(大写) :行选择,会将光标经过癿行反白选择!
    [Ctrl]+v : 区块选择,可以用长方形癿方式选择资料
    y :将反白癿地方复制起来 d 将反白癿地方删除掉
区块替换:
    C-V: 然后使用hjkl选中块操作区域。使用I在区域前插入,一般用于单行注释,使用A添加到后边,使用r全部替换成一个字符,
            使用c的话,使用输入词替换每一行。然后使用ESC使操作对整个区块生效。

Basic Editing
=============

Basics
------
hjkl    Eursor keys
i       Enter insert mode, cursor turns from a blok into a vertial line,
            new entered before current char.
x       Delete current character.
X       Delete the one to the left.
A       Insert text at the end of line.
Extras
------
u       Undo the last action. vim unlimited undo.
C-R     Redo action.
0       Jumps directly to the beginning of the line.
$       Jumps directly to the end of the line.
^       Jumps directly to the first non-blank.
w,e,b   Move along 'words'.
W,E,B   Move along 'WORDs'.
R       Enter inset mode with a overstrike cursor. witch types over existing characters.
:w<CR>  Save.
:q<CR>  Quit.

Operators & Repetition
======================

Basics
------
f       Moves the cursor to the next instance of that character on the current line.
F       Does the same backwards.
t       Same as 'f', but stop right before the character.
T       Same as 'F', but stop right before the character.
d       Delete, followed, by any motion deletes the next between the cursor 
            and that motion's destination. 'dw','df-'.
c       Change, does the same like R, but leaves you in insert mode.
.       Repeats the last editing action. 
Extras
------
Prepend a count to any command/motion to repeat it that number of times.
d2w     Delete up to the second word.
d2t,    Delete up to but not including the second comma.
2i      Repeats the text after you press(ESC) to finish the input session.
cc      Change,operator on the current line.
dd      Delete, operator on the current line.
v       Enters visual mode.
V       Enters visual line mode.
C-v     Selects rectangular blocks.

Yank & Paste
============

Basics
------
y       Followed by any motion to yank(copy). 
p       Paste after(if charwise, to the right, if linewise, below)
P       Paste before.
yy      Copy the current line.
y       Works also but in visual mode.
Note: Text deleted with d,c,x...is also copied.
Extras
------
"a-z    Before any yank/delete/paste command choose a register.
"A-Z    Same as above but append only.
"* or "+    Select the system clipboard.
o       Enters insert mode in a new line below the current one.
O       Does the same above the current line.

Searching
=========

Basics
------
/       Basic search motion.
?       Does the same, backwards.
n       Repeats the last search in the same directon.
N       Repeats the last search in the reverse direction.
Extras
------
*       Search forward for the next instance of the identifier under the cursor.
#       Does the same backwards.

Marks & Macros
=============

Marks
-----
m       Followed by an a-z A-Z character to set a mark.
`       Followed by an a-z A-Z character to go to that mark.
'       Followed by an a-z A-Z character to go to the first non-blank in that line.
`.      Refers to the position of the last modification.
Note: A-Z marks are global, a-z per-buffer.
Macros
------
q       Followed by an a-z character to start recording. Use q afterwards to stop recording.
@       Followed by an a-z character replays that macro.
@@      Repeat the last macro played.

Various Motions
===============
%       Jumps between matching pairs of (),[],{} etc...
H,M,L   Jumps directly to the top/middle/bottom of the screen.
G       Jumps to the end of the file, or to the line # typed before it.
-/+     Jumps to the previous/next line.
K       Jumps to the help for the word under the cursor.
()      Jumps to the beginning/end of the current sentence.
{}      Jumps to the previous/next empty line.
[[      Jumps to the previous '{' in column 0.
]]      Jumps to the next '}' in column 0.

Various Commands
================

Basics
------
J       Joins the current line with the next one, or all the lines in the current visual selection.
r       Followed by any character replaces the current character with that one.
C       The shorthand for c$, change to end of line.
D       The shorthand for d$, delete to end of line.
Y       The shorthand for yy, yanks the whole line.
s       Delete the character under cursor and enters insert mode.
S       Clear the current line and enters insert mode.
Extras
------
>       And a motion to indent one or more lines.
<       And a motion to unindent one or more lines.
>>      Indent 4.
<<      Unindent 4.
Note: All of them work in visual mode, or can be repeated to operate on the current line.
~       Toggles the case of the character under the cursor.

抱歉!评论已关闭.