Common vim operations

Source: Internet
Author: User
Article Title: Common vim operations. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Vi is a visual editor. The meaning of the visual editor is that you can see them when editing text. Vim in Linux is an enhanced version of vi. It has features such as color and highlight, which is very helpful for programming.

Basic usage:

Start and exit vim

Start vim: At a Linux prompt, type vim (or use vim myfile to edit an existing file) to start it.

Vim Exit: Press Esc to return to command line mode, and then type ":". The cursor stays in the bottom line, type "q" again (if forced exit, add "! "), And then press Enter.

VIM commands on mobile

Move the cursor effectively in the current row

* Fx: move the cursor to the next x of the current row. Obviously, x can be any letter, and you can use; to repeat your previous f command.

* Tx: similar to the preceding command, but it is moved to the left of x. (This is really useful)

* Fx: similar to fx, it is used to search back.

* W: move the cursor forward to a word.

* B: Move a word behind the cursor.

* 0: move the cursor to the beginning of the current row.

* ^: Move the cursor to the first letter of the current row.

* $: Move the cursor to the end of the row.

*): Move the cursor to the next sentence.

* (: Move the cursor to the previous sentence.

Move the cursor effectively throughout the file

* : Move a screen down.

* : Move the screen up.

* G: to the end of the file

* NumG: move the cursor to the specified row (num ). (For example, 10 Gb is to 10th rows)

* Gg: to the beginning of the file

* H: move the cursor to the screen

* M: move the cursor to the center of the screen

* L: move the cursor to the screen

**: Read the string at the cursor and move the cursor to where it appears again.

* #: It is similar to the above, but it is looking in the opposite direction.

*/Text: search for the string text from the current cursor and reach the place where the text appears. You must press enter to start the search command. If you want to repeat the previous search, press n.

*? Text: similar to the above, but in the opposite direction.

* Ma: Mark a bookmark with the name a at the current cursor position. Only lowercase letters are allowed. You can't see the existence of bookmarks, but it is already there.

* 'A: Go to bookmarks. Note that this is not a single quotation mark. It is generally located on the left of 1 of most keyboards.

* '.: The last time you edited the file. This command is useful, and you do not need to mark it yourself.

Valid text for moving large segments

* V: select by character. Frequently used mode, so try it yourself.

* V: select by row. This is especially useful when you want to copy or move many lines of text.

* : Select by block. Very powerful. This function is available only in a few editors. You can select a rectangle and the text in the rectangle is highlighted.

When selecting a mode, use the arrow keys and commands described above (motion ). For example, vwww highlights the three words in front of the cursor. Vjj will highlight the current line and the following two lines.

Once you highlight the selection area, you may want to perform some operations:

* D: paste the selected content to the clipboard.

* Y: copy the selected content to the clipboard.

* C: paste the selected content to the clipboard and enter the insert mode.

If you know exactly what you want to copy or cut, you do not need to enter the visual selection mode. This will save time:

* D {motion}: Cut the characters crossed by the motion command to the clipboard. For example, dw cut a word and dfS cut the character from the current cursor to the next S to the clipboard.

* Y {motion}: similar to the above, but it is a copy.

* C {motion}: similar to d {motion}, but enters the insert mode at last.

* Dd: Cut the current row.

* Yy: copy the current row.

* Cc: Cut the current row and enter the insert mode.

* D: cut from the cursor position to the end of the row to the clipboard.

* Y: copy the current row.

* C: similar to D, and finally enters the insert mode.

* X: Cut the current character to the clipboard.

* S: similar to x, but enters the insert mode at last.

Y + y copies the current position of the cursor in the whole row

Y + w: copy the cursor to the entire word.

If n + y + w is pressed by 3yw, the cursor is located at the end of the word and the last two words (three words in total) are copied together.

If n + y is pressed by 3yy, the line at the cursor position is copied together with the following two lines.

Pasting is simple. Press p.

Use multiple clipboard

Many editors only provide one clipboard. There are many VIM instances. The clipboard is called a register in VIM ). You can list all register names and their contents defined currently. The command is ": reg ". It is best to use lower-case letters as the register name, because some of the upper-case letters are occupied by VIM. The register command is double quotation marks.

For example, we want to copy the current row to register k. You should press "kyy. (You can also use V "ky. Why ?) Now the current row should already exist in register k until you copy something into Register k. Now you can use the "kp" command to paste the content in register k to the desired position.

Commonly used delete text commands in vim:

D + the left arrow key continuously presses d and the left arrow key to delete the character before the cursor position

D + right arrow key: Delete the character at the cursor position

D + The up arrow key deletes the row where the cursor is located and the previous row at the same time.

D + the arrow key to delete both the cursor position and the next row

D + d: press d twice to delete the row where the cursor is located. If the row is deleted consecutively, press and hold d.

D + w: delete the word at the cursor position. If the cursor is between two words, delete a character behind the cursor.

N + d Delete rows including cursor and country

N + d + the arrow key is used to delete the rows that contain the cursor and n rows that are up.

The n + d + downstream direction keys are the same as the n + d commands.

D. Delete all words behind the row where the cursor is located

X deletes the last character from the cursor location.

X deletes the character before the cursor position

N + x deletes the cursor position and n characters after it

N + X deletes the cursor position and n characters before it

Commonly used commands in vim to find the number of rows:

Ctrl + g display the number of rows at the cursor position and the total number of rows in the article in the last row

NG move the cursor to n rows

R: Modify the character of the cursor

R: Modify the character at the cursor position. You can always replace the character and Press ESC.

U indicates restoration.

U cancels all changes made to the row

. Repeat the previous command.

Z + Z press Z twice consecutively, the file will be saved and exit vim

% Symbol matching function. If you enter "% (", the system will automatically match the corresponding ")" during editing # This function is not implemented in ubuntu, I don't know who can explain it.

# % Can be replaced/

Search and replace commands in vim:

/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.

[1] [2] [3] [4] Next page

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.