Efficient use of the VIM editor--vim Editor common commands Learn notes

Source: Internet
Author: User

Mastering the common commands of the Vim editor can greatly improve the efficiency of our text editing. Basic Operations

When we first contacted the Vim editor, we learned some of the following things:

Just enter the Vim editor, we are in Normal mode , press I on the keyboard, we enter the insertion mode . In insert mode, if you press ESC, you are back in normal mode.

In insert mode, we can type text.

In normal mode, we can use some commands to quickly manipulate our text, such as:

x: Deletes one character from the current cursor

: Wq: Save and exit (: W save,: Q Exits, and with: Start command requires input carriage return)

DD: Deletes the current row and saves the deleted row to the Clipboard

p: Paste the contents of the Clipboard

H (left), J (down), K (UP), L (right) move the cursor, of course, we can also use ↑, ↓, ←, → to move

further

Having learned the basics of the Vim editor, we need to learn more commands to go further.

1. Different insertion modes

I: Insert a at the cursor position after the cursor is inserted

o: Inserts a new row after the current line

o: Inserts a new line before the current line

CW: Delete the cursor position before the next word, and enter insert mode

Example: CW command

Cursor Current position

After using the CW command

2. Fast Move cursor
Want to quickly move the cursor to improve efficiency, we move the cursor operation should not be limited to H, J, K, L/↑, ↓, ←, →.

0 (number 0): Move to wardrobe

^: Move to the bank the first position that is not a blank character is

$: move to the line end of the bank

G_: Move to the last position of the bank that is not a blank character.

/The string to find: Find string (if multiple matches are searched, press N to move to next)

Example: Finding a string

Finds the Thread string in the open text, and the command is/thread

The found results will be highlighted, we can press N to move to the next match (: Nohl command suppresses highlighting)

3. Copy/Paste

p/p: Paste (P and P is the difference between p is pasted to the current position, p is pasted before the current position)

yy: Copy the contents of the current line

4. Withdrawal/Recovery

U:undo, undo the previous Operation

Ctrl+r:redo, redo the previous operation

5. File Open/Save/exit

: E filename: Open a file in the Vim editor

: SaveAs filename: Save as

: X,zz or: Wq: Saving and exiting (ZZ does not need to enter a colon and carriage return)
: X and: Wq The difference is: Wq will be forced to write the text Pieces and exit, even if the file has not been modified and forced to write, and update the file's modified time

: q!: Quit without saving 
: qa!: Forcibly quit all the files being edited, even if other files have changed

: bn and: BP: When Vim opens many files at the same time, You can use these two commands to switch to the next or previous file

Example: opening multiple Files

First use the command on the command line vim thread.cc Open the file in the Vim editor thread.cc

In vim Normal mode, enter: E./atomic.h, the file in the current directory is opened Atomic.h

Raise Efficiency

1. Repeat Operation
In some cases, we need to repeat for an operation many times, such as deleting multiple lines, entering the same line of text multiple times ... So how is vim repeated operation?

One way is to press., you will repeat the last order;
Another way is to N+command, enter the number of times you want to execute a command, and the command executes N times.

Example:
2DD: Delete 2 lines

4p: Paste text 4 times

10ilove<esc>: Writes "Lovelovelovelovelovelovelovelovelovelove" ( The I in the command is the switch to insert mode
, and then press., and it will be written 10 times "Love"

2. Move the cursor faster
In addition to some of the fast-moving cursor commands we've described above, there are some faster, more mobile cursor movement commands that we should know about.

GG: Move to the beginning

of the text G: Move to the end of the text

n+g or: N: Move to Nth line

W: move by word, move to the beginning of the next word

e: move by word, move to the end of the next word

We would like to highlight the following two cursor move commands:

%: matching parentheses move, including (, {, [. (You need to move the cursor to parentheses before using)

* and #: match the current word of the cursor, move the cursor to the next (or previous) matching position (* is next, #是上一个)

Example: use% to match bracket movement
First move the cursor to a bracket

Press% to see the cursor move to the bracket where the brackets match

Example: use * to match words and move between words
Before using, we need to move the cursor to a word, we move to Thread

Then press *, you can see the Thread all the matches are highlighted, we can continue to press * move to the next match

3. Combined use of commands
We can use the cursor movement commands we have learned to work with other commands to achieve efficient editing, such as the combination of the following cursor movement and the copy command:

(1) 0y$ command means:

0→ first to the wardrobe
y→ from here to copy
$→ copy to the bank last character

(2) The Ye command means that the last character (3) that is copied from the current position to the word

can also be entered y2/ Foo to copy the string between 2 "foo"

Example: Use a command in an open text Y2/thread
The red box identifies the current position of the cursor and the location of the adjacent two "Thread" strings

Execute Command y2/thread

Move the cursor to a blank line, press paste action p to see what is copied

4. Auto-complement
For example: We want to enter Thread, when we type the beginning of the word, then press CTRL+P or CTRL + N, we can enter the automatic completion of the selection.

5. Visual Selection
Press V, V, or CTRL + + in normal mode to enter visual mode. In visual mode, as the cursor moves, you can select rows, blocks, and regions.

Once selected, we can perform some operations, such as Y replication, d deletion, and some of the actions described below

J: Connect all selected rows (into one line)

< or >: Indent left or right

=: Auto Indent

gu: Turn lowercase

gu: uppercase

Example: Select a few lines of content and turn it into uppercase
Go to the visual line, select the contents

Execute Command GU

Resources:
Cool shell-Concise Vim training strategy: http://coolshell.cn/articles/5426.html

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.