【转】vim基本命令

1.
vim
#在命令行中输入vim,进入vim编辑器
2.
i
#按一下i键,下端显示 –INSERT–
#插入命令,在vim中可能任意字符都有作用
3.
Esc
#退出i(插入)命令进行其它命令使用
4.
:r filename
#读入一个文件内容,并写入到当前编辑器中
5.
:w newfilename
#将该编辑器中的内容写入到一个新文件中
6.
:w
#在编辑的过程中保存文件,相当于word中的ctrl+s
7.
:! command
暂时离开 vi 到指令列模式下执行 command 的显示结果!例如
:!ls
#在编辑过程中执行shell命令ls
8.
:sh
#进入shell命令行,执行完命令后ctrl+d退出重新进入vim编辑继续编辑

在shell命令下,执行ctral+l完成清屏
9.
:wq
#保存文件并退出
10.
ZZ
#保存文件并退出,同上一个命令,注意大写
11.
:q!
#强制退出,不保存
12.
:set number或者:set nu
#使编辑中的文件显示行号
13.
:set nonumber或者:set nonu
#与上一条命令相反,不显示行号
14.
:help i
#查看插入命令帮助
15.
u
#撤消上一步操作
16.
/Fedora
#查找Fedora字符
17.
:s /Fedora/Redhat
#将Fedora字符替换为Redhat(只替换在光标所在的行)
18.
dw
#删除单词
dd
#删除行
19.
o
#打开空白一行
20.
vim + filename
#进行文件最后一行进行编辑
21.
vim +n filename
#进入文件第n行进行编辑
22.
:1,.s/redhat/fedora
#.号表示当前行,即光标所在行
#将第1行到当前行(.)第一次出现的redhat字符代替为fedora
23.
:1,.s/redhat/fedora/g
#将第1行到当前行(.)所有出现的redhat字符代替为fedora,g
全局标志
24.
:1,$s/redhat/fedora/g
#$表示最后一行
#将第1行到最后一行所有出现的redhat字符代替为fedora
25.
:%s/redhat/fedora/g
#同上一个命令
26.
:%s/\/fedora/g
#将第1行到最后一行所有出现的redhat字代替为fedora
#字,而不是字符
27.
:f
#显示文件内容,状态等等
#同ctrl+g命令
28.
:e!
#当前文件,返回到上次保存
:e file
#切换编辑文件
29.
:n
#当编辑时有多个文件(比如vim file1 file2)时切换到下一个文件,与:e file
结合使用

VIM命令大全
光标控制命令
命令                   光标移动
h                   向左移一个字符
j                   向下移一行
k                   向上移一行
l                   向右移一个字符
G                   移到文件的最后一行
w                   移到下一个字的开头
W                   移到下一个字的开头,忽略标点符号
b                   移到前一个字的开头
B                   移到前一个字的开头,忽略标点符号
L                   移到屏幕的最后一行
M                   移到屏幕的中间一行
H                   移到屏幕的第一行
e                   移到下一个字的结尾
E                   移到下一个字的结尾,忽略标点符号
(                       移到句子的开头
)                       移到句子的结尾
{                       移到段落的开头
}                       移到下一个段落的开头
0(数字), |                移到当前行的第一列
^                       移到当前行的第一个非空字符
$                       移到当前行的最后一个字符
+, Enter             移到下一行的第一个字符
–                       移到前一行的第一个非空字符
在 vi 中添加文本
命令                   插入动作
a                   在光标后插入文本
A                   在当前行插入文本
i                   在光标前插入文本
I                   在当前行前插入文本
o                   在当前行的下边插入新行
O                   在当前行的上边插入新行
s                   删除光标所在处字符,并进入插入模式
S                   删除光标所在的行,并进入插入模式
:r file             读入文件 file 内容,并插在当前行后
:nr file            读入文件 file 内容,并插在第 n 行后
Esc                 回到命令模式
^v char             插入时忽略 char 的指定意义,这是为了插入特殊字符
在 vi 中删除文本
命令                   删除操作
x                   删除光标处的字符
dw                  删至下一个字的开头
dG                  删除行,直到文件结束
dd                  删除整行
db                  删除光标前面的字
:n,md               从第 m 行开始往前删除 n 行
d, d$               从光标处删除到行尾
^h, backspace       插入时,删除前面的字符
^w                  插入时,删除前面的字
修改 vi 文本
每个命令前面的数字表示该命令重复的次数
命令                               替换操作
rchar                         用 char 替换当前字符
R text escape                 用 text 替换当前字符直到按下 Esc 键
stext escape                  用 text 代替当前字符
S 或 cctext escape             用 text 代替整行
cwtext escape                 将当前字改为 text
Ctext escape                  将当前行余下的改为 text
cG escape                     修改至文件的末尾
ccursor_cmd text escape       从当前位置处到光标命令位置处都改为 text
在 vi 中查找与替换
命令                               查找与替换操作
/text                         在文件中向前查找 text
?text                         在文件中向后查找 text
n                             在同一方向重复查找
N                             在相反方向重复查找
ftext                         在当前行向前查找 text
Ftext                         在当前行向后查找 text
ttext                         在当前行向前查找 text,并将光标定位在 text 的第一个字符
Ttext                         在当前行向后查找 text,并将光标定位在 text 的第一个字符
:set ic                       查找时忽略大小写
:set noic                     查找时对大小写敏感
:ranges/pat1/pat2/g           用 newtext 替换 oldtext
:m,ns/oldtext/newtext         在 m 行通过 n,用 newtext 替换 oldtext
&                                  重复最后的 :s 命令
:g/text1/s/text2/text3        查找包含 text1 的行,用 text3 替换 text2
:g/text/command               在所有包含 text 的行运行 command 所表示的命令
:v/text/command               在所有不包含 text 的行运行 command 所表示的命令
在 vi 中复制文本
命令                      复制操作
yy                    将当前行的内容放入临时缓冲区
nyy                   将 n 行的内容放入临时缓冲区
p                     将临时缓冲区中的文本放入光标后
P                     将临时缓冲区中的文本放入光标前
“(a-z)nyy             复制 n 行放入名字为圆括号内的可命名缓冲区,省略 n 表示当前行
“(a-z)ndd             删除 n 行放入名字为圆括号内的可命名缓冲区,省略 n 表示当前行
“(a-z)p               将名字为圆括号的可命名缓冲区的内容放入当前行后
“(a-z)P               将名字为圆括号的可命名缓冲区的内容放入当前行前
在 vi 中撤消与重复
命令               撤消操作
u               撤消最后一次修改
U               撤消当前行的所有修改
.                  重复最后一次修改
,                  以相反的方向重复前面的 f、F、t 或 T 查找命令
;                  重复前面的 f、F、t 或 T 查找命令
“np             取回最后第 n 次的删除(缓冲区中存有一定次数的删除内容,一般为 9)
n               重复前面的 / 或 ? 查找命令
N               以相反方向重复前面的 / 或 ? 命令
保存文本和退出 vi
命令                      保存和/或退出操作
:w                    保存文件但不退出 vi
:w file               将修改保存在 file 中但不退出 vi
:wq 或 ZZ 或 😡         保存文件并退出 vi
:q!                   不保存文件,退出 vi
:e!                   放弃所有修改,从上次保存文件开始再编辑
vi 中的选项
选项                      作用
:set all              打印所有选项
:set nooption         关闭 option 选项
:set nu               每行前打印行号
:set showmode         显示是输入模式还是替换模式
:set autoindent          继承前一行的缩进方式,特别适用于多行注释
:set smartindent        为C程序提供自动缩进
:set list             显示制表符(^I)和行尾符号
:set ts=8             为文本输入设置 tab stops
:set window=n         设置文本窗口显示 n 行
:set number           显示行数
:set nonumber         取消显示行数
vi 的状态
选项                   作用
:.=                     打印当前行的行号
:=                     打印文件中的行数
ctrl+g             显示文件名、当前的行号、文件的总行数和文件位置的百分比
:l                 使用字母 “l” 来显示许多的特殊字符,如制表符和换行符
在文本中定位段落和放置标记
选项                      作用
{                         在第一列插入 { 来定义一个段落
[[                         回到段落的开头处
]]                         向前移到下一个段落的开头处
m(a-z)                用一个字母来标记当前位置,如用 mz 表示标记 z
‘(a-z)                将光标移动到指定的标记,如用 ‘z 表示移动到 z
在 vi 中连接行
选项                      作用
J                     将下一行连接到当前行的末尾
nJ                    连接后面 n 行
光标放置与屏幕调整
选项                      作用
H                     将光标移动到屏幕的顶行
nH                    将光标移动到屏幕顶行下的第 n 行
M                     将光标移动到屏幕的中间
L                     将光标移动到屏幕的底行
nL                    将光标移动到屏幕底行上的第 n 行
^e(ctrl+e)            将屏幕上滚一行
ctrl+y                将屏幕下滚一行
ctrl+u                将屏幕上滚半页
ctrl+d                将屏幕下滚半页
ctrl+b                将屏幕上滚一页
ctrl+f                将屏幕下滚一页
ctrl+l                重绘屏幕
z-return              将当前行置为屏幕的顶行
nz-return             将当前行下的第 n 行置为屏幕的顶行
z.                    将当前行置为屏幕的中央
nz.                   将当前行上的第 n 行置为屏幕的中央
z-                    将当前行置为屏幕的底行
nz-                   将当前行上的第 n 行置为屏幕的底行
vi 中的 shell 转义命令
选项                      作用
:!command             执行 shell 的 command 命令,如 :!ls
:!!                         执行前一个 shell 命令
:r!command            读取 command 命令的输入并插入,如 :r!ls 会先执行 ls,然后读入内容
:w!command            将当前已编辑文件作为 command 命令的标准输入并执行 command 命令,如 :w!grep all
:cd directory         将当前工作目录更改为 directory 所表示的目录
:sh                   将启动一个子 shell,使用 ^d(ctrl+d) 返回 vi
:so file              在 shell 程序 file 中读入和执行命令
vi 中的宏与缩写
(避免使用控制键和符号,不要使用字符 K、V、g、q、v、*、= 和功能键)
选项                      作用
:map key command_seq 定义一个键来运行 command_seq,如 :map e ea,无论什么时候都可以 e 移到一个字的末尾来追加文本
:map                  在状态行显示所有已定义的宏
:umap key             删除该键的宏
:ab string1 string2   定义一个缩写,使得当插入 string1 时,用 string2 替换 string1。当要插入文本时,键入 string1 然后按 Esc 键,系统就插入了 string2
:ab                   显示所有缩写
:una string           取消 string 的缩写
在 vi 中缩进文本
选项                     作用
ctrl+i或 tab     插入文本时,插入移动的宽度,移动宽度是事先定义好的
:set ai               打开自动缩进
:set sw=n             将移动宽度设置为 n 个字符
n>                   使 n 行都向右移动一个宽度,例如 3>> 就将接下来的三行每行都向右移动一个移动宽度

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104223/showart_2126523.html

39 Responses so far.

  1. quest bars says:
    Incredible! This blog looks just like my old one! It’s
    on a completely different topic but it has pretty
    much the same layout and design. Outstanding choice
    of colors!
  2. Quest Bars says:
    Keep this going please, great job!
  3. quest Bars says:
    Hello just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Chrome.
    I’m not sure if this is a formatting issue or something to do
    with internet browser compatibility but I figured I’d post to let you know.
    The layout look great though! Hope you get the problem solved soon. Kudos
  4. Thank you, I have recently been searching for info about this topic for a while and yours is the best I’ve came upon so
    far. However, what concerning the conclusion? Are you positive about the supply?
  5. Saved as a favorite, I really like your site!
  6. Hello There. I found your blog using msn. This is a really well written article.
    I will make sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll certainly return.
  7. Excellent post. I was checking continuously this blog and I’m impressed!
    Extremely useful information specifically the last part 🙂
    I care for such information a lot. I was seeking
    this certain information for a long time. Thank you and best of luck.
  8. I used to be able to find good information from
    your blog articles.
  9. Thanks for another magnificent post. The place else could anyone get that type of
    information in such an ideal way of writing? I’ve a presentation subsequent week, and I’m at the look for such info.
  10. hello there and thank you for your info – I have
    definitely picked up anything new from right here. I did however expertise a few technical issues using this web site, as I experienced to reload the website
    lots of times previous to I could get it to load correctly.
    I had been wondering if your web hosting is
    OK? Not that I’m complaining, but sluggish loading instances times
    will very frequently affect your placement in google and could damage your high quality score if advertising and marketing with Adwords.
    Anyway I’m adding this RSS to my e-mail and could look out for
    much more of your respective exciting content.

    Ensure that you update this again soon.

  11. Heya just wanted to give you a quick heads up and let you know a few of the images
    aren’t loading correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the same outcome.
  12. Hi friends, how is all, and what you would like to say concerning this piece of writing, in my view its really awesome in favor of
    me.
  13. Greetings! This is my 1st comment here so I just wanted
    to give a quick shout out and tell you I truly enjoy reading through your blog posts.
    Can you recommend any other blogs/websites/forums that deal with the same topics?
    Thank you so much!
  14. Wonderful, what a webpage it is! This web site presents useful data to us, keep
    it up.
  15. Fantastic beat ! I wish to apprentice at the same time as you amend your site,
    how could i subscribe for a weblog web site? The account
    helped me a appropriate deal. I were tiny bit familiar of this your broadcast offered vibrant clear idea
  16. Hi, i read your blog from time to time and i own a similar
    one and i was just curious if you get a lot of spam feedback?

    If so how do you prevent it, any plugin or anything you can recommend?
    I get so much lately it’s driving me crazy so any support is very much appreciated.

  17. This is a topic that is near to my heart…
    Cheers! Exactly where are your contact details though?
  18. Spot on with this write-up, I seriously believe this site needs a
    lot more attention. I’ll probably be returning to read more,
    thanks for the information!
  19. I’ll right away take hold of your rss feed as
    I can not in finding your email subscription hyperlink or newsletter service.
    Do you’ve any? Please let me recognize in order that I could subscribe.
    Thanks.
  20. Wonderful post however , I was wanting to know if you could write a litte more
    on this topic? I’d be very thankful if you could elaborate a little bit further.

    Bless you!

  21. This is the perfect site for anybody who wishes to understand this topic.
    You realize so much its almost tough to argue with you
    (not that I actually would want to…HaHa). You definitely put a brand new
    spin on a topic that has been discussed for a long time.

    Great stuff, just excellent!

  22. Aw, this was an extremely good post. Spending some time and actual effort
    to create a great article… but what can I say… I procrastinate a lot
    and never seem to get anything done.
  23. What a information of un-ambiguity and preserveness of precious knowledge concerning unpredicted
    emotions.
  24. Hey very interesting blog!
  25. Hi there to every one, it’s genuinely a fastidious
    for me to pay a visit this web site, it includes important Information.
  26. An fascinating dialogue is value comment. I think that you must write more on this matter, it might not be a taboo subject but usually people are not sufficient to speak on such topics. To the next. Cheers
  27. Brysen says:
    This is exactly what I was looking for. Thanks for wrtgini!
  28. Jera says:
    If your arteclis are always this helpful, “I’ll be back.”

Leave a Reply to plenty of fish dating site of free dating Cancel reply