Common VI skills

Source: Internet
Author: User
Common VI skills

This article aims to introduce some of VI's most common commands and some advanced applications in a more systematic and systematic manner.

 

I. Basic commands:

1) cursor command
K, J, H, l --- move the cursor up and down the command, although you can use the four cursor keys on the right of the keyboard in Linux,
But remember that these four commands are useful, that is, the position of the right hand on the keyboard.
Ng ---- n indicates the number of rows. This command immediately redirects the cursor to the specified row.
CTRL + G ----- number of rows and columns at the cursor position
W, B ------ let the cursor skip a word forward or backward

2) EDIT commands
I, A, R ------- Insert the character command above the cursor (I = insert, A = append, r = replace)
CW, DW ------ change (replace)/delete the word at the cursor position (C = change, D = delete)
X, d $, DD ----- delete a character, all characters from the cursor position to the end of the line, and the whole line command

3) Search for commands
/String ,? String ----- command to search for the corresponding string from the cursor to the forward

4) Copy command
YY, P ----- command to copy a row to the clipboard/to retrieve the content in the clipboard

 

Go to VI (enter the following command at the system prompt ):
VI enters VI without reading any files

Ii. FAQs and application skills

1) Read/etc/passwd in a new file and retrieve the username section.
VI File
: R/etc/passwd: Read/etc/passwd at the cursor position in the opened file
: % S/:. * // G delete all parts after the user name in/etc/passwd from the colon until the end of the line
: 3R/etc/passwd: Read the file content after the specified row number

Another method is to delete all empty lines in the file and comment lines starting #.
# Cat squid. conf. Default | grep-V '^ $' | grep-V '^ #'

2) After opening a file and editing it, the logged-on user does not have the write permission for the file and cannot store the disk.
VI File
: W/tmp/1 Since the disk cannot be stored, do not want to give up all the modifications, first temporarily save to/tmp/1
: 20th w/tmp/1 or simply store the content between and 59 rows into a file/tmp/1

3) Use VI to edit a file, but you need to delete the content of a large segment.
VI File
CTRL + G move the cursor to the row to be deleted, press Ctrl + G to display the row number, and then press Ctrl + G at the end.
: 23,104 5d if the two row numbers are 23 and 1045, delete all the contents of these two columns.

You can also use Ma In the start and end lines, mark the MB command, and delete it with 'a, 'bd.

4) add strings to the beginning or end of a row in the entire file or in a certain number of rows.
VI File
: 3, $ S/^/some string/insert some string before the beginning of the first line to the last line of the file
: % S/$/some string/g add some string at the end of each row of the entire file

: % S/string1/string2/g replace string1 with string2 in the entire file
: S/string1/string2/replace string1 in the third to seventh rows of the file with string2

Note: S is substitute, % indicates all rows, G indicates global

5) EDIT two files at the same time and copy and paste the text in the two files.
VI file1 file2
YY simultaneously open two files and copy the row where the cursor of file 1 is located
: N switch to file 2 (n = NEXT)
P paste the copied row at the cursor of file 2
: N switch back to file 1

6) Replace the path in the file

: % S #/usr/bin # G. replace all the paths/usr/bin in the file with/bin.
Or use
: % S // USR // bin/G indicates '/' is a single character '/' before '/'

 

VI filename: Enter VI and read the file with the specified name (both new and old files can be used ).
Vi + N filename enters VI and starts from the first line of the file.
Vi + filename enters VI and starts from the last line of the file.
Vi +/word filename enters VI and starts with the word of the file.
VI filename (s) enters VI and lists the specified files. The first file is read first.
Vedit enters VI and "insert mode" is displayed in the status line when the input mode is used ".

Edit several files(Use VI filename (s) after entering VI)
: ARGs displays the file names in the editing list.
: N: Read the next file in the editing list.
: Rew reads the first file in the editing list
: E # Read the previous file in the editing list
: E file: Read another file into VI (this file may not be in the editing list). If the original file has not been archived after modification, archive it with W.
: E! File forces reading another file into VI. The original file is not archived.

 Storage and exit vi
: W filename is saved to the specified file, but VI is not exited (if no file name is specified, it is the currently working file name ).
: WQ or ZZ save the file and exit VI.
: Q does not make any changes and exits VI.
: Q! Discard any modifications and exit VI.
:! Command temporarily exits VI and executes shell commands. After the command is executed, return to VI.
: Sh temporarily exits VI to the system. Press Ctrl + D at the end to return to VI.
 
Add data commands
I start to insert characters at the mark-off position, and Press ESC at the end.
I start to fill in at the beginning of the row where the cursor is located, and Press ESC at the end.
A starts to add words after the cursor position. Press ESC at the end.
A starts to add words at the end of the row where the cursor is located. Press ESC at the end.
O add a blank line under the cursor and start to add words. Press ESC at the end.
O add a blank line to the cursor and start adding the word. Press ESC at the end.
! Command executes the shell command and adds the result to the next line in the row where the cursor is located.

DELETE command
NX deletes n characters starting from the cursor position (including the cursor position, and deletes the character of the cursor by X)
NX deletes n characters starting from the cursor position (excluding the cursor position ).
Ndw deletes the N character groups (Word) at the cursor position ).
D0 deletes all the characters starting from the row to the cursor position.
D $ or D Delete All characters starting from the cursor position to the end of the line.
NDD deletes n rows (whole row) starting from the cursor position (DD indicates deleting the row where the cursor is located ).
: Start, endd Delete the start-to-end row of the object.

Mark moving
0: Move to the beginning of a row
$ Move to the end of a row
[Move to the start position of the file
] Move to the end of the file
NH shifts n places to the left
NL or spacebar shifts n places to the right
NK moves n rows up
N + shifts n rows up, And the cursor starts at the beginning of the row
Ni moves n rows down
N-move n rows downward, cursor at the beginning of the row
H move to the upper left corner of the screen
M moves to the beginning of the middle line of the screen
L move to the last line of the screen
G move to the last row of the file
NG or: N moves to the nth row of the file.
NW shifts N word groups to the right. punctuation marks belong to word groups.
NW shifts N word groups to the right. The punctuation marks do not belong to the word group.
N word groups are moved to the left, and punctuation marks belong to word groups.
N word groups are moved to the left, and punctuation marks do not belong to the word group.
CTRL + u scroll menu on the screen
CTRL + D. Half menu on the screen
CTRL + B roll a menu on the screen
CTRL + F. A menu is available under the screen.

Modify command
R modify the character of the cursor file
R changes from the cursor position. Press ESC at the end.
New: Change N groups of characters. Press ESC at the end.
NCC changes n rows from the cursor position, and Press ESC at the end

Rearrange the length of each row
I and press enter to disconnect the line from the cursor and enter the insert mode
J. After connecting the data in the next row to the current row

Search for commands
/Text: Find the string text from the cursor position
? Text searches for the string text from the cursor position
N continue to find the next string (used after entering the SEARCH Command above)

Find and replace commands
: Getxt1/S // text2/options replace text1 of each row with text2
Option = G indicates that all the text1 files are replaced. If no option is entered, only the first text1 in each row is replaced.
Option = go display the replaced rows on the screen
Option = GC requires confirmation before each string is replaced

Start, endgtext1/S // text2/options are the same as above, and only the start ~ End line.
Or: Start, ends/text1/text2/options

Copy and move files

: First, last co dest: copy the data from the first row to the last row to the target row (DEST ).
: Start, endm DEST move the data from start to end row to the target row (DEST.
: R filename: reads the content of the specified file into the row where the cursor is located.
NY saves n rows of data starting from the cursor position
P copies the saved data in the next row of the cursor.
P copies the temporary data row on the top of the cursor.

Other commands
. Repeat the previous command
U cancels the previous command
CTRL + L refresh the screen
: Set number: displays the row number of the file, but does not store the file
: Set nonumber remove row number display
: Set AI to set the starting position of each line (starting with the current position of the cursor)
: Set noai to cancel row start position setting
: F or <Ctrl> + G tells the user about the data of the current edited file.

 

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.