Vi editor usage

Source: Internet
Author: User
Vi is a screen editor provided by all UNIX systems. It provides a Windows device that allows you to edit files. Of course, people who know a little about UNIX systems think that vi is super difficult to use, but vi is the most basic editor. after learning vi, in the UNIX world, it will surely be smooth... vi is a screen editor provided by all UNIX systems. It provides a Windows device that allows you to edit files. Of course, people who know a little about UNIX systems think that vi is super difficult to use, but vi is the most basic editor. after learning vi, in the future, the UNIX world will surely be smooth and easy to use, because other types of text processors are not UNIX-standard. If joe or pico is not installed on other Linux machines and vi is not used, it may not be easy.
 
The vi editor has three modes: command mode, input mode, and last line mode. It is very important to master these three modes:
 
Command mode: after vi is started, the command mode is entered by default. from this mode, you can switch to the other two modes by using commands, in any mode, you can press the [Esc] Key to return to the command mode. In command mode, enter the letter I, o, or a to enter the vi input mode to edit the file.
 
Input Mode: In this mode, you can edit, modify, and input files. in the last line of the editor, "-- INSERT --" is displayed, indicating that vi enters the input mode. When modifying input and other operations, we need to save the file. at this time, we need to first return to the command mode and save it in the last line mode.
 
Last line mode: Enter ":" In command mode to enter this mode. There are many useful commands in last line mode.
 
The following describes common command operations in vi.
 
Enter input mode command
 
I. enter the text on the left of the cursor. [common]
A. enter the body on the right of the cursor. [common]
O lowercase character o, adding a new line to the next line of the row where the cursor is located
O uppercase characters O: add a new line to the upper line of the row where the cursor is located
I enter the body at the beginning of the row where the cursor is located
A. enter the body at the end of the row where the cursor is located.
The preceding describes several simple methods to switch to the insert mode. There are also some commands that allow you to delete a segment before entering the insert mode
Text to replace the text. These commands include:
S: replace the character pointed to by the cursor with the input body [common]
Ns replaces n characters on the right of the cursor with the input body
Cw replaces the word on the right of the cursor with the input body [common]
Ncw replaces n words on the right of the cursor with the input body
Cb replaces the word on the left of the cursor with the input body [common]
Ncb replaces n words on the left of the cursor with the input body
Cd replaces the row where the cursor is located with the input body [common]
Ncd replaces n rows under the cursor with the input body
C $ replace all the characters starting from the cursor to the end of the line with the input body. [common]
C0 replaces all characters starting from the beginning of the line with the input body with the cursor. [common]
 
R replaces the character at the cursor. for example, c3 indicates replacing the character at the cursor with character 3. [common]
 
R replace the character until you press [ESC]
 
Press Esc or press ctrl + [exit input mode
 
Last row mode
 
W. save files [frequently used]
 
W file: save the content of the current file to the file
 
Wq: Save the file and exit vi editor [common]
 
Wq file writes the content of the current file to the file, keeps the content of the original file unchanged, and then exits the vi editor (if the content of the current file is changed but the file is not saved, the vi editor will not exit after the command is executed)
 
R file reads the content of the file and inserts the content behind the row where the current cursor is located.
E file: close the current file and open the new file. (if the content of the current file changes but is not saved, this command will not be executed.) [common]
F file: rename the current file as file [common]
F print the name and status of the current file, such as the number of lines of the file and the number of lines where the cursor is located.
 
Q: exit the vi editor (if the current file is changed, it cannot exit) [common]
 
Q! Force exit vi editor (do not save changes) [common]
 
# Move the cursor to line # [common]
 
PS: Tips for Using row numbers:
 
The absolute line number is usually specified with a number. The character "." indicates the line number of the row where the cursor is located; the character "$" indicates the line number of the last line of the file, for example:
 
. + 5 move the cursor down the current row 5 rows
 
$ Move the cursor to the last line of the file
 
1 Move the cursor to the first line of the file
 
23 w file write 23rd lines of the file into the file
 
3rd million file write 5th to rows into file
1. w file writes row 1st to the current row into the file
., $ W file writes the current row to the last row to the file
... + 5 w file: write 6 lines of content into the file from the current row
1, $ w file writes all content to the file, which is equivalent to the w file command
 
 
 
Move the cursor
 
Home: cursor to the beginning of the line
[End] cursor to End of line
[Page Up] Page Up
 
Page Down
 
[Delect] delete the character at the cursor position
Ctrl + f move one page forward
 
Ctrl + B move one page backward
 
H. move a character from the cursor to the left. [common]
J move the cursor down a character [common]
K move the cursor up to a character [common]
L move the cursor one character to the right [common]
Number 0 move the cursor to the end of a row [common]
$ Move the cursor to the beginning of a line [common]
G. move the cursor to the end of the file. [common]
Gg move the cursor to the beginning of the file [common]
W move the cursor to the beginning of the next word [common]
E move the cursor to the end of the current or next word. [common]
B. move the cursor to the beginning of the current or next word. [common]
^ Move the cursor to the first non-blank character (tab, space) of the row [common]
: # Move the cursor to the # position of the row, for example, 3,: 10 [common]
) Move the cursor to the end of the sentence
(Move the cursor to the beginning of the sentence.
Note: The sentence is separated by a space (.) after the end of the half-width period.
} Move the cursor to the end of the paragraph
{Move the cursor to the beginning of a paragraph
Note: paragraphs are separated by blank lines.
NG positions the cursor at the beginning of line n. [common]
 
N + move the cursor down n rows
 
N-move the cursor up n rows
 
N $ move the cursor down n rows and place it at the end of the row
 
H place the cursor at the top of the screen
 
M place the cursor in the middle of the current screen
 
L place the cursor on the last line of the screen
 
~ Change the case sensitivity of the current character. [common]
 
> Move the current row to the right by 8 characters (equivalent to the [tab] Key) [common]
 
<Move the current row to the left by 8 characters (equivalent to [Shift] + [tab]) [common]
 
Nz: Rolls row n to the top of the screen. if n is not specified, the current row is rolled to the top of the screen.
 
Cancel operation
 
The u command revokes the last operation and can use multiple times to restore the original operation. [common]
U undo all operations
. You can restore operations on the use of the u command [frequently used]
 
Copy, delete, and paste operations
X deletes a single character at the cursor and places the deleted character in the cache (actually deleting is equivalent to cutting). [common]
Nx deletes three characters. For example, 3x deletes three characters to the right of the cursor
"Bnx put the deleted characters in the cache area B
 
Dw deletes all characters from the current character to the end of a word (including spaces). [common]
 
Dd delete the row where the cursor is located [common] [common]
Ndd deletes n rows. For example, 3dd deletes three lines of text from the current row.
 
"Bndd: put the deleted content in cache Zone B.
 
PS: If you want to delete the contents from rows m to n, you can enter m and nd in the last row mode. For example, 3, 5d indicates deleting 3rd ~ 5 rows [common]
 
Yy copies the content of the row where the current cursor is located to the vi buffer. [common]
Nyy copies n rows. For example, 5yy copies 5 rows down from the row where the current cursor is located, and places the copied content in the vi cache. [common]
"Bnyy puts the copied content in the cache B. For example, b3yy saves the three rows of data to the cache B. [common]
 
PS: If you want to copy content between Row m and row n, you can enter m in the last row mode, for example, 3, 5y: copy the content from the third row to the fifth row to the cache [common]
 
Yw copies the content from the current cursor position to the end of the word (excluding spaces) to the vi cache, which is equivalent to copying a word [commonly used].
Nyw replicates n words. for example, 2yw replicates two words.
"Bnyw: for example, copy n words to cache Zone B.
 
Y $ copy the cursor to the cache area at the end of the row
Y ^ copy the cursor to the cache area from the beginning of the line
P: lowercase p. paste the content in the cache area below the current row (in combination with yy) or insert it to the next position of the current cursor (with dw, x, y $, y ^) [common]
 
P indicates uppercase P, which is similar to lowercase p, but the pasting is in the opposite direction.
 
Search and replace
The search and replacement functions of vi must be completed in the last row mode.
 
/Top-down search for keywords, such as/work, indicates searching for work content from the current cursor. [common]
 
? Keyword bottom-up search, such as/work, indicates to search for content with the keyword "work" from the current cursor [common]
 
PS: When a match is found, the cursor is positioned at the beginning of the keyword. To continue searching for the next match, you only need to input the character n. to search back, Type N. you can enter 2n to search for the next match.
 
S/old_string/new_string replace the old_string that appears for the first time in the row where the cursor is located with new_string
 
S/old_string/new_string/g replace all old_string in the row where the cursor is located with new_string
 
M, ns/old_string/new_string/g replace all old_string from line m to line n with new_string [common]
 
% S/old_string/new_string/g replace all old_string with new_string [common]
 
/Pattern_address/s/old_string/new_string/g search for the matched row with pattern_address from the current cursor, and replace all old_string with new_string on the row (in fact, this command is composed of/pattern_address and/s/old_string/new_string/g)
 
G/pattern_address/s/old_string/new_string/g find all rows that match pattern_address and replace all old_string values of each row with new_string. (Note: The Meanings of the two g types are different, the previous g indicates that the entire text search matches pattern_address, and the subsequent g indicates that old_string is replaced with new_string for each line that matches pattern_address)
 
If pattern_address is the same as old_string, it can be abbreviated to g/pattern_address/s // new_string.
 
You can add a c command at the end of all commands to force each replacement to be confirmed by the user, for example, s/old/new/c or s/old/new/gc [common].
 
PS: Search Tips
 
/Str/w file: Forward search, and write the first row containing the string str to the file.
/Str1/,/str2/w file forward search, and write the line containing the string str1 to the line containing the string str2 into the file
 
Restore Files
When editing a file, vi will generate a temporary file that starts with. and ends with. swp. Normally, the file is automatically deleted. if the file is unexpectedly exited, such as a sudden power failure, the file will not be deleted. we can select the command to process it the next time we edit it:
 
O read-only access without changing the file content
E. continue to edit the file and do not restore the content saved in the. swp file.
R restores the content of the file not saved after the last edit
Q: Exit vi
D. delete the. swp file.
 
Or use the vi-r file name to restore unsaved content.

Author's Patriot blog
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.