Use of text editing tool vi

  
 

vi is a standard text editing program under linux. It is also the most frequently used text file visual editor under the Linux system command line. It is similar to the Dos command edit. So, what is visualization? In fact, it is to let you edit the text file. When you can see them, it sounds normal, but in the character interface of the command line, visual editing really plays a big role. Vi is the default editor under linux. Almost all linux/unix systems are bundled with vi. This means that as long as you learn the editing tool vi, you can use it on any linux/unix system, and the vi program is very small. It is only a few hundred K in size, so as long as you are proficient in vi operation, you can do whatever you want. The vi editing program has two working modes: command mode and text input mode. In the command mode, the input characters are interpreted as vi commands, which means that these commands form a powerful function of vi, which can be converted to text input mode by vi command. In the text input mode, the input characters are accepted as the body content of the file being edited, and if the “Esc” key is pressed, the mode is switched to the vi command mode. Below we introduce the meaning and usage of a series of vi commands one by one by means of classification. 1. The command options for opening files with vi under the Linux command line are as follows:

vi Use options Meaning vi filename Open or create a new file and place the cursor at the beginning of the first line. Vi –r filename Restores the file that crashed the last time vi was opened. Vi –R filename puts the specified file into the editor as read-only. Vi + filename Opens the file and places the cursor at the beginning of the last line. Vi +n filename Open the file and place the cursor at the beginning of the nth line. Vi +/pattern filename Opens the file and places the cursor at the first position that matches pattern. Vi –c command filename Executes the specified command before editing the file.

2. The command options in the vi command mode are mainly divided into the move cursor command, the screen roll command, the insert delete text command, the search and replace command, and the save exit command. They are explained in the following list. (1) Move cursor type command list:

Typed characters Meaning h Move the cursor one character to the left l Move the cursor one character to the right. Backspace key Moves the cursor one character to the left. k or Ctrl+p The cursor moves up one character. j or Ctrl+n The cursor moves down one character. Enter key The cursor moves down one line. w or W Cursor right one word to the beginning of the word. b or B The cursor moves one word to the left. The e or E cursor moves right one word to the end of the word. The nG cursor moves to the beginning of the nth line. n+ The cursor moves down n lines. N- The cursor moves up n lines. n$ Relative to the current cursor line, the cursor moves back n lines to the end of the line. H The cursor moves to the top line of the current screen. The M cursor moves to the middle of the current screen. The L cursor moves to the bottom line of the current screen. 0 Moves the cursor to the beginning of the current line. $ Moves the cursor to the end of the current line. :$ Moves the cursor to the beginning of the line in the last line of the file.

(2) List of screen scrolling commands:

Typed characters Meaning Ctrl+u Relative to the current screen, halfway through the file. Ctrl+d Relative to the current screen, half a screen to the end of the file. Ctrl+b Relative to the current screen, flip the file to the first screen Ctrl+f relative to the current screen, flip the screen to the end of the file nz+Enter to scroll the nth line of the file to the top of the screen. If you do not specify n value, the current The line scrolls to the top of the screen.

(3) Insert delete text class command list:

Typed characters Meaning Esc Returns the command line mode. i Enter text before the cursor. a Enter text after the cursor. o Open a new line below the current line. O Open a new line above the current line. r Replace the character of the cursor. After inputting the r command, enter the new character to be replaced on the keyboard to complete the replacement. x Delete the character where the cursor is. Dd Delete the line where the cursor is. The deleted content is automatically saved on the clipboard. Yy Copies the line where the cursor is located to the clipboard. If you add a number before “yy”, you can copy multiple lines. p Copy the contents of the clipboard to the cursor. P Copy the contents of the clipboard to the front of the cursor. Yw copies the word where the cursor is located to the clipboard.

(4) Search and replace class command list

Typed characters Meaning /abc Look forward to the string abc in the file. ?abc looks backwards for the string abc in the file. n Repeat the last search command in the same direction. N repeats the last search command in the reverse direction. :s/a1/a2/g Replaces all a1s in the line where the current cursor is located with a2. :n1,n2s/a1/a2/g Replace all a1 in the nth to n2th lines of the file with a2. :g/a1/a2/g Replace all a1 in the file with a2. :set nu Adds a line number to each line of the file.

(5) Save exit class command list

Typed characters Meaning :wq Save to exit vi editor, “:wq!” means forced save exit. :q Do not save and exit vi editor, “:q!” means not to save, forced to exit. :w Save but do not exit vi editor, “:w!” means forced save, “:w filename” means to save the changes to “filename”. Equivalent to a separate file. x! Save the file, exit the vi editor, a more general vi command.

Copyright © Windows knowledge All Rights Reserved