- 工信部备案号 滇ICP备05000110号-1
- 滇公网安备53011102001527号
- 增值电信业务经营许可证 B1.B2-20181647、滇B1.B2-20190004
- 云南互联网协会理事单位
- 安全联盟认证网站身份V标记
- 域名注册服务机构许可:滇D3-20230001
- 代理域名注册服务机构:新网数码
- CN域名投诉举报处理平台:电话:010-58813000、邮箱:service@cnnic.cn
基础
| :e filename | Open filename for edition |
| :w | Save file |
| :q | Exit Vim |
| :q! | Quit without saving |
| :x | Write file (if changes has been made) and exit |
| :sav filename | Saves 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 Arrow | move the cursor up one line |
| j or Down Arrow | move the cursor down one line |
| e | move the cursor to the end of the word |
| b | move the cursor to the begining of the word |
| 0 | move the cursor to the begining of the line |
| G | move the cursor to the end of the line |
| gg | move the cursor to the begining of the file |
| L | move the cursor to the end of the file |
| :59 | move 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 |
剪切、复制和粘贴
| y | Copy the selected text to clipboard |
| p | Paste clipboard contents |
| dd | Cut current line |
| yy | Copy current line |
| y$ | Copy to end of line |
| D | Cut to end of line |
搜索
| /word | Search word from top to bottom |
| ?word | Search word from bottom to top |
| * | Search the word under cursor |
| /cstring | Search STRING or string, case insensitive |
| /jo[ha]n | Search john or joan |
| /< the | Search 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|joe | Search fred or joe |
| / | Search exactly 4 digits |
| /^ {3} | Find 3 empty lines |
| :bufdo /searchstr/ | Search in all open files |
| bufdo %s/something/somethingelse/g | Search something in all the open buffers and replace it with somethingelse |
替换
| :%s/old/new/g | replace all occurences of old by new in file |
| :%s/onward/forward/gi | replace onward by forward, case unsensitive |
| :%s/old/new/gc | 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/^M//g | delete DOS carriage returns (^M) |
| :%s///g | Transform DOS carriage returns in returns |
| :%s#<[^>]+>##g | delete HTML tags but keeps text |
| :%s/^(.*) 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 |
大小写
| Vu | Lowercase line |
| VU | Uppercase line |
| g~~ | Invert case |
| vEU | Switch word to uppercase |
| vE~ | Modify word case |
| ggguG | Set all text to lowercase |
| gggUG | Set all text to uppercase |
| :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 |
读写文件
| :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 |
文件浏览器
| :e . | Open integrated file explorer |
| :Sex | Split window and open integrated file explorer |
| :Sex! | Same as :Sex but split window vertically |
| :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 |
和 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 |
对齐
| :%!fmt | Align all lines |
| !}fmt | Align all lines at the current position |
| 5!!fmt | Align the next 5 lines |
Tabs/Windows
| :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 |
| :new abc.txt | Edit abc.txt in new window |
分屏显示
| :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_ | 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 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 |
自动完成
| 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
| m {a-z} | Marks current position as {a-z} |
| ' {a-z} | Move to position {a-z} |
| '' | Move to previous position |
缩写
| :ab mail mail@provider.org | Define mail as abbreviation of mail@provider.org |
文本缩进
| :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 |
| =% | Indent the code between parenthesis |
| 1GVG= | Indent the whole file |
语法高亮
| :syntax on | Turn on syntax highlighting |
| :syntax off | Turn off syntax highlighting |
| :set syntax=perl | Force syntax highlighting |
三.命令行模式操作
1.进入插入模式
按“i”:从光标当前位置开始输入文件。
按“I”:在光标所在行的行首插入。
按“a”:从目前光标所在位置的下一个位置开始输入文字。
按“A”:在光标所在行的行末插入。
按“o”:在下面插入一行
按“O”:在上面插入一行。
按“s”:删除光标后的一个字符,然后进入插入模式。
按“S”:删除光标所在的行,然后进入插入模式。
3.移动光标
“h”、“j”、“k”、“l”分别控制光标左、下、上、右移一格。
“w”:光标跳到下个字的开头。
“e”:光标跳到下个字的字尾。
“b”:光标回到上个字的开头。
“nl”:光标移动该行的第n个位置,例如:“5l”表示移动到该行的第5个字符。
“ctrl+b”:屏幕往后移动一页。
“ctrl+f”:屏幕往前移动一页。
“ctrl+u”:屏幕往后移动半页。
“ctrl+d”:屏幕往前移动半页。
—数字“0”:移动到文本的开头。
“G”:移动到文件的最后。
—“$”:移动到光标所在行的行尾。
“^”:移动到光标所在行的行首。
4.删除文字
“x”:每按一次,删除光?所在位置的后面一个字符。
“nx”:例如:“6x”表示删除光标所在位置后面6个字符。
“X”:每按一次,删除光标所在位置的前面一个字符。
“dd”:删除光标所在行。
“ndd”:从光标所在行开始删除n行。例如:“4dd”表示删除从光标所在行开始的4行字符。
5.复制
“yw”:将光标所在之处到字尾的字符复制到缓冲区中。
“nyw”:复制n个字到缓冲区。
“yy”:复制光标所在行到缓冲区。
“nyy”:例如:“6yy”表示复制从光标所在行开始6行字符。
“p”:将缓冲区内的字符写到光标所在位置。
8.更改
“cw”:更改光标所在处的字到字尾处。
“cnw”:例如:“c3w”表示更改3个字。
9.跳至指定的行
“ctrl+g”:列出光标所在行的行号。
“nG”:例如:“15G”,表示移动光标到该文件的第15行行首。
其他:
“ZZ”:存盘退出
“ZQ”:不存盘退出
“r”:替换光标所在处的字符。
“R”:替换光标所到处的字符,直到按下“ESC”键为止。
“u”:撤销上一次操作
四.末行模式操作
在使用末行模式之前,请记住先按“ESC”键确定已经处于命令行模式后,再按冒号“:”即可进入末行模式。
“set nu”:列出行号
“set nonu”:取消列出行号
“set ic”:搜索时忽略大小写
“set noic”:取消在搜索时忽略大小写。
“n”:跳到文件中的某一行,“n”表示一个数字,如输入数字15,再回车就会跳到文本的第15行。
“!cmd”:运行shell命令cmd。
查找字符:
“/关键字”:先按“/”,再输入想查找的字符,如果第一次查找的关键字不是想要的,可?一直按“n”,往后查找一个关键字。
“? 关键字”:先按“?”键,再输入想查找的字符,如果第一次查找的关键字不是想要的,可以一直按“?”,往后查找一个关键字。
替换字符:
“s /SPARCH/replace/g”:把当前光标所处的行中的SEARCH单词替换成REPLACE,并把所有SEARCH高亮显示。
“%s /SPARCH/REPLACE”:把文档中所有SEARCH替换成REPLACE。
“n1,n2 s /SPARCH/replace/g”:n1、n2表示数字,表示从n1行到n2行,把SEARCH替换成REPLACE。
五.命令行内容说明
| 命令行模式:移动光标的方法 | |
| h或向左方向键(←) | 光标向左移动一个字符 |
| j或向下方向键(↓) | 光标向下移动一个字符 |
| k或向上方向键(↑) | 光标向上移动一个字符 |
| l或向右方向键(→) | 光标向右移动一个字符 |
| 如果想要进行多次移动的话,例如;向下移动30行,可以使用“30j”或“30↓”的组合键,即加上想要进行的次数(数字)后,操作即可。 | |
| [Ctrl]+[f] | 屏幕“向下”移动一页,相当于[Page Down]按键 |
| [Ctrl]+[b] | 屏幕“向上”移动一页,相当于[Page Up]按键 |
| [Ctrl]+[d] | 屏幕“向下”移动半页 |
| [Ctrl]+[u] | 屏幕“向上”移动半页 |
| 命令行模式:移动光标的方法 | |
| + | 光标移动到非空格符的下一行 |
| - | 光标移动到非空格符的上一行 |
| n | n表示“数字”,例如20.按下数字后再按空格键,光标会向右移动这一行n个字符。例如20 |
| 0 | 这是数字“0”:移动到这一行的最前面字符处(常用) |
| $ | 移动到这一行的最后面字符处(常用) |
| H | 光标移动到这个屏幕的最上方哪一行 |
| M | 光标移动到这个屏幕的中央哪一行 |
| L | 光标移动到这个屏幕的最下方哪一行 |
| G | 移动到这个文件的最后一行(常用) |
| nG | n为?字。移动到这个文件的第n行。例如20G则会移动到这个文件的第20行(可配合:set nu) |
| gg | 移动到这个文件的第一行,相当于1G(常用) |
| n | n为数字。光标向下移动n行(常用) |
| 命令行模式:搜索与替换 | |
| /word | 从光标位置开始,向下寻找一个名为word的字符串。例如要在文件内搜索vbird这个字符串,就输入/vbird即可(常用) |
| ?word | 从光标位置开始,向上寻找一个名为word的字符串 |
| n | n是英文按键。表示“重复前一个搜索的动作”。举例来说,如果刚刚执行/vbird去向下搜索vbird字符串,则按下n后,会向下继续搜索下?个名称为vbird的字符串。如果是执行?vbird的话,那么按下n,则会向上继续搜索名称为vbird的字符串 |
| N | 这个N是英文按键。与n刚好相反,为“反向”进行前一个搜索操作。例如/vbird后,按下N则表示“向上”搜索vbird |
| 命令行模式:搜索与替换 | |
| :n1、n2s/word1/word2/g | n1与n2为数字。在第n1与n2行之间寻找word1这个字符串,并将该字符串替换为word2。举例来说,在100到200行之间搜索vbird并替换为VBIRD则:“:100、200s/vbird/VBIRD/g”(常用) |
| :1、$s/word1/word2/g | 从第一行到最后一行寻找word1字符串,并将该字符串替换为word2(常用) |
| :1、$s/word1/word2/gc | 从第一行到最后一行寻找word1字符串,并将该字符串替换为word2。且在替换前显示提示符给用户确认(conform)是否需要替换(常用) |
| 命令行模式:删除、复制与粘贴 | |
| p,P | p为将已复制的数据粘贴到光标的下一行,P则为贴在光标上一行。举例来说,当前光标在第20行,且已经复制了10行数据。则按下p后,那10行数据会粘在原来的20行之后,?由21行开始贴。但如果是按下P,那么原来的第20行会被变成30行(常用) |
| J | 将光标所在行与下一列的数据结合成同一行 |
| c | 重复删除多个数据,例如向下删除10行,[10cj] |
| u | 复原前一个操作(常用) |
| [Ctrl]+r | 重做上一个操作(常用) |
| U与[Ctrl]+r是很常用的命令。一个是复原,另一个则是重做一次。利用这两个功能按键,编辑起来就得心应手。 | |
| 命令行模式:删除、复制与粘贴 | |
| . | 这就是不数点。意思是重复前一个动作。如果想重复删除、重复粘贴,按下小数点“.”就可以(常用) |
| 插入模式 | |
| i、I | 插入:在当前光标所在处插入输入文字,已存在的文字会向后退;其中,i为“从当前光标所在处插入”,I为“在当前所在行的第一个非空格符处开始插入”(常用) |
| a、A | a为“从当前光标所在的下一个字符处开始插入”,A为“从光标所在行的最?一个字符处开始插入”(常用) |
| o、O | 这是英文字母o的大小写。o为“在当前光标所在的下一行处插入新的一行”,O为“在当前光标所在处的上一行插入新的一行”(常用) |
| r、R | 替换:r会替换光标所在的那一个字符;R会一直替换光标所在的文字,直到按下Esc键为止(常用) |
| 使用上面这些按键时,在vi画面的左下角处会出现“—INSERT--”或“—replace--”的字样。通过名称就知道是什么操作。特别注意,上面也提过了,想在文件中输入字符时,一定要在左下角处看到INSERT/ REPLACE才能输入。 | |
| Esc | 退出插入模式,回到命令行模式中(常用) |
| 末行命令模式 | |
| :w | 将编辑的数据写入硬盘文件中(常用) |
| :w! | 若文件属性为“只读”时,强制写入该文件。不过,到底能不能写入,与文件权限有关 |
| :q | 离开vi(常用) |
| :q! | 若曾修改过文件,又不想存储,使用!为强制离开不存储文件 |
| 注意一下,那个感叹号(!)在vi当中,常常具有“强制”的意思。 | |
| :wq | 存储后离开,若为:wq!则为强制存储后离开(常用) |
| :e! | 将文件还原到最原始的状态 |
| ZZ | 若文件没有更改,则不存储离开,若文件已经更改,则存储后离开 |
| :w[filename] | 将编辑的数据存储成另一个文件(类似另存新文件) |
| :r[filename] | 在编辑的数据中,读入另一个文件的数据。即将“filename”这个文件内容加到光标所在行的后面 |
| :n1、n2 w[filename] | 将n1到n2的内容存储成filename文件 |
| :!command | 暂时离开vi到命令模式下执行command的显示结果。例如,“:! ls /home”,即可在vi中查看/home中以ls输出的文件信息 |
| :set nu | 显示行号,设置之后,会在每一行的前缀显示该行的行号 |
| :set nonu | 与set nu相反,为取消行号 |
特别注意,在vi中,“数字”是很有意义的。数字通常表示重复做几次的意思。也有可能表示要去哪里的意思。举例来说,要删除50行,则是用“50dd”。数字加在动作之前。要向下移动20行,使用“20j”或“20↓”即可。
掌握这些命令就很不错了,因为常用到的命令也可能只有一半。通常vi的命令除了上面笔者注明的常用的几个外,其他不用背,可以做一张简单的命令表,当有问题时就可以马上查询。

售前咨询
售后咨询
备案咨询
二维码

TOP