VI is a text editor with a plain character interface as standard under Linux/unix. Because the mouse function is not supported, there is no graphical interface, the related operations are to be done through the keyboard instructions, need to memorize a large number of commands. So many people do not like it, but at the same time because the keyboard is often faster than the mouse, once the proficiency of the use of a very smooth feeling, but also some people like it very much.
Whether you like it or not, VI is the standard Linux editor, many times you also only this one editor available, if you want to do a Linux coder, familiar with VI is very necessary.
PS: Because of the very many commands in VI, this article mainly introduces some basic commands. And even though many of these basic commands are less commonly used, I've shown these commands in gray text, and beginners don't have to pay much attention to these gray commands.
Two modes of operation
As mentioned earlier, because VI does not have a graphical interface, such as page flipping, moving the cursor, save, exit and other operations must be done with the help of the keyboard. In addition, VI also provides a number of shortcut keys to speed up the operation, therefore, vi in the editing mode, but also introduced a new mode of operation-command mode.
编辑模式: 在此模式下,输入可见字符时和传统Windows环境下输入字符的功能相同。按Esc键可切换值命令模式。命令模式: 在此模式下,输入的可见字符对应的是命令。当插入命令执行后进入编辑模式。
PS: Some articles also listed a last line mode, this can be counted as a special command mode, but the command will be displayed in the final row, and need to hit enter to execute. These commands usually start with special characters such as:/?$.
Pattern query: To determine which mode is currently in, the last line (the status bar) can be used to determine: If in edit mode, the following two states are displayed.
Switching between modes: In edit mode, the ESC key can be used to enter command mode, and in command mode, some insert commands are entered into edit mode. If you do not know which mode is currently in, press ESC to enter command mode and press Insert to enter edit mode.
Start VI
VI has a number of startup parameters, but the usual way to use the following: VI filename
If the file in the parameter exists, the file is opened, and if the file in the parameter does not exist, the file is created (not created immediately, but saved).
Insert text
Just enter VI, is out of command mode, this is not input text, so need to enter the insertion mode. There are several commands to enter the Insert mode:
i 从目前光标所在之处插入文字a 从目前光标所在下一个字符插入文字o 往下插入一个空行O 往上插入一个空行
After entering these commands, you enter edit mode and you can see the current insert status through the last line at the bottom of the screen:
You can toggle the Insert state and the Overtype state (as in the image interface) with the INSERT key.
In addition, in command mode, you can also enter edit mode (insert state) with the Insert key, but usually the four commands described above go into edit mode.
Leaving and saving files
In VI, the command to leave and save the file has these several (remember to enter command mode before use):
:w 保存:w filename 另存为:wq 保存并退出 [常用]:q! 强制退出SHIFT+zz 保存并退出,功能同:wq [常用]
Move cursor
In VI, moving the cursor is not commonly used in our game wasd, but H, J, K, L, respectively, control the left, bottom, upper and right of the cursor, respectively, distributed in the right hand four fingers, novice needs a period of time to adapt. The use is very frequent and requires mastery.
Some other cursor control commands are as follows:
Ctrl+B:屏幕往后移动一页。Ctrl+F:屏幕往前移动一页。Ctrl+U:屏幕往后移动半页。Ctrl+D:屏幕往前移动半页。gg:移动文章的开头。G:移动到文章的最后。w:光标跳到下个word的开头。e:光标跳到下个word的字尾。b:光标回到上个word的开头。$:移到光标所在行的行尾。^:移到该行第一个非空白的字符。0:移到该行的开头位置。#:移到该行的第#个位置,例:51、121。
In addition, there are several lines of operations related to the command set to introduce
:set nu 显示行号:set nonu 隐藏行号:# 移动到第#行,如:15Ctrl+G: 在状态栏显示当前进度#G: 移动到第#行,例:15G
Delete text
x:删除光标所在位置的后面一个字符。#x:例如,6x 表删除光标所在位置的后面6个字符。X:大字的X,每按一次删除光标所在位置的前面一个字符。#X:例如,20X 表删除光标所在位置的前面20个字符。dd:删除光标所在行。#dd:例如,6dd表删除从光标所在的该行往下数6行之文字。
Copy and paste
I am not very accustomed to the copy and paste provided by VI, because there is no highlight, it is not very convenient to use. Most of the usual is the use of the Terminal tool copy and paste function, so here the command I have added ash, interested can see.
yw:将光标所在处到字尾的字符复制到缓冲区中。p:将缓冲区内的字符粘贴到光标所在位置(指令‘yw‘与‘p必须搭配使用)。yy:复制光标所在行。[超常用]p:复制单行到您想粘贴之处。(指令‘yy‘与‘p‘必须搭配使用)#yy:如:6yy表示拷贝从光标所在的该行往下数6行之文字。[常用]p: 复制多行到您想粘贴之处。(指令‘#yy‘与‘p‘必须搭配使用)ayy:将复制行放入buffer a, vi提供buffer功能,可将常用的数据存在bufferap:将放在buffer a的数据粘贴。b3yy:将三行数据存入buffer b。b3p:将存在buffer b的资料粘贴
Undo and Redo
Undo and redo are very useful commands in any editor:
u:假如您误操作一个指令,可以马上按u,回复到上一个操作。.: 重复执行上一次的指令
Find and replace
Find and replace the syntax notes complex, first simple introduction to find the syntax:
/pattern<Enter> :向下查找pattern匹配字符串?pattern<Enter> :向上查找pattern匹配字符串
After using the Find command, use the following two keys to quickly find:
n:按照同一方向继续查找N:按照反方向查找
As for the replacement of the syntax, confined to space, here is not introduced, to a link for your reference: http://blog.csdn.net/lanxinju/article/details/5731843.
VI Support for programming
VI to programming or provide some support. For example, it is supported by parentheses pairing, syntax highlighting.
However, because the parsing function is not supported, as the coding tool and professional code editor such as Sourceinsight, VisualStudio and so on, there is a big gap. Personally feel that doing small projects and temporarily modifying code is OK, but doing large projects is a bit of a struggle.
From for notes (Wiz)
Linux Advanced Programming--01.VI commands