vi 基本命令总结

终于下定决心学vi命令啦??
在这里插入图片描述

几点说明与建议:

  • 学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常难学,但是却不可思议地好用。
  • 我猜你的学习步骤是这样的:1.先存活;2.感觉良好;3.觉得更好,更强,更快;4.具备使用VIM的超能力。
  • vim的学习曲线相当陡峭,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的。So,先看看我这个简短的版本。
  • vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim)。vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率。vi是“visual interface”的缩写,vim是vi IMproved(增强版的vi)。

Let’s go!

进入命令行模式

命令 作用
[Esc] 退出编辑模式(editing mode)此处将键盘键解释执行为一条命令

移动光标(命令行模式下)

命令 作用 你要的英语
h 左移光标 move the cursor left
l 右移光标 move the cursor right
j 下移光标 move the cursor down
k 上移光标 move the cursor up
[Ctrl] f 将光标向前移动一页 move the cursor one page forward
[Ctrl] b 将光标向后移动一页 move the cursor one page backward
^ 在当前行将光标移动到第一个非空字符处 move cursor to the first non-white character in the current line
$ 将光标移动到当前行的结尾处 move the cursor to the end of the current line
G 到文件的最后一行 go to the last line in the file
nG 到文件的第n行 go to line number n
[Ctrl] G 显示当前文件的名字和光标的位置 display the name of the current file and the cursor position in it

进入编辑模式(editing mode)

命令 作用 你要的英语
i 在光标前插入文本(并进入编辑模式) insert new text before the cursor
a 在光标后添加文本(并进入编辑模式) append new text after the cursor
o(小写o) 在当前行之后开启一个新行(并进入编辑模式) start to edit a new line after the current one
O(大写O) 在当前行之前开启一个新行(并进入编辑模式) start to edit a new line before the current one

替换字符、某一行或单词

命令 作用 你要的英语
r 替换当前字符(不会进入编辑模式) replace the current character (does not enter edit mode).
s 进入编辑模式用几个其他字符代替当前字符 enter edit mode and substitute the current character by several ones
cw 进入编辑模式改变单词在光标之后的部分 enter edit mode and change the word after the cursor
C(大写) 进入编辑模式改变当前行在光标之后的部分 enter edit mode and Change the rest of the line after the cursor

复制和粘贴

命令 作用 你要的英语
yy 复制当前行到 复制/粘贴缓存中 copy (yank) the current line to the copy/paste buffer
p(小写p) 在当前行之后粘贴 复制/粘贴缓存中的内容 paste the copy/paste buffer after the current line
P(大写P) 在当前行之前粘贴 复制/粘贴缓存中的内容 Paste the copy/paste buffer before the current line

删除字符、单词或某一行

注: 所有被删除的字符,单词或者某一行全都被复制到 复制/粘贴缓存中

命令 作用 你要的英语
x 删除在光标位置的字符
dw 删除当前单词光标之后的部分
D 删除当前行光标之后剩余的部分
dd 删除当前行

重复上一个操作

命令 作用 你要的英语
. 重复上一个插入、替换或删除指令

查找字符串

命令 作用 你要的英语
/string 找到光标之后第一次出现的字符串
?string 找到光标之前第一次出现的字符串
n 找到上次搜索中的下一个匹配项

替换字符串

当然也可以手动完成,搜索和替换一次,然后使用n(下次出现)和 . (重复上次操作)

命令 作用 你要的英语
n,ps/str1/str2/g 在行号n和p之间,用str2替换str1
1,$s/str1/str2/g 在整个文件($:last line)中,用str2替换所有出现的str1

多次应用命令 - 举例

命令 作用 你要的英语
5j 光标向下移动5行
30dd 删除30行
4cw 从光标位置修改4个单词
1G 光标到文件的第一行

杂项

命令 作用 你要的英语
[Ctrl] l 重新绘制屏幕
J 将下一行添加到本行
u 取消上一次操作

退出并保存

命令 作用 你要的英语
ZZ 保存当前文件并退出vi
:w 写入(保存)到当前的文件中
:w file 写入(保存)到名为file的文件中
:q! 退出vi不保存任何修改
发布了29 篇原创文章 · 获赞 8 · 访问量 2710

猜你喜欢

转载自blog.csdn.net/HaoZiHuang/article/details/93385742