VI editor

xiaoxiao2021-03-06  66

16,000 year editor: vi

VI's Working Mode Edit Mode Insert Mode Command Mode SHELL Switch Option Sets Vim and GVIM Advanced Features

1.6.1 VI work mode

VI first enters editing mode after initial startup, then the user can use some predefined buttons to move the cursor, delete text, copy, or paste text. These buttons are ordinary characters, such as L, moving the cursor to the right, equivalent to the right arrow keys, K is moving down the cursor, equivalent to the down arrow key. In editing mode, users can also use some special buttons to select text, then delete, or copying.

When the user typed I, A, O and other commands in edit mode, you can enter the insert mode; type: You can enter the naming mode. In the insert mode, the user then enters, any character other than ESC will be seen as a character inserted into the edit buffer. After pressing Esc, switch from the insert mode to the edit mode.

In command mode, VI will move the cursor to the bottom of the screen and display one at the position of the first character: (colon). At this time, the user can type some commands. These commands can be used to save files, read file content, execute the shell command, set the VI parameters, find strings or replace strings in a regular expression.

1.6.2 Edit mode

Mobile cursor

To modify the content of the body, you must first move the cursor to the specified location. The easiest way to move the cursor is to press the upper, lower, left and right arrow keys on the keyboard. In addition to this most original method, the user can use the numerous character combination keys provided by the VI, move the cursor in the body, quickly reach the specified row or column, and implement positioning. For example: K, J, H, L function is equivalent to the upper, lower, left, right arrow keys Ctrl b move upward (equivalent to the PageUp key) Ctrl F moves down in the file ( Equivalent to the PAGEDOWN key) h Move the cursor to the top of the screen to move the cursor to the nth line 2H of the screen to move the cursor to the second row m to move the cursor to the middle of the screen to the screen. The cursor moves to the bottom of the screen to move the cursor to the countdown nth line 3L of the screen to move the cursor to the countdown line W in the screen W, right-handed the cursor inside the specified line, to the beginning of the next word E. Right shifting the beam, to the end of the word B. The left shot in the specified line, the beginning of the previous word 0 digits 0, the left shidder, to the beginning of the Bank, right moving the cursor, to the end of the Bank, "mobile cursor, To the first non-empty character

2. Replace and delete

When the cursor is positioned in the file specified position, you can replace the characters directed by the cursor or delete one or more characters from the current cursor position. For example, the current character NRC pointed to the RC by the C replacement cursor is pointed to by the C replacement cursor, and the first five character x pointed by the C replace the cursor to remove the cursor point to the cursor pointing the cursor pointing. The first n characters 3X ​​Delete the first three characters directed by the cursor to remove the word NDW on the right side of the cursor to remove the n word on the right side of the cursor 3DW Delete the 3 word DB of the cursor on the right side of the cursor to delete the word NDB to delete the cursor left The n word 5db on the side deletes the 5 word DD deletion cursor on the left side of the cursor, and removes the gap NDD to delete the N row content, and remove the void 3DD to delete 3 line content, and remove the void

3. Paste and copy

The content deleted from the body (such as characters, words or rows) is not really lost, but is cut and copied to a memory buffer. The user can paste it into the specified location in the body. The command to complete this is: p lowercase letter P, paste the contents of the buffer to the back of the cursor P uppercase the letter P, paste the contents of the buffer to the cursor, if the content of the buffer is character or word, direct paste At the front or back of the cursor; if the content of the buffer is a special line, it is pasted on the previous line or next line of the current cursor. Note the case in the above two commands. The VI editor often provides a pair of similar functions in a pair of large, lowercase letters (such as P and P). Typically, the lowercase command operates behind the cursor, and the uppercase command operates in front of the cursor. Sometimes you need to copy a piece of text to a new location while retaining the original location. In this case, you should first copy the specified content (instead of cutting) to the memory buffer. The command to complete this is: yy Copy Current Direction to Memory Buffer NYY Copy N Route content to memory buffer 5yy copy 5 line content to memory buffer 4. Search string

Like many advanced editors, VI provides a powerful string search function. To find the location of the specified word or phrase in the file, you can search directly with VI without having to perform manual manual. Search method is: Type characters /, followed by the string to search, and press Enter. The edit program performs a forward search (ie, the end of the file), and then stops the cursor to the beginning of the string after finding the specified string; type n command to continue search, find out the next appearance next time position. Use characters to replace /, reverse search (in the beginning of the document). For example: / STR1 proceeds to search string Str1n, finds the position of the STR1 string next time? STR2 reverse search string STR2 does not have the search direction, when the end of the file is reached, the search work will loop to the file. On the other end and continue.

5. Undo and repeat

In the process of editing the document, in order to eliminate the consequences of an error editing command, you can use the undo command. In addition, if the user wishes to repeat the editing commands performed in front of the new cursor position, you can use a repetition command. u Undo the result of the previous command. Repeat the last modification of the text

6. Text selection

The VI can enter a mode that becomes Visual. In this mode, the user can select the text with the cursor movement command, and then perform other editing operations, such as deletion, copying, etc. v character selection command V line selection command

# Demo #

1.6.3 Insert mode

1. Enter the insert mode

After the cursor is properly locked in the edit mode, you can switch to the insert mode with the following command: i Enter the body A on the left side of the cursor A In the right side of the cursor O. The next line of the cursor is in the row of new rows O on the cursor. Adding a new row i In the start of the cursor, the input body A is input to the end of the end of the cursor, and the text is introduced to a simple method for switching to the insert mode. There are also some commands that allow first to delete a body before entering the insert mode, thereby implementing the body's replacement. These commands include: s The character NS pointed by the input body ns by the input body NS replaces the cursor right side N-character CW with the input body side NCW with the input body side NCW with the input body to replace NS. N The word Cb replaces the word NCB on the left side of the word Cb by the input body NCB replaces the N-line CD on the left side of the cue on the left side of the N-line NCD, the NCD is replaced with the input body, the N-row C $ input. The body replaces all characters from the beginning to the end of the Bank to the beginning of the cursor to the beginning of the key to the cursor.

2. Exit insert mode

The method of exiting the insert mode is to press the ESC button or the combination key Ctrl [.

# Demo # 1.6.4 Command mode

In command mode of VI, you can use a complex command. In editing mode, type ":", the cursor jumps to the last row of the screen and displays the colon there, and has entered the command mode. Command mode is also known as "Live Mode", and the contents of the user are displayed on the last line of the screen, press the Enter key, the VI execute command.

Exit command

In editing mode, you can exit the VI editor with the zz command, saving the modifications made to the body to overwrite the original file. If you only need to exit the editor, not to save the edited content, you can use the following command :: q Exit without modification: Q! Abandon all modifications, exit editing

2. Line number and file

Each of the edits has its own line number. With the following command, you can move the cursor to the specified line: n Move the cursor to the nth line command mode, you can specify the line number range of the command operation. The value is used to specify the absolute line number; the character "indicates the line number where the cursor is located; the character mens" $ "indicates the line number of the last line of the body; simple expressions, such as" " 5" means the current line down Chapter 5. For example: 345 Move the cursor to 345 lines: 345W file Write the 345th line to the File file: 3, 5W File Write the third line to the 5th line of File file: 1, .w file will The current line writes the file file:., $ W File Writes the File file to the last line:.,. 5W file starts from the current line to write the 6-line content into the file file: 1, $ w File will all content Write the file file, equivalent to: w file command In command mode, allow you to read the body from the file or write the text into the file. For example: W W Write the contents of the edited to the original file to save the intermediate result of the edited: WQ writes the edited content into the original file and exits the editor (equivalent to the zz command): w File writes the contents of the edited to File File, keep the content of the original file unchanged: a, BW file writes the contents of the line A to the B row into the file file: r file reads the contents of the file file, insert the back of the current cursor in the line: e File Editor New file file instead of the original content: f file Rename the current file to file: f Print the current file name and status, such as the number of lines of the file, the line number where the cursor is located, etc.

3. String search

A string is given, you can reach the specified line by searching the string. If you want to perform a forward search, place the string to be searched between two "/"; if you want to reverse search, place the string between two "?". For example: / STR / forward search, move the cursor to the next row containing the string STR:? STR? Reverse search, move the cursor to the previous line: / STR / W File forward Search, and write the first row of string STR to File files: / str1 /, / str2 / w file forward search, and write a row containing strings STR1 to a row containing string Str2

4. Text replacement

Using: S commands can be replaced by strings. The specific usage includes: S / STR1 / STR2 / with a string Str2 string S / STR1 / STR2 / G with string S / STR1 / STR2 / G in the string STR2 string string string string string string string string string string Str1:., $ S / STR1 / STR2 / G Replace the text of the text to the end to the end to the end of all appearance strings string STR1: 1, $ S / STR1 / STR2 / G with string STR2 Str1: g / STR1 / S / / STR2 / G function can be seen from the above replacement command: g is placed at the end of the command, indicating that each appearance of the search string is replaced; does not add G, indicating that only the first appearance of the search string Replace; g is placed at the beginning of the command, indicating that all the rows containing the search string in the body are replaced. 5. Delete the body

In command mode, you can also delete the content in the body. For example: D Delete the cursor in line: 3D Delete 3 lines:., $ D Delete the end of the current line to the body: / ​​str1 /, / str2 / d Delete all rows from strings STR1 to STR2

6. Restore files

When you edit a file, you will generate a temporary file. The name of this file is usually in the beginning, and the end of the .swp. When the VI is properly exiting, the file is deleted, if an unexpected exit, without saving the latest modification content of the file, you can use the recovery command: Recover recovery file can also utilize the -R option when the VI is started.

# Demo #

# Demo #

1.6.5 option settings

To control different editing functions, VI provides a lot of internal options. Utilize: The set command can set the option. The basic syntax is :: Set Option Settings Option Option Options: AutoInDent Settings this option, then the body is automatically indented to set this option, ignore the difference between the rule expressions in the upper and lower cases of the rules Number setting this option, display the body. The line number RULER sets this option, the cursor is displayed at the bottom of the screen, and the position of the column TabStop sets the number of spaces that tab the Tab key. For example: SET TABSTOP = N, n defaults to 8MK Save option in the current directory. EXRC file

# Demo #

1.6.6 shell switch

When editing the body, use the shell switch command provided in the VI command mode without having to exit the VI to execute the Linux command, which is very convenient. After the syntax format is ::! Command, return to VI after the shell command Command, in the edit mode, type K, can command the VI to find the man page where the cursor is located, which is equivalent to running the man command.

# Demo #

1.6.7 Advanced Features of Vim and GVIM

Vim represents VI Improved, as in its name, Vim is present as a standard UNIX system VI editor raised version. In addition to providing the same power as the VI editor, VI is provided with multi-level recovery, command line history, and command and file name completion.

GVIM is a VI's X WINDOW version, which supports the mouse selection, some advanced cursor mobile features, and with menu and tool buttons.

# Demo # You can write the settings in the .exrc file, after setting, save it with MK

Vim supports Make command to knock directly when you write Makefile: Make will automatically compile and automatically transfer to an error in the error, it is very easy to use.

转载请注明原文地址:https://www.9cbs.com/read-112213.html

New Post(0)