Home >Backend Development >PHP Problem >What is the function of linux file editing command vi?

What is the function of linux file editing command vi?

晓曦&sea
晓曦&seaOriginal
2020-07-17 17:35:574614browse

The vi command is used to enter vi, the most versatile full-screen plain text editor in the UNIX operating system. The vi editor supports edit mode and command mode. In the edit mode, you can complete the text editing function, and in the command mode, you can complete the file operation commands. To use the vi editor correctly, you must be proficient in switching between the two modes.

What is the function of linux file editing command vi?

vi command

vi command is in UNIX operating systems and UNIX-like operating systems The most versatile full-screen plain text editor. The vi editor in Linux is called vim, which is an enhanced version of vi (vi Improved). It is fully compatible with the vi editor and implements many enhanced functions.

vi editor supports edit mode and command mode. In edit mode, you can complete text editing functions, and in command mode, you can complete file operation commands. To use the vi editor correctly, you must be proficient in both. Mode switching. By default, the vi editor automatically enters command mode after opening it. Use the "esc" key to switch from edit mode to command mode, and use the "A", "a", "O", "o", "I", and "i" keys to switch from command mode to edit mode.

The vi editor provides a wealth of built-in commands. Some built-in commands can be completed using keyboard key combinations, and some built-in commands need to be entered starting with a colon ":". Commonly used built-in commands are as follows:

Ctrl+u:向文件首翻半屏;
Ctrl+d:向文件尾翻半屏;
Ctrl+f:向文件尾翻一屏;
Ctrl+b:向文件首翻一屏;
Esc:从编辑模式切换到命令模式;
ZZ:命令模式下保存当前文件所做的修改后退出vi;
:行号:光标跳转到指定行的行首;
:$:光标跳转到最后一行的行首;
x或X:删除一个字符,x删除光标后的,而X删除光标前的;
D:删除从当前光标到光标所在行尾的全部字符;dd:删除光标行正行内容;
ndd:删除当前行及其后n-1行;
nyy:将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字;
p:粘贴文本操作,用于将缓存区的内容粘贴到当前光标所在位置的下方;
P:粘贴文本操作,用于将缓存区的内容粘贴到当前光标所在位置的上方;
/字符串:文本查找操作,用于从当前光标所在位置开始向文件尾部查找指定字符串的内容,查找的字符串会被加亮显示;
?name:文本查找操作,用于从当前光标所在位置开始向文件头部查找指定字符串的内容,查找的字符串会被加亮显示;
a,bs/F/T:替换文本操作,用于在第a行到第b行之间,将F字符串换成T字符串。其中,“s/”表示进行替换操作;
a:在当前字符后添加文本;
A:在行末添加文本;
i:在当前字符前插入文本;
I:在行首插入文本;
o:在当前行后面插入一空行;
O:在当前行前面插入一空行;
:wq:在命令模式下,执行存盘退出操作;
:w:在命令模式下,执行存盘操作;
:w!:在命令模式下,执行强制存盘操作;
:q:在命令模式下,执行退出vi操作;
:q!:在命令模式下,执行强制退出vi操作;
:e文件名:在命令模式下,打开并编辑指定名称的文件;
:n:在命令模式下,如果同时打开多个文件,则继续编辑下一个文件;
:f:在命令模式下,用于显示当前的文件名、光标所在行的行号以及显示比例;
:set number:在命令模式下,用于在最左端显示行号;
:set nonumber:在命令模式下,用于在最左端不显示行号;

Syntax

##vi(options)(parameters)


Options

+<行号>:从指定行号的行开始先是文本内容;
-b:以二进制模式打开文件,用于编辑二进制文件和可执行文件;
-c<指令>:在完成对第一个文件编辑任务后,执行给出的指令;
-d:以diff模式打开文件,当多个文件编辑时,显示文件差异部分;
-l:使用lisp模式,打开“lisp”和“showmatch”;
-m:取消写文件功能,重设“write”选项;
-M:关闭修改功能;
-n:不实用缓存功能;
-o<文件数目>:指定同时打开指定数目的文件;
-R:以只读方式打开文件;
-s:安静模式,不现实指令的任何错误信息。

Parameters

File list: Specify the Edited file list. Use spaces to separate multiple files.

Knowledge expansion

The vi editor has three working modes: command mode, input mode and ex escape mode. Through corresponding commands or operations, conversion can be performed between these three working modes.

Command mode

Enter the command vi after the Shell prompt to enter the vi editor and be in vi command mode. At this time, any characters entered from the keyboard are interpreted as editing commands. For example, a (append) represents an append command, i (insert) represents an insert command, x represents a delete character command, etc. If the entered character is not a legal command of vi, the machine will sound an "alarm sound" and the cursor will not move. In addition, the characters entered in command mode (i.e. vi command) are not displayed on the screen. For example, if you enter i, there will be no change on the screen, but by executing the i command, the working mode of the editor will change: by command mode Change to input mode.

Input method

By inputting vi’s insert command (i), append command (a), open command (o), replace command (s), and modify command (c) or replace the command (r) to enter the input mode from the command mode. In input mode, all characters entered from the keyboard are inserted into the buffer being edited and treated as the text of the file. After entering the input mode, all the visible characters entered are displayed on the screen, but the editing commands no longer work and only appear as ordinary letters. For example, if you enter the letter i in command mode, enter the input mode, and then enter i again, a letter i will be added to the corresponding cursor on the screen.

The way to return to the command mode from the input mode is to press the Esc key. If you are already in command mode, pressing the Esc key will make a "beep" sound. In order to ensure that the vi command that the user wants to execute is entered in command mode, you may wish to press the Esc key a few more times and enter the command after hearing the beep.

ex escape method

The functions of vi and ex editor are the same, the main difference between them is the user interface. In vi, commands are usually single letters, such as a, x, r, etc. In ex, the command is a command line that ends with the Enter; key. vi has a dedicated "escape" command that provides access to many line-oriented ex commands. To use ex escape mode, enter a colon (:). As with the ex command prompt, the colon appears on the status line (usually the bottom line of the screen). Press the interrupt key (usually the Del key) to terminate the executing command. Most file management commands are executed in ex escape mode (for example, reading a file, writing the contents of the edit buffer to a file, etc.). After the escape command is executed, it automatically returns to the command mode. For example:

:1,$s/I/i/g 按Enter键

will replace all uppercase I's with lowercase i's from the first line of the file to the end of the file ($).

The above is the detailed content of What is the function of linux file editing command vi?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn