vim(vi improved)是Unix/Linux环境下最为普遍的文本编辑器,被广泛应用于Linux/Unix编程、系统管理中。如果被合理的配置(写配置文件,增加高亮、缩进的设置),VI还是一个合格的IDE。当然,现在有很多的Unix文本编辑器具有,或者超越了VI的功能,但是,作为一种Unix的基本配置软件,VI的使用依然很普遍。 笔者今天在公司系统学习了VIM的基本命令,现整理于下(为了输入方便,命令的注解将使用英文,日后如有闲暇,将转移为中文):
在命令行提示符下,输入vi test.txt或vim test.txt,即可打开(若存在,如不存在则新建)test.txt。进入之后为command mode,不可以输入文本。输入I, i, A, a, O, o可以进入Input mode编辑文本;在Input mode中按Esc可以进入Command mode。
I: Engage insert mode, the cursor will be positioned at the beginning of the current line.
i: Engage insert mode, the cursor will be positioned one character after the current position.
A: Engage append mode, the cursor will be positioned at the end of the current line.
a: Engage append mode, the cursor will be positioned after the current position.
O: Engage insert mode, a new line will be added before the current line.
o: Engage insert mode, a new line will be added after the current line.
在Command mode下,有如下基本命令(Note: Linux is case sensitive, so the case(Hi/Low) of the commands matters):
(When I mention "current line", I actually mean the line where the cursor is currently positioned)
dd: Delete the current line.
D: Delete the content from the current cursor position to the end of the line.
(n)dd: Where (n) represents numbers like 10dd. (All the (n) in this article means the same) Deletes n line from the current line.
yy: copy the current line to the buffer.
(n)yy: copy n lines from the current line to the buffer.
cc: cut the current line to the buffer.
(n)cc: cut n lines from the current line to the buffer.
p: paste the content of buffer after the current line.
P: paste the content of buffer before the current line.
u: Undo
(ctrl-r: Redo)
Ctrl-r is not redo, tested on HP unix.
Later I will post the Redo command once I know.
(VI/VIM allows only one step of undo/redo, it's like notepad, but not like winword)
h: Move the cursor left by one character.
l(the letter l, lower case, not number 1): Move the cursor right by one character.
j: Move the cursor up by one character.
k: Move the cursor down by one character.
w(lower case): move the cursor to the next word(word by word).
0(zero): move the cursor to the beginning of the current line(like Home in notepad).
$: move the cursor to the end of the current line(like End in notepad).
G: move the cursor to the end of the document.
ctrl-f: move forward for 1 page.
ctrl-b: move backward for 1 page.
:1(column, then the number 1): move the cursor to the beginning(the beginning of the first line) of the document.
:(n): move the cursor to the beginning of the nth line.
/keyword: search the document for the keyword and highlight them. like /tcs, /china to search for the string "tcs", "china". Then press n to move the cursor to the next one matching pattern, press N to move it to the last one matching pattern.
:%s/string1/string2/g: replace all "string1" with "string2". /g means globally. If no /g was specified, then only the first matching pattern in each line will be replaced. The editor will not look further to find more patterns.
r: replace the letter at the current cursor position with another.
R: replace(overwrite) everything from the current cursor position to the end of the current line.
cw: change(overwrite) the current word. The rest of the current line remains untouched.
:w: write the document to the current file.
:w filename1: write the document to another file, with the name "filename1".
:q: quit.(will prompt if the content is changed but not saved.)
:q!: force quit.(will not prompt and quit directly, thus will lose all changes made since last save.)
:wq: save and quit.
以上即为VI基本操作的命令集合。涵盖了文件打开、保存、退出,剪切、复制、粘贴,文本查找、替换,指针(光标)的定位、移动等基本的编辑器使用方法。
如有更高阶的操作在Training中有介绍,将会另贴文出来。
Simon Jo