VIM命令—Vi IMproved, a programmers text editor文本编辑

1、a、i、o等键:输入模式
2、:键:末行模式
3、R键:替换模式(replace)
4、v键:可视模式(visual)
1、i 当前位置插入内容(insert)
2、a (append)
1、:q =如果未对文件做改动则退出 quit
2、:q! =放弃保存并退出
3、:wq =保存当前文件并退出 write quit
4、:wq! =强制保存当前文件并退出
5、😡 =保存当前文件并退出
6、:x! =强制保存当前文件并退出
7、:w =保存
8、:w abc.txt =另存为 (可以使用绝对路径)
9、ZZ =在命令模式下使用,退出并且保存
10、:X =设置密码。退出最好使用:wq

在末行模式下使用
:set nu 显示行号(:set number)
:set nonu 取消行号
:50 调到第50行
u是撤销(undo)
ctrl+r 恢复 recover redo
1、小v 可视模式 visual
2、大V 可视行模式 VISUAL line
3、ctrl+v 可视块模式 VISUAL block
1 2 3 4 5 6 7 8 |
<span style="font-size: 18px;"><span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);">、查找功能(高亮效果) :</span><span style="color: rgba(0, 0, 255, 1);">set</span><span style="color: rgba(0, 0, 0, 1);"> nohlsearch 取消高亮效果 (high light search) :</span><span style="color: rgba(0, 0, 255, 1);">set</span><span style="color: rgba(0, 0, 0, 1);"> hlsearch 开启高亮效果 示例: </span>/<span style="color: rgba(0, 0, 0, 1);">word 从上而下在文件中查找字符串“word” </span>?<span style="color: rgba(0, 0, 0, 1);">word 从下而上在文件中查找字符串“word” n 定位下一个匹配的被查找字符串 N 定位上一个匹配的被查找字符串</span></span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="font-size: 18px;"><span style="color: rgba(128, 0, 128, 1);">2</span><span style="color: rgba(0, 0, 0, 1);">、替换substitute 例1:将6到8行所有的old替换为new :</span><span style="color: rgba(128, 0, 128, 1);">6</span>,8s/old/<span style="color: rgba(0, 0, 255, 1);">new</span>/<span style="color: rgba(0, 0, 0, 1);">g </span><span style="color: rgba(0, 0, 0, 1);">例2:将整篇文章里的old替换为new :</span>%s/old/<span style="color: rgba(0, 0, 255, 1);">new</span>/<span style="color: rgba(0, 0, 0, 1);">g </span><span style="color: rgba(0, 0, 0, 1);"> 例3:替换的时候需要确认,将old替换为new :</span>%s/old/<span style="color: rgba(0, 0, 255, 1);">new</span>/<span style="color: rgba(0, 0, 0, 1);">gc <span style="color: rgba(255, 0, 0, 1);">y是yes :替换 n是no :不替换 a是all :全部替换 q是quit :退出不替换 l是line :替换整行的</span></span></span> |
1 2 3 4 5 6 |
<span style="font-size: 18px;"><span style="color: rgba(128, 0, 128, 1);">3</span><span style="color: rgba(0, 0, 0, 1);">、末行模式下对<span style="color: rgba(255, 0, 0, 1);">其他文件</span>的操作 例1:打开新文件进行编辑 :e </span>~<span style="color: rgba(0, 0, 0, 1);">instal.log 例2:在当前文件中读入其他文件内容 :r </span>/etc/filesystems</span> |
1 2 3 4 5 |
<span style="font-size: 18px;"><span style="color: rgba(128, 0, 128, 1);">4</span><span style="color: rgba(0, 0, 0, 1);">、终止文件操作 例1:撤销对文件的所有操作,重新开始 :e</span>!<span style="color: rgba(0, 0, 0, 1);"> 例2:把当前文件另存为</span>/<span style="color: rgba(0, 0, 0, 1);">tmp目录下的hello.txt :w </span>/tmp/hello.txt</span> |
1 2 3 4 5 6 7 8 9 10 11 |
<span style="font-size: 18px;"><span style="color: rgba(128, 0, 128, 1);">5</span><span style="color: rgba(0, 0, 0, 1);">、多文件操作 vim </span><span style="color: rgba(128, 0, 128, 1);">1</span>.txt <span style="color: rgba(128, 0, 128, 1);">2</span>.txt <span style="color: rgba(128, 0, 128, 1);">3</span><span style="color: rgba(0, 0, 0, 1);">.txt :n 编辑下一个文件 :N 编辑上一个文件 :file 列出vim打开的文件名 </span><span style="color: rgba(128, 0, 128, 1);">1</span> <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">bajie</span><span style="color: rgba(128, 0, 0, 1);">"</span> line <span style="color: rgba(128, 0, 128, 1);">1</span> <span style="color: rgba(128, 0, 128, 1);">2</span> %a <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">wukong</span><span style="color: rgba(128, 0, 0, 1);">"</span> line <span style="color: rgba(128, 0, 128, 1);">1</span> <span style="color: rgba(128, 0, 128, 1);">3</span> # <span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">shashidi</span><span style="color: rgba(128, 0, 0, 1);">"</span> line <span style="color: rgba(128, 0, 128, 1);">1</span><span style="color: rgba(0, 0, 0, 1);"> :last 最后一个 :first 第1个</span></span> |
:syntax enable 开启语法颜色显示效果
:syntax off 关闭语法颜色显示效果
:set hlsearch 开启查找文字的高亮效果 (high light)
:set nohlsearch 关闭查找文字的高亮效果
:set cursorline 显示下划线的效果(光标所在行)
:set cursorcolumn 显示光标所在的列
A:启动vim编辑器的时候需要运行的命令,可以放到vim的配置文件里
局部配置文件:/root/.vimrc
全局配置文件:/etc/vimrc
###配置文件里面的命令,都会是写入vim末行模式的命令(类似于预操作)
例如:
shell> cat /root/.vimrc
set cursorline
set cursorcolumn
set nu
set tabstop=4 #设置vim的tab键的宽度(默认情况下是8个空格)
然后再使用vim的时候,会自动开启上面的功能
注意:如果上面的配置不需要,建议删除,不要在前面加#,会导致vim启动的时候报错