Vim common operation and shortcut key Skills Summary

Source: Internet
Author: User
Tags first string save file

VI(VIM) is a very common editor on Linux, and many Linux distributions have VI (VIM) installed by default. VI (VIM) commands are numerous but if used flexibly it will greatly improve efficiency. VI is the abbreviation for "Visual Interface", Vim is VI improved (enhanced version VI). In general system Management Maintenance VI is sufficient, if you want to use code highlighting, you can use VIM.

VI has 3 modes: insert mode, command mode, low line mode

1) Insert mode: You can enter characters in this mode and press ESC to return to command mode.

2) Command mode: You can move the cursor, delete characters and so on.

3) Low-line mode: can save files, exit VI, set VI, Find and other functions (low-line mode can also be seen as a command mode).

command to enter VI
VI FileName: Opens or creates a new file and places the cursor at the beginning of the first
VI +n FileName: Opens the file and places the cursor at the beginning of nth
VI + FileName: Opens the file and places the cursor at the beginning of the last line
VI +/pattern FileName: Opens the file and places the cursor at the first string that matches the pattern
Vi-r FileName: The last time you were editing with VI, a system crash occurred, restoring filename
vi-o/o filename1 filename2 ... : Open multiple files, edit them sequentially

VI Close File

: w//Save File
: w vpser.net//Save to Vpser.net file
: Q//Exit editor, if the file has been modified please use the following command
: q! Exits the editor without saving
: Wq//Exit editor, and save file

Move Cursor Class command
H: Move the cursor one character to the left
L: Move the cursor right one character
Space: Move the cursor right one character
Backspace: Cursor moves left one character
K or ctrl+p: Move the cursor up one line
J or CTRL + N: Move the cursor down one line
Enter: Move the cursor down one line
W or W: Move the cursor right one word to the beginning of the word
B or B: The cursor moves left one word to the beginning of the word
E or E: Move the cursor right one word to the end of the word
): Move the cursor to the end of the sentence
(: The cursor moves to the beginning of the sentence
}: Move the cursor to the beginning of the paragraph
{: Cursor moves to end of paragraph
NG: Cursor moves to the beginning of nth
n+: The cursor moves down n rows
N: Move the cursor up n rows
n$: Cursor moves to end of Nth line
H: Move the cursor to the top row of the screen
M: Move the cursor to the middle line of the screen
L: The cursor moves to the last line of the screen
0: (note is the number 0) cursor moves to the beginning of the current line
$: Cursor moves to the end of the current line

n+//Jump down N rows
N//Jump up N rows
NG//jumps to rows with line number n
G//Jump to the bottom of the file

Screen Tumbling Class command
Ctrl+u: First half screen to file
Ctrl+d: Half-screen to the end of the file
CTRL+F: Flip a screen to the end of a file
ctrl+b; Turn one screen to the top of the file
NZ: Rolls line N to the top of the screen and scrolls the current line to the top of the screen when n is not specified.

Insert Text Class command
I: Before the cursor
I: At the beginning of the current
A: After the cursor
A: At the end of the current line
O: A new line below the current line
O: New row above the current line
R: Replace the current character
R: Replaces the current character and its characters until the ESC key is pressed
S: replaces the specified number of characters with the input text starting at the current cursor position
S: Deletes the specified number of rows and replaces them with the input text
NCW or NCW: Modifies a specified number of words
NCC: Modifying a specified number of rows

Copy, paste

YY//Copy the current line to the buffer, you can also use "Ayy copy," A is a buffer, a can also be replaced by any letter A to Z, you can complete multiple replication tasks.
Nyy//Copy the current row down n rows to the buffer, or you can use "Anyy copy," A is a buffer, a can also be replaced with any letter A through Z, you can complete multiple replication tasks.
YW//Copy the character from the beginning of the cursor to the ending.
NYW//Copy n words starting from the cursor.
y^//Copy the contents from the cursor to the beginning of the line.
y$//Copy the contents from the cursor to the end of the line.
P//Paste the contents of the Clipboard after the cursor, if the previous custom buffer is used, it is recommended to use the "AP to paste."
P//Paste the contents of the Clipboard before the cursor, if the previous custom buffer is used, it is recommended to use the "AP to paste."

Text substitution

: S/old/new//Replaces the first occurrence of old in a row with new
: s/old/new/g//Replace all old in line with new
: n,m s/old/new/g//Replace all old from N to M with new
:%s/old/new/g//Replace all old in current file with new

Simple substitution expressions
:%s/four/4/g
The "%" range prefix means that the substitution is performed on all rows, and the Last "G" Mark represents all the matching points in the replacement row, and if only the current row is manipulated, the% can be removed


If you have a word like "thirtyfour", the above command will go wrong. In this case, the word will be replaced with "Thirty4″." To resolve this problem, use "<" to specify the beginning of the matching word:

:%s/<four/4/g
Obviously, this will still make a mistake when dealing with "fourty". Use ">" to solve this problem:
:%s/<four>/4/g
If you are coding, you may just want to replace "four" in the comment, while preserving the code. Since this is difficult to specify, you can add a "C" tag to the replacement command so that Vim prompts you before each replacement:
:%S/<FOUR>/4/GC

Word Exact match substitution

Sed-e "s/\<old\>/new/g" file

Delete command
NDW or NDW: Delete the n-1 characters at the beginning and after the cursor
Do: Delete to the beginning of the line
d$: Delete to end of line
NDD: Deletes the current line and its subsequent n-1 rows
X or x: Deletes a character, x deletes the cursor, and x deletes the cursor before the
Ctrl+u: Delete text entered under input mode

X//Delete current character
NX//delete n characters starting from the cursor
DD//Delete when moving forward
NDD//Down Delete the current row, including n rows
U//Undo Previous action
U//Undo all operations on the current line

Search and Replace commands
/pattern: Searches for pattern at the end of the file from the beginning of the cursor
? pattern: Searches for pattern from the beginning of the cursor to the top of the file
N: Repeat the last search command in the same direction
N: Repeats the last search command in the opposite direction
: s/p1/p2/g: Replaces all P1 in the current row with P2
: n1,n2s/p1/p2/g: All P1 in line N1 to N2 are replaced with P2
: g/p1/s//p2/g: Replace all P1 in the file with P2

Option settings
All: List all option settings
Term: Set terminal type
Ignorance: ignoring case in search
List: Display tab stops (CTRL+I) and end-of-line flags ($)
Number: Show line numbers
Report: Displays the number modified by the line-oriented command
Terse: Displays a short warning message
Warn: Displays no write message if the current file is not saved when you go to another file
Nomagic: Allows special characters that are not preceded by "/" to be used in search mode
Nowrapscan: Prohibit VI from the other end when the search reaches the end of the file
MESG: Allow VI to display information that other users write to their terminal using write

Last line mode command
: N1,N2 CO N3: Copy the contents of the N1 line to the N2 row below the N3 line
: n1,n2 m N3: Move the contents of the N1 line to the N2 line below the N3 line
: n1,n2 d: Delete the contents of N1 rows to N2 rows
: w: Save current file
: E filename: Open file filename for editing
: x: Save current file and exit
: Q: Exit VI
: q!: Do not save file and Exit VI
:!command: Execute shell command
: n1,n2 W!command: The contents of the N1 line to the N2 line in the file are entered as command and executed, and if N1,N2 is not specified, the entire file content is entered as the command
: R!command: The output of command commands is placed in the current line

Register operation
"? Nyy: Saves the contents of the current row and its next n rows to the register?" , where? is a letter, n is a number
"? NYW: Saves the current line and its next n characters to the register?" , where? is a letter, n is a number
"? Nyl: Saves the current line and its next n characters to a register?" , where? is a letter, n is a number
"? P: Remove the Register?" and place it at the cursor position. Over here? Can be a letter, or it can be a number
NDD: Deletes the current line and its total n lines of text, and places the deleted content in the 1th delete register.

Vim ~/.VIMRC into the configuration file

If you don't know where the VIMRC file is, you can use the: Scriptnames to view

Set Nu #行号

Set tabstop=4 #一个tab为4个空格长度

Set AI #设置自动缩进

Syntax on #高亮

Reprinted from:

http://blog.csdn.net/sunboy_2050/article/details/6002837

Reference recommendation:

Vim General Command Summary

Tips for VIM editing commands

Vim's split-screen function

VI shortcut key must know must be

Using the VI (VIM) editor on Linux tutorial

Vim User MANUAL (vim owner's manual)

Vim configuration file VIMRC

Emacs shortcut keys

Vim common operation and shortcut key Skills Summary

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.