Simple VI usage

Source: Internet
Author: User
Tags add numbers

There are two modes after entering vi
1. editing mode
2. Command mode

After entering VI, the command mode is used by default. Therefore, do not press it to generate dd alerts. If you want to enter text, press a and I to start typing text!
Press ESC to return to the command mode.
Enter Q to exit
Enter WQ to save the disk and exit.
Enter Q! Discard the disk and forcibly exit

.
VI usage

VI is a standard text editor in Unix systems. Every network administrator must be familiar with it, because it is often encountered during system maintenance.
VI for beginners, the first impression is always bad and hard to use. as a matter of fact, after getting used to the office software, you may not be able to adapt to these unseen monsters. This is normal. but don't forget, everyone will be an important network administrator in the school. Your role is no longer a pure user, but a manager and problem solver facing complicated networks, you must be able to control your system, set your system, and adjust your system. In this process, you have to face VI.
Do not use Word habits to pick VI. They have different roles and styles. Once you can get started, you may like it. VI is the standard for network administrators.
So, open your mind, and accept will be more rewarding than rebound.
2. Enable and disable an archive
VI prepare name Open File
: Method for entering the command Column
Q exit (command column)
Store the WQ disk and then exit (command column)
Q! Discard the disk and force it out (command column)
ZZ Save the disk and then exit (not used in the Command column)
: E-producer name: Enable file in VI
Iii. VI operation mode:
VI has two main operation modes. The ESC key is a one-way switch (from the input mode in this article to the edit command mode)
Edit command mode: to perform specific operations for VI. such as insert, append, replace, modify, delete, move the cursor, search, and so on. if you want to save disks or other actions, you must issue them in the Command column. to switch to the command column, you must first press:
Input mode: In this mode, any character is considered as input data.
After entering VI at the beginning, you enter the edit command mode. Each character you enter is considered as a command. valid commands are accepted. If invalid commands are used, beep sound is generated to indicate a warning.
If you want to perform the input operation, you must switch to the input mode in this article. You only need to press a special character key, as shown below:
Insert by I or I
Append status by a or
In the "Replace by R" status, R will replace consecutive characters.
Delete A character by S or S; Delete the column where the cursor is located
Press CW, C2B, C $, C0 to modify the status
Add blank columns by O or O
You don't have to use it all at once. As long as I, A, and O modes are met, you can win the competition, because most of the time you only use these three modes.
OK, now you can try to enter some materials in the input mode of this article to see. Then, Press ESC to switch back to the edit command mode.
4. move the cursor:
To apply different terminal modes, VI uses a word key to manipulate the cursor.
K j h l top, bottom, left, right
Enter for columns
W B: One Word forward; one word backward
0 $ to the beginning of the column; to the end of the column
+-The next column; the last column
Remember the following tips:
In VI, most commands can be preceded by a number, which indicates the number of times the command is repeated.
For example, 8l indicates moving 8 characters to the right, 3 W indicates moving three words forward, and 2B indicates moving two words backward.
NOTE: For the definition of word, continuous English numerals form a word. For other special characters, each character is a word (unless consecutive special characters). For example: -- classify contains two words, one being -- and the other being classify.
-X has three words.
In special terminal mode, direction keys can also be used, such as Linux or vt100.
In fact, most of the time, you still have the habit of using the direction keys, but you can only use the pgup, pgdn, end, home and other keys in the local environment!
However, if the terminal mode you are using does not support direction keys, the keys in the above table are very important.
V. Volume page:
^ F to the next page (forward)
^ B goes to the previous page (Before)
^ D half down the screen (down)
^ U up half screen (up)
6. Skip columns:
You can specify the position to jump to a column. Before the jump, check the number of columns currently in the row.
^ G can display the current position, for example, "ls.txt" line 53 of 264 -- 20% -- Col 16-23
As shown above, currently there are 53rd columns, all of which are 264 columns, accounting for 20% of this file. Currently, the cursor is actually in 23rd rows, but only 16th rows, because the tab key is used in the middle.
Jump 80g or 80 Gg to 80th Columns
G jump to the end of the file
Gg jump to File Header
7. Delete: (but will not enter the input mode in this article)
X delete a character
Dd deletes a row
R replaces one character
All of the above functions for the current position of the cursor.
There are also DW, DB, de, D0, d $ and other commands, but you don't have to use them all at once. The three in the above table are enough.
Remember one tips: There are a lot of VI commands, but you don't need to use them much, and that's enough.
8. Undo (reply action ):
If you regret the previous action, you can use the Undo function:
U cancels the changes produced by the previous action
U cancel all changes in the column where the cursor is located
: E! Cancel all changes and reply to the change before modification
9. Keyword Search/replace:
/Search for keywords
? Keyword Search for keywords
N repeated search commands (whether down or up)
N and N in the opposite direction
The CW string replaces the original keyword with this string
N. Continue to replace the string action
% S/target/replace/g replace all target strings with replacement strings
10. Selection, replication, and Migration:
Y or YY select the column where the cursor is located
5y or 5yy select the 5 columns in which the cursor column is located (5 is just an example, you can use another number)
P: copy the selected content to the cursor
P copies the selected content before the cursor
The delete commands such as CW, X, and DD will put the deleted content into the buffer, and use P or P to perform the migration.
11. Temporarily jump out of VI:
During the editing process, you may want to execute other commands as follows:
:! Command to execute this command
: Sh temporarily jumps out of vi. If you want to return to VI, press ^ d or exit.

VI Working Mode

Edit mode

Insert mode

Command mode

Shell Switch

Option settings

Advanced features of vim and gvim

1.6.1 VI Working Mode
After the initial startup, VI enters the editing mode. You can use predefined buttons to move the cursor, delete text, copy or paste text. These buttons are common characters. For example, if l is to move the cursor to the right, K is to move the cursor down, and K is to move the cursor down. In editing mode, you can also use some special buttons to select text and then delete or copy the text.

After you enter commands such as I, A, and O in editing mode, you can enter the insert mode. Type: Enter the naming mode. In insert mode, any character other than ESC entered by the user will be considered as a character inserted into the editing buffer. Press ESC to switch from insert mode to edit mode.

In command mode, VI moves the cursor to the bottom of the screen and displays a colon at the first character ). In this case, you can enter some commands. These commands can be used to save files, Read File Content, execute shell commands, set VI parameters, find strings or replace strings in a regular expression.

1.6.2 editing mode
1. move the cursor
To modify the body content, you must first move the cursor to the specified position. The simplest way to move the cursor is to press the top, bottom, left,
Right arrow. In addition to this primitive method, you can also use the many character combination keys provided by VI to move the cursor in the body
To locate the specified row or column. For example:
K, J, H, and l functions are equivalent to the upper, lower, left, and right arrow keys.
CTRL + B Move up a page in the file (equivalent to the Pageup key)
CTRL + F move down a page in the file (equivalent to the Pagedown key)
H move the cursor to the top of the screen (highest)
NH moves the cursor to line N of the screen
2 h move the cursor to the second line of the screen
M move the cursor to the middle of the screen (middle)
L move the cursor down the screen (lowest)
NL moves the cursor to the nth line of the screen
3l move the cursor to the last 3rd rows of the screen
W shifts the cursor right in the specified row to the beginning of the next word.
E move the cursor right in the specified row to the end of a word
B. move the cursor left in the specified row to the beginning of the previous word.
0: 0. move the cursor left to the beginning of the line.
$ Move the cursor right to the end of the row
^ Move the cursor to the first non-empty character in the row

Cursor movement command:

Command description function key
0 move to the front of the column where the cursor is located [home]
$ Move to the last [end] of the column where the cursor is located
[CTRL] [d] half a page [Pagedown]
[CTRL] [f] to the next page
[CTRL]Up Half
[CTRL]To the previous page [Pageup]
H move to the first column of the window
M moves to the middle column of the window
L move to the last column of the window
B Move to the first letter of the next word
W move to the first letter of the previous word
E. Move to the last letter of the next word.
^ Move the first non-blank character to the column where the cursor is located
N-Minus signs move to the first non-blank character in the previous column
You can add numbers to the preceding n columns.
N + plus signs move to the first non-blank character in the next column
You can move the preceding number to the following n columns.
Ng directly uses the number N plus the capital G to move to column N

2. Replace and delete
After the light is calibrated to a specified position in the file, you can use other characters to replace the characters pointed to by the cursor, or delete one or more
Characters. For example:
RC uses C to replace the current character pointed to by the cursor
NRC uses C to replace the first n characters pointed to by the cursor
5rc replaces the first five characters pointed to by the cursor with C
X deletes the current character pointed to by the cursor
NX deletes the first n characters pointed to by the cursor
3X Delete the first 3 characters pointed to by the cursor
DW Delete the word on the right of the cursor
Ndw deletes n words on the right of the cursor
3 DWS Delete the three words on the right of the cursor
DB deletes words on the left of the cursor
NDB deletes n words on the left of the cursor
Delete 5 words on the left of the cursor in 5db
Dd deletes the row where the cursor is located and removes gaps
NDD deletes n rows of content and removes gaps
3 dd delete 3 rows and remove gaps

3. paste and copy
The content (such as characters, words, or rows) deleted from the body is not really lost, but is cut and copied to a memory buffer. Users can
Paste it to the specified position in the body. The command to complete this operation is:
P: lowercase letter P. paste the content of the buffer to the end of the cursor.
P capital letter P, paste the content of the buffer to the front of the cursor
If the buffer content is a character or word, it is directly pasted before or after the cursor; if the buffer content is the whole line of body, it is pasted in the current
The top or next line of the row where the cursor is located.
Note the uppercase and lowercase letters in the preceding two commands. The VI editor often provides a pair of similar functions with a large and lowercase letter (such as P and P ).
. Generally, lower-case commands operate after the cursor, and upper-case commands operate before the cursor.
Sometimes you need to copy a piece of text to a new location, while retaining the content of the original location. In this case, you should first copy the specified content (instead
Cut) to the memory buffer. The command to complete this operation is:
YY copies the current row to the memory buffer
Nyy copies n lines of content to the memory buffer.
Copy 5 lines of content to the memory buffer in 5yy

4. Search for strings
Like many advanced editors, VI provides powerful string search functions. To find the location where a specified word or phrase appears in the file, you can
Search directly with VI instead of manually. The search method is: Enter the character/, followed by the string to be searched, and then
Press enter. The editing program performs a forward search (toward the end of the file) and stops the cursor
Enter the N command to continue the search and find the next position of the string. Use characters? Replace/to reverse
Search (beginning with a file ). For example:
/Str1 forward search string str1
N continue searching to find the position where the str1 string will appear next time
? Str2 reverse search string str2
Regardless of the search direction, when the end or the beginning of the file is reached, the search will loop to the other end of the file and continue to execute.

5. Revocation and repetition
When editing a document, you can use the undo command to eliminate the consequences of an incorrect editing command. In addition, if you want
The cursor position of repeat the previously executed edit command. You can use the Repeat command.
U undo the result of the previous command
. Repeat the last command to modify the body.

6. Select text
Vi can enter a visual mode. In this mode, you can use the cursor to move the command to select text visually and then perform other editing operations, such as deleting and copying. V character selected command v line selected command

# DEMO #

1.6.3 Insert Mode
1. Enter the insert mode
After you locate the cursor correctly in edit mode, you can switch to insert mode with the following command:
I enter the body on the left of the cursor
A. Enter the text on the right of the cursor.
O Add a new line to the next row of the cursor
O Add a new line to 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.
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
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
NCB replaces n words on the left of the cursor with the input body
CD replaces the row of the cursor with the input body
NCD replaces n rows under the cursor with the input body
C $ replace all characters starting from the cursor to the end of the line with the input body
C0 replaces all characters starting from the beginning of the line with the input body with the cursor

2. Exit the insert mode.
To exit the insert mode, Press ESC or Ctrl + [.

# DEMO #

1.6.4 command mode
In the command mode of Vi, you can use complex commands. In edit mode, type ":". Then, the cursor jumps to the last line of the screen and a colon is displayed. The command mode is displayed. Command mode, also known as "last line mode", displays user input in the last line of the screen, press the Enter key, VI to execute the command.

1. Exit the command
In editing mode, you can use the ZZ command to exit the VI editing program. This command saves the changes made to the body and overwrites the original file. If you only need
Exit the editing program and do not want to save the edited content. You can run the following command:
: Q exit without modification
: Q! Discard all modifications and exit the editing program

2. line number and file
Each line in the editing contains its own line number. You can use the following command to move the cursor to the specified line:
: N move the cursor to line N
In command mode, you can specify the range of line numbers for command operations. A numeric value is used to specify an absolute line number. The character "." indicates the line number of the row where the cursor is located.
"$" Indicates the row number of the last line of the body. A simple expression, for example, ". + 5" indicates the row down the current row. For example:
: 345 move the cursor to line 345th
: 345 million file write 345th rows into the file File
: 3rd W file: Write lines 5th to file files
: 1,. W file writes row 1st to the current row into the file File
:., $ W file: Write the current row to the last row to the file 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, equivalent to the w file command
In command mode, you can read or write the body from a file. For example:
: W writes the edited content to the original file to save the intermediate editing result.
: WQ writes the edited content to the original file and exits the editing program (equivalent to the ZZ command)
: W file writes the edited content to the file, keeping the content of the original file unchanged.
: A, BW file writes the content from row A to row B to the file File
: R file reads the content of the file and inserts the content behind the row where the current cursor is located.
: E file: edit the new file to replace the original content.
: F file: Rename the current file to file
: 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.

3. string SEARCH
Returns a string that can be searched to reach the specified row. If you want to perform a forward search, place the string to be searched in two
/"; If you want reverse search, place the string in 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 row containing the string Str
:/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 row containing str1 to the row containing str2
Input File

4. Text replacement
Use the S command to replace strings. The specific usage includes:
: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2
: 1, $ S/str1/str2/g replace str1 with string str2
: G/str1/S // str2/g functions are the same as above
From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search
String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.

5. Delete the body
In command mode, you can also delete content in the body. For example:
: D. Delete the row where the cursor is located.
: 3D delete 3 rows
:., $ D Delete the current row to the end of the body
:/Str1/,/str2/d delete all rows from string str1 to str2

6. Restore Files
VI will generate another temporary file when editing a file. The file name usually starts with. And ends with. SWP. VI in
When the file Exits normally, it is deleted. If the file unexpectedly exits without saving the latest modification content, you can use the recovery command:
: Recover recovery file
You can also use the-r option when starting VI.

# DEMO #

# DEMO #

1.6.5 option settings
To control different editing functions, VI provides many internal options. Use the SET command to set options. The basic syntax is:
: Set option set Option
Common features include:
Autoindent: sets this option, and the body is automatically indented.
If this option is set for ignorecase, the differences between upper and lower case letters in the Rule expression are ignored.
Number.
Ruler sets this option to display the position of the row and column of the cursor at the bottom of the screen.
Tabstop sets the number of spaces skipped by pressing the tab key. For example, set tabstop = n. The default value of N is 8.
MK saves the options in the. exrc file of the current directory.

# DEMO #

1.6.6 shell Switch
When editing the body, use the shell switch Command provided in VI command mode to execute the Linux Command without exiting VI.
Convenience. Syntax format:
:! After executing the shell command, return to VI
In edit mode, Enter K to run the VI command to find the manual page of the word where the cursor is located. This is equivalent to running the man command.
# DEMO #

1.6.7 advanced features of vim and gvim
Vim stands for VI improved. As its name implies, VIM exists as an upgraded version of the standard UNIX system VI editor. Besides providing the same powerful functions as the VI Editor, VIM also provides multi-level recovery, command line history, command and file name completion functions.

Gvim is the X Window version of vi. It supports mouse selection and some advanced cursor movement functions with menus and tool buttons.

# DEMO #
Swap two character locations
XP
Change the upper and lower lines
DDP
Reverse the File Content
: G/^/M0/

Last
Merge the upper and lower rows
J

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.