Linux学习2_Vim命令总结及解释

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



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

基础

:e filenameOpen filename for edition
:wSave file
:qExit Vim
:q!Quit without saving
:xWrite file (if changes has been made) and exit
:sav filenameSaves 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 Arrowmove the cursor up one line
j or Down Arrowmove the cursor down one line
emove the cursor to the end of the word
bmove the cursor to the begining of the word
0move the cursor to the begining of the line
Gmove the cursor to the end of the line
ggmove the cursor to the begining of the file
Lmove the cursor to the end of the file
:59move 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

剪切、复制和粘贴

yCopy the selected text to clipboard
pPaste clipboard contents
ddCut current line
yyCopy current line
y$Copy to end of line
DCut to end of line

搜索

/wordSearch word from top to bottom
?wordSearch word from bottom to top
*Search the word under cursor
/\cstringSearch STRING or string, case insensitive
/jo[ha]nSearch john or joan
/\< theSearch 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\|joeSearch 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/gSearch something in all the open buffers and replace it withsomethingelse

替换

:%s/old/new/gReplace all occurences of old by new in file
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/old/new/gcReplace all occurences with confirmation
:2,35s/old/new/gReplace all occurences between lines 2 and 35
:5,$s/old/new/gReplace all occurences from line 5 to EOF
:%s/^/hello/gReplace the begining of each line by hello
:%s/$/Harry/gReplace the end of each line by Harry
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/ *$//gDelete all white spaces
:g/string/dDelete all lines containing string
:v/string/dDelete 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/gReplace Bill by Steve in current line
:%s/Bill/Steve/gReplace Bill by Steve in all the file
:%s/^M//gDelete DOS carriage returns (^M)
:%s/\r/\r/gTransform DOS carriage returns in returns
:%s#<[^>]\+>##gDelete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/Delete lines which appears twice
Ctrl+aIncrement number under the cursor
Ctrl+xDecrement number under cursor
ggVGg?Change text to Rot13

大小写

VuLowercase line
VUUppercase line
g~~Invert case
vEUSwitch word to uppercase
vE~Modify word case
ggguGSet all text to lowercase
gggUGSet all text to uppercase
:set ignorecaseIgnore case in searches
:set smartcaseIgnore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/gSets first letter of each word to uppercase
:%s/\<./\l&/gSets 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 outfileSaves lines 1 to 10 in outfile
:1,10 w >> outfileAppends lines 1 to 10 to outfile
:r infileInsert the content of infile
:23r infileInsert the content of infile under line 23

文件浏览器

:e .Open integrated file explorer
:SexSplit window and open integrated file explorer
:Sex!Same as :Sex but split window vertically
:browse eGraphical file explorer
:lsList buffers
:cd ..Move to parent directory
:argsList files
:args *.phpOpen file list
:grep expression *.phpReturns a list of .php files contening expression
gfOpen file name under cursor

和 Unix 系统交互

:!pwdExecute the pwd unix command, then returns to Vi
!!pwdExecute the pwd unix command and insert output in file
:shTemporary returns to Unix
$exitRetourns to Vi

对齐

:%!fmtAlign all lines
!}fmtAlign all lines at the current position
5!!fmtAlign the next 5 lines

Tabs/Windows

:tabnewCreates a new tab
gtShow next tab
:tabfirstShow first tab
:tablastShow last tab
:tabm n(position)Rearrange tabs
:tabdo %s/foo/bar/gExecute a command in all tabs
:tab ballPuts all open files in tabs
:new abc.txtEdit abc.txt in new window

分屏显示

:e filenameEdit filename in current window
:split filenameSplit the window and open filename
ctrl-w up arrowPuts cursor in top window
ctrl-w ctrl-wPuts 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 fileSplit window vertically
:sview fileSame as :split in readonly mode
:hideClose current window
:­nlyClose all windows, excepted current
:b 2Open #2 in this window

自动完成

Ctrl+n Ctrl+p (in insert mode)Complete word
Ctrl+x Ctrl+lComplete line
:set dictionary=dictDefine dict as a dictionnary
Ctrl+x Ctrl+kComplete with dictionnary

Marks

m {a-z}Marks current position as {a-z}
' {a-z}Move to position {a-z}
''Move to previous position

缩写

:ab mail mail@provider.orgDefine mail as abbreviation of mail@provider.org

文本缩进

:set autoindentTurn on auto-indent
:set smartindentTurn on intelligent auto-indent
:set shiftwidth=4Defines 4 spaces as indent size
ctrl-t, ctrl-dIndent/un-indent in insert mode
>>Indent
<<Un-indent
=%Indent the code between parenthesis
1GVG=Indent the whole file

语法高亮

:syntax onTurn on syntax highlighting
:syntax offTurn off syntax highlighting
:set syntax=perlForce syntax highlighting


Vim命令合集

命令历史

以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令。

启动vim

在命令行窗口中输入以下命令即可

vim 直接启动vim

vim filename 打开vim并创建名为filename的文件

文件命令

打开单个文件

vim file

同时打开多个文件

vim file1 file2 file3 ...

在vim窗口中打开一个新文件

:open file

在新窗口中打开文件

:split file

切换到下一个文件

:bn

切换到上一个文件

:bp

查看当前打开的文件列表,当前正在编辑的文件会用[]括起来。

:args

打开远程文件,比如ftp或者share folder

:e ftp://192.168.10.76/abc.txt

:e \\qadrive\test\1.txt

vim的模式

正常模式(按Esc或Ctrl+[进入) 左下角显示文件名或为空
插入模式(按i键进入) 左下角显示--INSERT--
可视模式(不知道如何进入) 左下角显示--VISUAL--

导航命令

% 括号匹配

插入命令

i 在当前位置生前插入

I 在当前行首插入

a 在当前位置后插入

A 在当前行尾插入

o 在当前行之后插入一行

O 在当前行之前插入一行

查找命令

/text  查找text,按n健查找下一个,按N健查找前一个。

?text  查找text,反向查找,按n健查找下一个,按N健查找前一个。

vim中有一些特殊字符在查找时需要转义  .*[]^%/?~$

:set ignorecase  忽略大小写的查找

:set noignorecase  不忽略大小写的查找

查找很长的词,如果一个词很长,键入麻烦,可以将光标移动到该词上,按*或#键即可以该单词进行搜索,相当于/搜索。而#命令相当于?搜索。

:set hlsearch  高亮搜索结果,所有结果都高亮显示,而不是只显示一个匹配。

:set nohlsearch  关闭高亮搜索显示

:nohlsearch  关闭当前的高亮显示,如果再次搜索或者按下n或N键,则会再次高亮。

:set incsearch  逐步搜索模式,对当前键入的字符进行搜索而不必等待键入完成。

:set wrapscan  重新搜索,在搜索到文件头或尾时,返回继续搜索,默认开启。

替换命令

ra 将当前字符替换为a,当期字符即光标所在字符。

s/old/new/ 用old替换new,替换当前行的第一个匹配

s/old/new/g 用old替换new,替换当前行的所有匹配

%s/old/new/ 用old替换new,替换所有行的第一个匹配

%s/old/new/g 用old替换new,替换整个文件的所有匹配

:10,20 s/^/    /g 在第10行知第20行每行前面加四个空格,用于缩进。

ddp 交换光标所在行和其下紧邻的一行。

移动命令

h 左移一个字符
l 右移一个字符,这个命令很少用,一般用w代替。
k 上移一个字符
j 下移一个字符
以上四个命令可以配合数字使用,比如20j就是向下移动20行,5h就是向左移动5个字符,在Vim中,很多命令都可以配合数字使用,比如删除10个字符10x,在当前位置后插入3个!,3a!<Esc>,这里的Esc是必须的,否则命令不生效。

w 向前移动一个单词(光标停在单词首部),如果已到行尾,则转至下一行行首。此命令快,可以代替l命令。

b 向后移动一个单词 2b 向后移动2个单词

e,同w,只不过是光标停在单词尾部

ge,同b,光标停在单词尾部。

^ 移动到本行第一个非空白字符上。

0(数字0)移动到本行第一个字符上,

<HOME> 移动到本行第一个字符。同0健。

$ 移动到行尾 3$ 移动到下面3行的行尾

gg 移动到文件头。 = [[

G(shift + g) 移动到文件尾。 = ]]

f(find)命令也可以用于移动,fx将找到光标后第一个为x的字符,3fd将找到第三个为d的字符。

F 同f,反向查找。

跳到指定行,冒号+行号,回车,比如跳到240行就是 :240回车。另一个方法是行号+G,比如230G跳到230行。

Ctrl + e 向下滚动一行

Ctrl + y 向上滚动一行

Ctrl + d 向下滚动半屏

Ctrl + u 向上滚动半屏

Ctrl + f 向下滚动一屏

Ctrl + b 向上滚动一屏

撤销和重做

u 撤销(Undo)
U 撤销对整行的操作
Ctrl + r 重做(Redo),即撤销的撤销。

删除命令

x 删除当前字符

3x 删除当前光标开始向后三个字符

X 删除当前字符的前一个字符。X=dh

dl 删除当前字符, dl=x

dh 删除前一个字符

dd 删除当前行

dj 删除上一行

dk 删除下一行

10d 删除当前行开始的10行。

D 删除当前字符至行尾。D=d$

d$ 删除当前字符之后的所有字符(本行)

kdgg 删除当前行之前所有行(不包括当前行)

jdG(jd shift + g)   删除当前行之后所有行(不包括当前行)

:1,10d 删除1-10行

:11,$d 删除11行及以后所有的行

:1,$d 删除所有行

J(shift + j)  删除两行之间的空行,实际上是合并两行。

拷贝和粘贴

yy 拷贝当前行

nyy 拷贝当前后开始的n行,比如2yy拷贝当前行及其下一行。

p  在当前光标后粘贴,如果之前使用了yy命令来复制一行,那么就在当前行的下一行粘贴。

shift+p 在当前行前粘贴

:1,10 co 20 将1-10行插入到第20行之后。

:1,$ co $ 将整个文件复制一份并添加到文件尾部。

正常模式下按v(逐字)或V(逐行)进入可视模式,然后用jklh命令移动即可选择某些行或字符,再按y即可复制

ddp交换当前行和其下一行

xp交换当前字符和其后一个字符

剪切命令

正常模式下按v(逐字)或V(逐行)进入可视模式,然后用jklh命令移动即可选择某些行或字符,再按d即可剪切

ndd 剪切当前行之后的n行。利用p命令可以对剪切的内容进行粘贴

:1,10d 将1-10行剪切。利用p命令可将剪切后的内容进行粘贴。

:1, 10 m 20 将第1-10行移动到第20行之后。

退出命令

:wq 保存并退出

ZZ 保存并退出

:q! 强制退出并忽略所有更改

:e! 放弃所有修改,并打开原来文件。

窗口命令

:split或new 打开一个新窗口,光标停在顶层的窗口上

:split file或:new file 用新窗口打开文件

split打开的窗口都是横向的,使用vsplit可以纵向打开窗口。

Ctrl+ww 移动到下一个窗口

Ctrl+wj 移动到下方的窗口

Ctrl+wk 移动到上方的窗口

关闭窗口

:close 最后一个窗口不能使用此命令,可以防止意外退出vim。

:q 如果是最后一个被关闭的窗口,那么将退出vim。

ZZ 保存并退出。

关闭所有窗口,只保留当前窗口

:only

录制宏

按q键加任意字母开始录制,再按q键结束录制(这意味着vim中的宏不可嵌套),使用的时候@加宏名,比如qa。。。q录制名为a的宏,@a使用这个宏。

执行shell命令

:!command

:!ls 列出当前目录下文件

:!perl -c script.pl 检查perl脚本语法,可以不用退出vim,非常方便。

:!perl script.pl 执行perl脚本,可以不用退出vim,非常方便。

:suspend或Ctrl - Z 挂起vim,回到shell,按fg可以返回vim。

注释命令

perl程序中#开始的行为注释,所以要注释某些行,只需在行首加入#

3,5 s/^/#/g 注释第3-5行

3,5 s/^#//g 解除3-5行的注释

1,$ s/^/#/g 注释整个文档。

:%s/^/#/g 注释整个文档,此法更快。

帮助命令

:help or F1 显示整个帮助
:help xxx 显示xxx的帮助,比如 :help i, :help CTRL-[(即Ctrl+[的帮助)。
:help 'number' Vim选项的帮助用单引号括起
:help <Esc> 特殊键的帮助用<>扩起
:help -t Vim启动参数的帮助用-
:help i_<Esc> 插入模式下Esc的帮助,某个模式下的帮助用模式_主题的模式
帮助文件中位于||之间的内容是超链接,可以用Ctrl+]进入链接,Ctrl+o(Ctrl + t)返回

其他非编辑命令

. 重复前一次命令

:set ruler?  查看是否设置了ruler,在.vimrc中,使用set命令设制的选项都可以通过这个命令查看

:scriptnames  查看vim脚本文件的位置,比如.vimrc文件,语法文件及plugin等。

:set list 显示非打印字符,如tab,空格,行尾等。如果tab无法显示,请确定用set lcs=tab:>-命令设置了.vimrc文件,并确保你的文件中的确有tab,如果开启了expendtab,那么tab将被扩展为空格。

Vim教程
在Unix系统上
$ vimtutor
在Windows系统上
:help tutor

:syntax 列出已经定义的语法项
:syntax clear 清除已定义的语法规则
:syntax case match 大小写敏感,int和Int将视为不同的语法元素
:syntax case ignore 大小写无关,int和Int将视为相同的语法元素,并使用同样的配色方案

文章来自:http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html


vim 选择文本,删除,复制,粘贴  


文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下:

v    从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束。 

V    从光标当前行开始,光标经过的行都会被选中,再按一下V结束。 

Ctrl + v   从光标当前位置开始,选中光标起点和终点所构成的矩形区域,再按一下Ctrl + v结束。 

ggVG 选中全部的文本, 其中gg为跳到行首,V选中整行,G末尾


选中后就可以用编辑命令对其进行编辑,如 
d   删除 

y   复制 (默认是复制到"寄存器) 

p  粘贴 (默认从"寄存器取出内容粘贴) 


"+y    复制到系统剪贴板(也就是vim的+寄存器) 

"+p   从系统剪贴板粘贴 



小总结

说明:以下黑色为vi和vim均有的一般功能,而红色为Vim(Vi Improved)所特有功能。Vim一般的Unix和Linux下均有安装。
 三种状态
Command: 任何输入都会作为编辑命令,而不会出现在屏幕上,任何输入都引起立即反映
Insert:  任何输入的数据都置于编辑寄存器,按ESC,可跳回command方式
Escape:  以“:”或者“/”为前导的指令,出现在屏幕的最下一行,任何输入都被当成特别指令。
 离开vi
:q!    离开vi,并放弃刚在缓冲区内编辑的内容。
:wq   将缓冲区内的资料写入磁盘中,并离开vi。
:x    同wq。
(注意—— :X 是文件加密,一定要与:x存盘退出相区别)
 进入输入模式
a (append)  由游标之后加入资料。
A    由该行之末加入资料。
i (insert)   由游标之前加入资料。
I    由该行之首加入资料。
o (open)   新增一行於该行之下供输入资料之用。
O    新增一行於该行之上供输入资料之用。
 删除与修改
x    删除游标所在该字元。
X    删除游标所在之前一字元。
r    用接於此指令之后的字元取代(replace)游标所在字元。如:ra将游标所在字元以 a 取代之。
R    进入取代状态,直到《ESC》为止。
s    删除游标所在之字元,并进入输入模式直到《ESC》。
S    删除游标所在之该行资料,并进入输入模式直到《ESC》。
 光标的移动
m<a-z>  设置书签<a-z>
‘<a-z>  移至书签<a-z>处
0    移至该行之首
$    移至该行之末。
e   移动到下个字的最後一个字母
w    移动到下个字的第一个字母。
b    移动到上个字的第一个字母。
^    移至该行的第一个字元处。
H    移至视窗的第一行。
M    移至视窗的中间那行。
L    移至视窗的最后一行。
G    移至该文件的最后一行。
+    移至下一列的第一个字元处。
- 移至上一列的第一个字元处。
:n    移至该文件的第 n 列。
n+    移至游标所在位置之后的第 n 列。
n-    移至游标所在位置之前的第 n 列。
<Ctrl><g>  显示该行之行号、文件名称、文件中最末行之行号、游标所在行号占总行号之百分比。

(Vim) 光标移动基本用法小解:
(这只要组合上边的功能就可以明白了,不用再一一讲解了吧!)
ge     b   w          e
←    ←    ---→        --→
This is-a  line,  with special/separated/words (and some more).
←-  ←--    -----------------→     ---→
GE   B        W      E

 视窗的移动
<Ctrl><f>  视窗往下卷一页。
<Ctrl><b>  视窗往上卷一页。
<Ctrl><d>  视窗往下卷半页。
<Ctrl><u>  视窗往上卷半页。
<Ctrl><e>  视窗往下卷一行。
<Ctrl><y>  视窗往上卷一行。
 剪切、复制、删除
Operator + Scope = command
 Operator
d    剪切
y    复制。
p    粘帖,与 d 和 y 配和使用。可将最后d或y的资料放置於游标所在位置之行列下。
c    修改,类似delete与insert的组和。删除一个字组、句子等之资料,并插入新建资料。
 Scope
e    由游标所在位置至该字串的最后一个字元。
w    由游标所在位置至下一个字串的第一个字元。
b    由游标所在位置至前一个字串的第一个字元。
$    由游标所在位置至该行的最后一个字元。
0    由游标所在位置至该行的第一个字元。
 整行动作
dd    删除整行。
D    以行为单位,删除游标后之所有字元。
cc    修改整行的内容。
yy   使游标所在该行复制到记忆体缓冲区。
 取消前一动作(Undo)
u    恢复最后一个指令之前的结果。
U    恢复游标该行之所有改变。
(vim) u   可以多次撤消指令,一次撤消一个操作,直至本次操作开始为止。
(vim) Ctrl+r 可以恢复撤消前内容,按多次可恢复多次。
 查找与替换
/字串   往游标之后寻找该字串。
?字串   往游标之前寻找该字串。
n    往下继续寻找下一个相同的字串。
N    往上继续寻找下一个相同的字串。
%   查找“(”,“)”,“{”,“}”的配对符。
s   搜寻某行列范围。
g   搜寻整个编辑缓冲区的资料。
:1,$s/old/new/g 将文件中所有的『old』改成『new』。
:10,20s/^/ /  将第10行至第20行资料的最前面插入5个空白。
(vim) 
/字符串   后边输入查询内容可保存至缓冲区中,可用↑↓进行以往内容选择。
另外:将光标移动在选定单词下方按*,则可以选中此单词作为查询字符,可以避免输入一长串字符的麻烦。
 (vim) 大小写替换
首先用按v开启选择功能,然后用↑↓←→键来选定所要替换的字符,若是小写变大写,则按U;反之按u;
如果是选择单词,则可以在按v后,按w,最后按U/u,这样就可以将字符随意的改变大小写了,而不用删除后重新敲入。

 资料的连接
J    句子的连接。将游标所在之下一行连接至游标该行的后面。
 环境的设定
:set all  可设置的环境变量列表
:set   环境变量的当前值
:set nu   设定资料的行号。
:set nonu  取消行号设定。
:set ai   自动内缩。
:set noai   取消自动内缩。
(vim) 
:set ruler  会在屏幕右下角显示当前光标所处位置,并随光移动而改变,占用屏幕空间较小,使用也比较方便,推荐使用。
:set hlsearch 在使用查找功能时,会高亮显示所有匹配的内容。
:set nohlsearch  关闭此功能。
:set incsearch  使Vim在输入字符串的过程中,光标就可定位显示匹配点。
:set nowrapscan 关闭查找自动回环功能,即查找到文件结尾处,结束查找;默认状态是自动回环

 ex指令
 读写资料
:10,20w test  将第10行至第20行的资料写入test文件。
:10,20w>>test 将第10行至第20行的资料加在test文件之后。
:r test   将test文件的资料读入编辑缓冲区的最后。
:e [filename] 编辑新的文件。
:e! [filename] 放弃当前修改的文件,编辑新的文件。
:sh   进入shell环境,使用exit退出,回到编辑器中。

:!cmd  运行命令cmd后,返回到编辑器中。
 删除、复制及搬移
:10,20d   删除第10行至第20行的资料。
:10d   删除第10行的资料。
:%d   删除整个编辑缓冲区。
:10,20co30  将第10行至第20行的资料复制至第30行之后。
:10,20mo30  将第10行至第20行的资料搬移至第30行之后。

原文来自:http://blog.csdn.net/sunboy_2050/article/details/5929428

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值