Vi/vim Key Description

Source: Internet
Author: User
Tags file permissions

Vi/vim Key Description

In addition to the simple example above I, ESC,: Wq, in fact, Vim has a lot of keys can be used.

Part I: General mode available cursor movement, copy and paste, search and replace, etc.
How to move the cursor
h or LEFT ARROW key (←) The cursor moves one character to the left
J or DOWN ARROW key (↓) Move the cursor down one character
K or Up ARROW key (↑) Move the cursor up one character
L or right ARROW key (→) The cursor moves one character to the right
If you put your right hand on the keyboard, you will find that the HJKL are arranged together, so you can use the four buttons to move the cursor. If you want to move multiple times, such as moving down 30 lines, you can use the "30j" or "30↓" combination of keys, that is, plus the number of times you want to do (number), press the action!
[Ctrl] + [f] Screen "Down" to move one page, equivalent to [PAGE DOWN] key (common)
[Ctrl] + [b] Screen "Up" to move one page, equivalent to [PAGE UP] key (common)
[Ctrl] + [d] Screen "Down" to move half a page
[Ctrl] + [u] Screen "Up" move half page
+ Cursor moves to the next line of non-whitespace
- Cursor moves to the previous line of non-whitespace
N<space> That n means "number", for example 20. When you press the number and then press the SPACEBAR, the cursor moves the n characters of the line to the right. For example 20<space>, the cursor moves 20 character distances to the back.
0 or function key [Home] This is the number "0": Move to the front of the line prompt (common)
$ or Function key [End] Move to the last face of this line prompt (common)
H The first character of the line that the cursor moves to the top of the screen
M The first character of the line that the cursor moves to the center of the screen
L The first character of the line that the cursor moves to the bottom of the screen
G Move to the last line of this file (common)
NG N is a number. Move to the nth row of this file. For example 20G moves to line 20th of this file (mates: Set Nu)
Gg Move to the first line of this file, equivalent to 1G Ah! Common
N<enter> N is a number. Move the cursor down n rows (common)
Search replacement
/word Look under the cursor for a string called Word. For example, in the file search Vbird This string, you can enter/vbird! Common
? word Look for a string with the string name word on top of the cursor.
N This n is the English key. Represents the action of repeating the previous search. For example, if we have just executed/vbird to search for Vbird this string, then pressing N will continue to search down the next string named Vbird. If the Vbird is executed, then pressing N will continue to search for the string named Vbird!
N This N is the English key. In contrast to N, the previous search action for "reverse". For example/vbird, pressing N means "up" to search for Vbird.
Using/word with N and n is extremely helpful! Can let you repeat to find some of the keywords you search!
: n1,n2s/word1/word2/g N1 and N2 are numbers. Look for the string word1 between N1 and N2, and replace the string with Word2! For example, search for vbird between 100 and 200 rows and replace it with Vbird:
": 100,200s/vbird/vbird/g". Common
: 1, $s/word1/word2/g Look for the word1 string from the first line to the last line and replace the string with Word2! Common
: 1, $s/word1/word2/gc Look for the word1 string from the first line to the last line and replace the string with Word2! and display the prompt character before the substitution to the user to confirm (confirm) whether need to replace! Common
Delete, copy and paste on
X, X In one line of words, X is the backward deletion of a character (equivalent to the [Del] key), and X is to delete a character (equivalent to [backspace], which is the backspace) (common)
Nx N is a number that continuously deletes n characters backwards. For example, I want to delete the 10 characters consecutively, "10x".
Dd Delete the entire row where the cursor is located (common)
Ndd N is a number. Delete the down n rows where the cursor is located, for example, 20DD to delete 20 rows (common)
d1g Remove all data from the first row of the cursor
Dg Delete all data from the last row of the cursor
d$ Delete the last character of the row where the cursor is located
D0 That is 0 of the number, delete the cursor at the top of the line, and the first character
Yy The row where the cursor is copied (common)
Nyy N is a number. The next n rows where the cursor is copied, for example, 20yy copies 20 rows (common)
y1g Copy all data from the row to the first row of the cursor
YG Copy all data from the row of the cursor to the last row
Y0 Copy all data from the same character as the cursor to the beginning of the line
y$ Copy all data at the end of the line with the same character as the cursor
P, p p to paste the copied data on the next line of the cursor, and p for a row on the cursor! For example, I currently have the cursor on line 20th and have copied 10 rows of data. When P is pressed, the 10 rows of data will be affixed to the original 20 lines, i.e. 21 lines. But what if we press P? Then the original 20th Guild was pushed into 30 rows. Common
J Combines data from the row of the cursor and the next row into the same row
C Repeatedly deleting multiple data, such as deleting 10 rows down, [10CJ]
U Restores the previous action. Common
[Ctrl]+r Redo the last action. Common
This u with [ctrl]+r is a very common instruction! One is recovery, the other is redo once ~ Use these two function keys, your editor, hey! Very happy!
. Don't doubt it! This is the decimal point! To repeat the meaning of a previous action. If you want to repeat the deletion, repeat the paste, and so on, press the decimal point "." It's all right! Common
Part Two: General mode switch to edit mode available button description
Enter or replace the edit mode
I, I Enter input mode (Insert modes):
I is "input from the current cursor," And I is "start typing at the first non-whitespace space where you are currently in line." ( Common)
A, a Enter input mode (Insert modes):
A is "Enter at the next character where the cursor is currently located" and A is "start at the last character of the line where the cursor is located." ( Common)
O, O Enter input mode (Insert modes):
This is the case of the English letter O. o Enter a new line at the next line where the cursor is currently located; o Enter a new line for the previous line where the cursor is currently located! ( Common)
R, R Enter Replace mode:
R will only replace the one character where the cursor is located, and R will always replace the text of the cursor until ESC is pressed; ( common)
above these keys, in the lower left corner of the VI screen will appear "--insert--" or "--replace--" words . The action is known by the name!! In particular, we mentioned above, you want to enter characters in the file, be sure to see the lower left corner of INSERT or REPLACE to enter Oh!
[ESC] Exit edit mode, return to normal mode ( Common)
Part III: Description of available buttons for general mode switch to command line mode
Instructions for storing, leaving, etc. of the command line
: W Write the edited data to the hard drive archive ( Common)
: w! If the file property is read-only, the archive is forced to be written. However, in the end can be written, or with you on the file permissions related Ah!
: Q Leave VI ( common)
: q! If you have modified the file, do not want to store, use! Do not store files for forced departures.
Note that the exclamation point (!) in VI, often has the meaning of "mandatory" ~
: Wq To leave after storage, if: wq! After forcing the store to leave ( Common)
Zz This is the capital Z! If the file does not change, then do not store away, if the file has been changed, then save and leave!
: w [filename] Save edited data to another file (similar to save new file)
: R [FileName] In the edited data, read the data from another file. The file "filename" will be added to the cursor row after the
: n1,n2 w [filename] Store the contents of N1 to N2 into the filename file.
:! Command Temporarily leave VI to command line mode to perform command display results! For example
『:! Ls/home "You can see in VI the file information under/home with LS output!
Changes to the VIM environment
: Set Nu Displays the line number, which, when set, displays the line number of the row in the prefix of each row
: Set Nonu In contrast to set NU, the line number is canceled!

Pay special attention to the Vi/vim, the number is very meaningful! A number usually means repeating it a few times! It is also possible to represent the meaning of going to the first few what.

For example, to delete 50 rows, use "50DD"! The number is added before the action, if I want to move down 20 rows? That's "20j" or "20↓".

1 find
/xxx (? xxx)   means searching the entire document for a string that matches xxx,/means looking down,? means looking up where xxx can be a regular expression, about the regular type is not much to say. Generally case-sensitive, to be case-insensitive, you must first enter: Set IgnoreCase find later, enter N to find the next match, enter n reverse Direction lookup * (#)    when the cursor rests on a word, enter this command to find the next (top) word matching the word, then enter N to find the next match, enter N to find the opposite direction g* (g#)       This command is similar to the previous command except that it does not exactly match the word at the cursor, but rather matches all strings containing the word GD              This command finds words that match the word in the cursor and places the cursor where the word first appears in the non-comment segment of the document.     This command looks up the parentheses that match the cursor, including () [] {}f (f) x           This command indicates that the cursor is located on the row where the pointer is found, and the first X-character of the right (left) side of the lookup cursor is located: input; Continue to search for input, indicating

2 Quick Move cursor
In VI, moving the cursor and editing is two things, because of the separation, so it is convenient to do cursor positioning and editing so it is very useful to move the cursor a little faster
W (e) Move the cursor to the next word b move the cursor to the previous word 0 move the cursor to the beginning of the line ^ move the cursor to the line at the             beginning of the character $ move the cursor to the             end of the             Bank H             Move the cursor to the first row of the screen m             Move the cursor to the middle line of the screen L move the cursor to             the end of the screen GG move the cursor            to the first line of the document G             move the cursor to the end of the document C-f           (that is, the CTRL key and the F key pressed together) This command is page Downc-b           (that is, CTRL Key with the B key press, after the same) This command is            very useful, this command is very helpful, it moves the cursor to the previous mark, such as GD, * and so on to find a word, and then enter this command back to the last position '             This command is quite good, it moves the cursor to the last Modify line '             This command is quite powerful, it moves the cursor to the last modification point

3 Copy, delete and paste

In vi, y means copy, d means delete, p means paste where copy and delete are combined with the cursor Movement command, see a few examples to understand

YW represents a copy of the content from the current cursor to the end of the word at the cursor the DW represents the deletion of the content from the current cursor to the end of the word at the cursor y0 means that the copy is from the current cursor to the beginning of the cursor d0 the content from the current cursor to the beginning of the cursor y$ represents copy from current cursor to cursor The content at the end of the line d$ means that the content from the current cursor to the end of the cursor is deleted YFA represents the contents of the copy from the current cursor to the first a character following the cursor the DFA represents the deletion of the contents from the current cursor to the first a character following the cursor

In particular:

YY means that the copy cursor line DD means the deletion of the cursor row d means the deletion from the current cursor to the end of the cursor line of content

About copy, delete and paste complex usage is related to register, can query by itself

4 numbers with commands

In VI, the combination of numbers and commands often means repeating this command, and if it appears at the beginning of the extension pattern, it indicates that the line number is positioned as:

5FX represents the 5th x character after the lookup cursor 5w (e)   moves the cursor to the next five words 5yy  means the copy cursor below 5 lines 5dd  means the deletion cursor the following 5 lines Y2FA represents the copy from the current cursor to the second after the cursor The content between the A-character: 12,24y represents the contents of the copy between lines 12th through 24th: 12,y represents the contents of a copy of line 12th to the line where the cursor is located: 24y means that the contents of the copy cursor line to the 24th row are deleted like

5 Fast input characters

In VI, do not require you to enter every character, there are many ways to quickly enter some characters to use Linux/unix's classmates must have an experience, typing the first few characters at the command line and pressing the TAB system will automatically fill the remaining characters, if there are multiple matches will print out This is the well-known command to complement (in fact, Windows also has a file name in the function) VI has a lot of string command, very convenient

C-p (c-n) in edit mode, enter a few characters and then enter this command then VI begins to search for the word that matches the beginning of the start (bottom) and fill it up, continuously enter this command to loop through this command will be in all the files opened in this Vim program to match c-x-l in edit mode, This command quickly complements the line contents, but only matches in the document that appears in this window c-x-f in edit mode, this command indicates that the file name is entered as input:/usr/local/tom and then enter this command to automatically match it:/USR/LOCAL/TOMCAT/ABBR That is, the abbreviation this is a macro operation, you can use an abbreviation in the editing mode instead of another string such as writing Java files often input systemoutprintln, which is very troublesome, so should be abbreviated to reduce the word can do this: abbr sprt Systemoutprintln after entering Sprt and then entering other non-alphabetic symbols, it will automatically expand to Systemoutprintln

6 Replacement

Replacement is the strength of VI, because you can match strings with regular expressions Here are a few examples

: s/aa/bb/g replaces all AA-containing strings in the line of the cursor with the bb:s/\/bb/g to replace  all AA occurrences with BB, replacing only the word AA:%s/aa/bb/g will All occurrences of AA in a document that contain AA replaced by bb:12,23s/aa/bb/g will replace the AA in all strings containing AA from 12 rows to 23 rows with bb:12,23s/^/#/to add the # character from the beginning of 12 lines to 23 lines: %s= *$== Remove any extra space at the end of the line: g/^\s*$/d Delete all empty lines that do not contain characters (spaces are not included)

Vi/vim Key Description

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.