Diagram of common vi/vim commands

Source: Internet
Author: User
: Setnu indicates that the line number encoding is mostly used in Windows editors, such as various IDEs, SourceInsight, and UE. in actual work, even if you compile a program in Linux, you can also select the SAMBA sharing method to encode it in Windows. Generally, it is not normal to use a Linux editor (such as Vim/Emacs ).

: Set nu display row number

Encoding is mostly used in Windows editors, such as various IDEs, SourceInsight, and UE. in actual work, even if you compile a program in Linux, you can also select the SAMBA sharing method to encode it in Windows. Generally, we think that if we can use an editor in Linux (such as Vim/Emacs) to work, we are not the average person. since we are so obsessed with Linux, the level will certainly not be too bad. However, the usage of Linux does plague many people. even Bruce Eckel, author of ThinkingIn Java, admitted that Cygwin has brought a lot of convenience to him, at the same time, I lamented that it would be nice to have these Windows tools when he was dizzy by GnuEmacs more than a decade ago. Of course, he must be familiar with Emacs, and he is not a beginner's dizzy.

Even if you can use Windows to solve the daily coding work, it is inevitable that you need a Linux editor, such as viewing a configuration file, changing a configuration script, and writing a test. c tests a small function. if it can be done directly in Linux, it is still very good, at least it saves time. In addition, sometimes SAMBA sharing is not always good. Therefore, you can grasp the editing commands of vi/vim, at least in these cases.

Vi/vim relies mainly on the keyboard and has many commands. if it is not often used, it cannot be remembered, but it also causes great troubles. For example, after opening a file in vi, the default mode is not the editing mode and the content cannot be modified. if you do not know how to switch to the editing mode, you will feel helpless. The hardest hit is that you don't even know how to operate even if you quit, so you have to turn off the shell and open another one, leaving a psychological shadow. These experiences are believed to be met by many people when vi/vim is used for the first time, or at the beginning. after a long time, you don't have to forget it. In the end, you will be able to get away with it and think about other methods, even if it takes more time, it can be done.

Which of the following vi/vim commands should be remembered? With this, you can enter (open the file) and exit (Exit elegantly) freely )? I don't want to be familiar with all kinds of shortcuts, but just want to be able to cope with basic editing. I believe this is a demand for vi by many people.

I think the following six commands should be remembered.

(1): set nu displays the row number. after this parameter is set, the row number is displayed at the beginning of each row.

(2) move G to the last line of the file (commonly used ). NG n is a number. The nth row of the file. For example, 20 GB will be moved to the 20th rows of this file (can be used with: set nu)

Move the 15G cursor to 15th rows

(3) dd deletes the entire row of the cursor (commonly used ). DG deletes all data from the row where the cursor is located to the last row. if the cursor is already in the first row (for example, when a file is opened), dG deletes all data.

DG deletes all content from the row where the cursor is located to the last row

(4) enter editing mode (input file content mode): insert by I or I. Insert the input text at the place where the current cursor is located. The existing text will be backward. Here, I is "insert from the current cursor" and I is "insert from the first non-blank character in the current row 』. (Common)

(5) exit the Editing mode: press ESC to enter the normal mode. this mode is used when vi starts to open the file, in this mode, you can perform operations such as displaying the row number and deleting the entire row in the above column.

(6) q leaves vi (commonly used ). : Q! If you have modified an archive and do not want to store it, use it! To force exit without storing files. Note that the exclamation point (!) In vi, it often means "forced ~ . : Wq is stored and left. if it is: wq! It is forcibly stored and then left, or: x (commonly used)

More commands are listed below for your reference:

General mode: method for moving a cursor

H or move a character to the left from the cursor
J or downward arrow key (cursor) move a character downward
K or the up arrow key (forward) cursor moves a character up
L or right Direction Key (→) move a character to the right of the cursor
If you want to move multiple times, for example, move 30 rows down, you can use a combination of "30j" or "30 Gbit/s", that is, after adding the number (number) you want, press the action!
[Ctrl] + [f] the screen moves one Page Down, which is equivalent to a [Page Down] button (commonly used)
[Ctrl] + [B] the screen moves one Page Up, which is equivalent to a [Page Up] button (commonly used)
+ Move the cursor to the first non-null character in the next row
-Move the cursor to the first non-null character in the previous line
N that n represents a number, for example, 20. Press the number and then press the blank key. the cursor moves the n characters in the row to the right. For example, if the cursor is 20, the distance between 20 characters is moved to the backend.
0 this is the number "0": move to the top character of this line (commonly used)
$ Move to the last area of the line (frequently used)
H move the cursor to the top row of the screen
M cursor moves to the middle line of the screen
L move the cursor to the bottom line of the screen
G moves to the last line of the file (frequently used)
NG n is a number. The nth row of the file. For example, 20 GB will be moved to the 20th rows of this file (can be used with: set nu)
N is a number. Move the cursor down n rows (commonly used)

General mode: Search and replace

/Word searches for a string named word under the cursor. For example, to search for the vbird string in the file, enter/vbird! (Common)
? Word searches for a string named word on the cursor.
N is an English button. Indicates the meaning of "repeating the previous search action. For example, if we execute/vbird to search for the vbird string, press n to continue searching for the next string named vbird. If it is execution? Press n to search for a string named vbird!
N is an English button. Opposite to n, this is the previous search action for "reverse. For example, after/vbird, press N to search for vbird "up.
: N1, n2s/word1/word2/g n1 and n2 are numbers. Search for the word1 string between line n1 and line n2 and replace it with word2! For example, search for vbird between 100 and 200 and replace it with VBIRD:
": 100,200 s/vbird/VBIRD/g 』. (Common)
: 1, $ s/word1/word2/g searches for the word1 string from the first row to the last row, and replaces this string with word2! (Common)
: 1, $ s/word1/word2/gc searches for the word1 string from the first row to the last row, and replaces this string with word2! The prompt character is displayed before replacement to the user to confirm (conform) whether to replace! (Common)

General mode: delete, copy, and attach

X, X in a row, x is to delete a character (equivalent to a [del] Key), X is to delete a character (equivalent to [backspace] or a regressing key) (Common)
Nx n is a number and n characters are deleted consecutively. For example, I want to delete 10 characters in a row, "10x 』.
Dd deletes the whole row of the cursor (commonly used)
Ndd n is a number. Delete the next n rows of the cursor. for example, 20 DD deletes 20 rows (commonly used)
D1G delete all data from the row where the cursor is located to the first row
DG deletes all data from the row where the cursor is located to the last row.
D $ delete the cursor to the last character of the row
D0 is the number 0, where the cursor is deleted, to the beginning of the row, a character
Yy copy the row of the cursor (commonly used)
Nyy n is a number. Copy the next n rows of the cursor. for example, if 20 YY is used, 20 rows are copied (commonly used)
Y1G copy all data from the row where the cursor is located to the first row
YG copies all data from the row where the cursor is located to the last row.
Y0 copies the character of the cursor to all data at the beginning of the row.
Y $ Copy all data from the character of the cursor to the end of the row
P, P p: Paste the copied data in the next row of the cursor, and P is paste on the previous row of the cursor! For example, I have already copied 10 rows of data with 20th rows of cursor. After you press p, the 10 rows of data will be pasted after the original 20 rows, that is, the Post starts from 21 rows. But what if I press P? Then the original 20th rows will be pushed to 30 rows. (Common)
J. Combine the row where the cursor is located with the data of the next row into the same row
C. delete multiple data records repeatedly. for example, delete 10 rows down and [10cj]
U restores the previous action. (Common)
[Ctrl] + r redo the previous action. (Common)
This u and [Ctrl] + r are very common commands! One is restoration, the other is redo once ~ With these two buttons, you can edit them! Happy!
. Do not doubt! This is the decimal point! It means to repeat the previous action. If you want to delete or repeat the action, just press the "." decimal point! (Common)

Enter edit mode

I, I insert: Insert the input text in the current cursor. The existing text will be backward. where, I is "insert from current cursor 』, I is "starting to insert the first non-blank character in the current row 』. (Common)
A, A a is "insert from the next character of the current cursor", and A is "insert from the last character of the row where the cursor is located 』. (Common)
O, O is the case of the English letter "o. O: insert a new row in the next row of the current cursor. O: insert a new row in the previous row of the current cursor! (Common)
R, R replace: r will replace the character where the cursor is located; R will replace the text where the cursor is located until you press ESC; (commonly used)
In the buttons above, "?" appears in the lower left corner of the vi image 『? INSERT ?』 Or 『? REPLACE ?』 . Let's know the action by name !! Note that we have mentioned above that when you want to input a token in the file, you must see INSERT/REPLACE in the lower left corner before entering it!
Exit the editing mode of Esc and return to normal mode (commonly used)

Command column command mode

: W write the edited data into the hard drive File (commonly used)
: W! If the archive attribute is "read-only", the archive is forcibly written. However, whether the file can be written or not depends on your permissions on the file!
: Q leaves vi (commonly used)
: Q! If you have modified an archive and do not want to store it, use it! To force exit without storing files.
Note that the exclamation point (!) In vi, it often means "forced ~
: Wq is stored and left. if it is: wq! It is forcibly stored and then left, or: x (commonly used)
: E! Restore the archive to the original state!
ZZ if the file is not changed, it will not be stored and left. if the file has been changed, it will be stored and left!
: W [filename] stores edited data into another File (similar to storing new files)
: R [filename]: read the data of another file in the edited file. Add the file "filename" to the end of the row where the cursor is located.
: N1, n2 w [filename]: store the content from n1 to n2 as filename.
:! Command temporarily leaves vi to display the result of executing command in command column mode! For example 『:! Ls/home> to view the file information output by ls under/home in vi!
: Set nu displays the row number. after this parameter is set, the row number is displayed at the beginning of each row.
: Opposite to set nu, set nonu is the canceled row number!

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.