Vim editor commands

Source: Internet
Author: User
Tags first string


Access the VI command
VI filename: open or create a file and place the cursor at the beginning of the first line
Vi + N filename: open the file and place the cursor at the beginning of line N
Vi + filename: open the file and place the cursor at the beginning of the last line
Vi +/pattern filename: open the file and place the cursor at the first string matching pattern
VI-r filename: a system crash occurred when VI was being edited last time, restoring filename
VI filename... filename: open multiple files and edit them one by one.

Move cursor command
H: move the cursor one character to the left
L: move the cursor one character to the right
Space: move the cursor one character to the right
Backspace: move the cursor one character to the left
K or Ctrl + P: move the cursor up a row
J or Ctrl + N: move the cursor down a row
Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move one word to the end of the word to the right of the cursor.
): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
NG: move the cursor to the beginning of line N
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line N
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row

Screen tumble command
CTRL + u: half screen at the beginning of the file
CTRL + D: Flip the half screen to the end of the file
CTRL + F: Flip the screen to the end of the file
CTRL + B; open a screen at the beginning of the file
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.

Insert text commands
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text
NCW or NCW: modify a specified number of characters
NCC: modifies a specified number of rows.

DELETE command
Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
NDD: Delete the current row and the next n-1 row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
CTRL + u: delete text entered in input mode

Search and replace commands
/Pattern: Search for pattern from the beginning of the cursor to the end of the file
? Pattern: Search for pattern from the beginning of the cursor
N: Repeat the previous search command in the same direction.
N: Repeat the previous search command in the reverse direction.
: S/P1/P2/G: replace all p1 in the current row with P2.
: N1, N2s/P1/P2/G: replace all P1 from line N1 to line N2 with P2.
: G/P1/S // P2/G: replace all P1 files with P2.

Option settings
ALL: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (CTRL + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: No write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "\" in search mode.
Nowrapscan: Prohibit VI from searching at both ends of the file and starting from the other end.
Mesg: Allows VI to display the information that other users write to their terminal using write.

Last-line command
: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit VI.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed. If N1 and N2 are not specified, the entire file content is used as the command input.
: R! Command: place the output result of the command to the current line.

Register operation
"? Nyy: Save the content of the current row and Its n rows to the register? , Where? It is a letter, and N is a number.
"? Nyw: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.
"? Nyl: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.
"? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number.
NDD: delete n lines of text from the current row and its bottom, and put the deleted content in the delete Register No. 1.

VI usage
--------------------------------------------------------------------------------

1. Insert text
┌ ── ┬ ── ─ ┐
│ Command │ description │
├ ── ┼ ── ─ ┤
│ I │ insert text before the current character │
├ ── ┼ ── ─ ┤
│ I │ insert text at the beginning of the line │
├ ── ┼ ── ─ ┤
│ A │ add text after the current character │
├ ── ┼ ── ─ ┤
│ A │ add text at the end of the line │
├ ── ┼ ── ─ ┤
│ O │ insert an empty row behind the current row │
├ ── ┼ ── ─ ┤
│ O │ insert an empty row before the current row │
├ ── ┼ ── ─ ┤
│ R │ input text in rewrite mode │
└ ── ┴ ── ─ ┘
2. move the cursor
┌ ── ─ ┬ ── ─ ┐
│ Command │ description │
├ ── ─ ┼ ── ─ ┤
│ J or down arrow │ move down a line │
├ ── ─ ┼ ── ─ ┤
│ K or up arrow │ move up a line │
├ ── ─ ┼ ── ─ ┤
│ H or the left arrow │ move one character to the left │
├ ── ─ ┼ ── ─ ┤
│ L or right arrow │ move one character to the right │
├ ── ─ ┼ ── ─ ┤
│ W │ shift one word to the right │
├ ── ─ ┼ ── ─ ┤
│ W │ right shift a word separated by spaces │
├ ── ─ ┼ ── ─ ┤
│ B │ move one word left │
├ ── ─ ┼ ── ─ ┤
│ B │ move one word left separated by spaces │
├ ── ─ ┼ ── ─ ┤
│ 0 │ move to the beginning of the line │
│ Ctrl-F │ flip forward │
├ ── ─ ┼ ── ─ ┤
│ Ctrl-B │ flip back │
├ ── ─ ┼ ── ─ ┤
│ Ng │ to line N │
├ ── ─ ┼ ── ─ ┤
│ G │ to the last line │
└ ── ─ ┴ ── ─ ┘
3. Replace text
┌ ── ─ ┬ ── ─ ┐
│ Command │ description │
├ ── ─ ┼ ── ─ ┤
│ $ │ To the end of the line │
├ ── ─ ┼ ── ─ ┤
│ (│ To the beginning of a sentence │
├ ── ─ ┼ ── ─ ┤
│) │ To the end of the sentence │
├ ── ─ ┼ ── ─ ┤
│ {│ To the beginning of a paragraph │
├ ── ─ ┼ ── ─ ┤
│ }│ To the end of the section │
└ ── ─ ┴ ── ─ ┘

4. delete text
┌ ── ─ ┬ ── ─ ┐
│ Command │ description │
├ ── ─ ┼ ── ─ ┤
│ R │ replace one character │
├ ── ─ ┼ ── ─ ┤
│ C │ modify the text until you Press ESC │
├ ── ─ ┼ ── ─ ┤
│ CW │ modify the next word │
├ ── ─ ┼ ── ─ ┤
│ Cnw │ modify the next n words │
└ ── ─ ┴ ── ─ ┘
V. text editing
┌ ── ┬ ── ─
│ Life temple │ description │
├ ── ┼ ── ─
│ YY │ move a line of text to the default buffer │
├ ── ┼ ── ─
│ YN │ move the next word to the default buffer │
├ ── ┼ ── ─
│ Ynw │ move the next n words to the default buffer │
├ ── ┼ ── ─
│ P │ if the default buffer contains a line of text
│ Insert an empty line well behind the row to inject the sound in the default buffer zone │
│ Paste to this line; if the default buffer contains │
│ Multiple words, paste these words to the right of the cursor. │
├ ── ┼ ── ─
│ P │ if the default buffer contains a line of text, the current │
│ Insert an empty hole in front of the row to insert the default buffer zone │
│ Paste to this line; if the default buffer contains │
│ Multiple words, paste these words to the left of the cursor


└ ── ┴ ── ─
6. Save and exit
┌ ── ─
│ Command │ description │
├ ── ─
│ ZZ │ save and exit │
├ ── ─
│: W filename │ Write File │
├ ── ─
│: W │ write files │
├ ── ─
│: X │ save (if the current file has been modified) and exit │
├ ── ─
│: Q! │ Exit without saving the file │
├ ── ─
│: Q │ exit VI │

Common VI skills

The VI command is the most commonly used file editing command in the Unix/Linux World, but many people are not used to it because of its many command sets, in fact, you only need to master the basic commands and use them flexibly. Then you will find its advantages and will gradually enjoy using this method. This article aims to introduce some of the most common commands and advanced application skills of VI.

1. Introduction to basic commands

---- 1. cursor command

K, J, H, l -- move the cursor up, down, left, and right. Although you can use the four optical keys on the right of the keyboard in Linux, it is very useful to remember these four commands. These four keys are the basic locations where the right hand is placed on the keyboard.
Ng -- jump command. 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 the command
I, A, R -- insert character commands (I = insert, A = append, r = replace) before, after, and at the cursor ).
CW, DW -- change (replace)/command for deleting words where the cursor is located (C = change, D = delete ).
X, d $, and DD -- delete a character, delete all characters from the cursor's position to the end of the line, and delete the entire line.
---- 3. Search for commands
----/String ,? String -- the command used to search for the corresponding string from the cursor to the back or forward position.
---- 4. copy Copy command
---- YY, p -- command used to copy a row to the clipboard or retrieve the content in the clipboard.

Ii. FAQs and application skills

---- 1. Read the content in/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.
---- You can also read the file content after the specified row number. For example, you can use the command ": 3R/etc/passwd" to read all the content of/etc/passwd from line 1 of the new file.
---- You can also use the following method to delete all blank 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. You need to save the modifications to the temporary file.
---- VI File
----: W/tmp/1 save all the modifications you have made. You can also save some of the modifications to a temporary file. For example, you can only save 20th ~ Store the contents between 59 rows into a file/tmp/1. You can type the following command.
---- VI File
----: 20, 59 w/tmp/1

---- 3. Use VI to edit a file, but you need to delete the contents of a large segment.
---- Use the edit command "VI file" to open the file, move the cursor to the row to be deleted, press Ctrl + G to display the row number, and then press Ctrl + G to the end, displays the row number at the end of the file.
----: Assume that the number of rows obtained twice in 23,104 5d is 23 and 1045, all contents in this period will be deleted, you can also mark the start and end lines to be deleted with the MA and MB commands, and then use the ": A, BD" command to delete them.

---- 4. Add strings at the beginning or end of the row or several lines of the entire file.
---- VI File
----: 3, $ S/^/some string/Insert "some string" at the beginning of the line from the first row to the last row 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 ".
----: 3rd S/string1/string2/Replace "string1" in rows 7th to of the file with "string2 ".
---- Note: S is substitute, % indicates all rows, and G indicates global.

---- 5. Edit two files at the same time, copy the text in one file and paste it into another file.
---- VI file1 file2
---- YY copies the row at the cursor of file 1
----: 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.
---- Use the command ": % s #/usr/bin # G" to replace all the paths/usr/bin in the file with/bin. You can also run the command ": % S/usr/bin // bin/g", where "" is an escape character, it indicates that the subsequent "/" character is a meaningful character, not a separator.

Vim editor commands

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.