How to Use Vim

Source: Internet
Author: User

How to Use Vim

Vim, the editor highly praised by most programmers, is derived from its free, flexible, and comfortable input mode. But it is a nightmare for new users (too many commands are required ), I have been using vim for some time. I will summarize the commonly used commands to help beginners quickly enter vim and feel its powerful and fascinating processing methods.

The following describes only common usage methods. You will be interested in heavy concepts and difficult operations.

You can use the following command to open a file with vi/vim for the first time:

Move command:

1. the hjkl buttons correspond to the top left and bottom buttons, which are used to move the cursor (same as the top left and right direction keys). They can be used in combination with numbers, for example, nh (move n steps left.

2. the wb buttons are the most commonly used keys, representing the length of a word that is moved to the right to the left.

3. gg and G, where gg moves to the first row, and G moves to the last row.

4. Move nG to row n.

There are also a lot of mobile commands such as h l...

Insert text:

1. the three buttons of iao correspond to these three different insertion modes. I inserts the button before the cursor (the default cursor is square), and a inserts the button after the cursor, o starts a new row and enters the insert mode. another example is: O inserts a new line into the indicator and enters the insertion mode.

Press esc to move the command

Select text:

1. v press v to enter the selection mode from the current cursor, and then move the cursor to select.

2. Press V to select a line. The subsequent steps are the same as above.

3. ctrl + v is a very useful selector in vi. You can select blocks. You can also use hjkl or wb to select blocks later.

Text clipboard, copy, paste, and undo:

1. You can use d clipboard and y to copy the selected text.

2. dd: You can also use ndd to cut the n rows from the current start.

3. Copy the current row using yy, or use nyy.

4. p paste the clipboard content or copied content. The original format is usually retained. That is, dd or yy is used to paste the content to the next line. Partial Text Selection and pasting will be performed on the current line.

5. P paste it up in the same format as above.

6. u undo the last operation. Ctrl + r is opposite to u operation.

Relatively advanced copy and paste: (but very useful)

Vi/vim has many built-in registers for separate use.

1. "ayy saves the current row in register a," anyy copies n rows to register a or "any press Enter. The number of registers ranges from a-z to 0-9.

2. + these two registers are the system clipboard. You can copy the content to the clipboard for external use, as shown in the preceding figure: "+ yy.

3. "ap paste the content in register a. The same applies to others.

Search Mode:

1./search downward, for example, to search for test, you can use the regular expression in vim, use n to move to the next searched word, and use N to move to the last searched word.

2 .? Same as above, but n is used to search up, N is down.

Add bookmarks:

1. m. For example, mk adds the label k to the current row and uses 'k (note that it is' under esc) to jump to the k tag.

 

Command mode:

It can be said that the command mode is the core of vim, at least this is what I feel when using it now.

(Subsequent matching texts can be selected using the regular expression in vim. We will not describe it here to avoid increasing the difficulty. However, we recommend that you have a good understanding of regular expressions)

1.: % g/content to be matched/d this command deletes all the lines matching the content in the entire file. The "%" number indicates full text matching, or {n, m} g // d indicates that from n to m, n and m can be omitted. By default, it starts from the current row.

2.: s/Text to be matched/Text to be replaced/g this command is used to replace the matched text, the same method as above.

3.: s/\ (matched text \)/inserted content \ 0/g this command is different from the front command in that () is used to enclose the text to be matched, you can use \ 0 to restore the data and perform insert operations. For example, you can use \ 1 and so on.

4 .:! The cd/home command calls the cd command in bash in linux and switches it to the home directory. You can use :! Ls-l displays the file information in the current working directory.

5. reg: Check the register content in vim. You can view the register content at any time.

5: h call up the Help file, which is very useful.

Saving text files:

1. w modified content is written to the original file (A. swap temporary file will be generated DURING vim editing ).

2. q exits without saving.

3.: wq is saved and exited.

4.: w! Force write, exit command can also be used.

5.: Write if x is changed. Otherwise, exit. (Most commonly used)

Vi multi-screen:

1.: sp displays another file in Landscape mode. : Vsp is displayed in portrait mode. : Ex switch to another file. The current file disappears.

2. ctrl + w switch between the two files.

Vi merge other files:

1.: r path appends the path file to the cursor.

Add the vim replacement operation and regular expression:

1. Regular Expressions:
Replace the command: The command below s // g can be used in combination.
None: only replaces the first match in the specified range.
G: replace all matching items within the specified range.
C: Ask the user for confirmation before replacement
E: Ignore errors during execution
Ii. metacharacters:
.: Match any character
[Abc]: match any character in square brackets. You can use-to indicate the character range.
[^ Abc]: Start with a ^ symbol in square brackets to match any character except the character in square brackets.
\ D: matches Arabic numerals, equivalent to [0-9]
\ D: match any character other than Arabic numerals, equivalent to [^ 0-9]
\ X: matches a hexadecimal number, which is equivalent to [0-9A-Fa-f]
\ X: matches any character other than the hexadecimal number, which is equivalent to [^ [0-9A-Fa-f]
\ W: Match letters, equivalent to [0-9A-Za-z]
\ W: match any character other than letters, which is equivalent to [^ 0-9a-za-z]
\ T: Match <TAB> characters
\ S: matches blank characters, equivalent to [\ t]
\ S: matches non-blank characters, equivalent to [^ \ t]
* Special Note: If you need to find metacharacters, You Need To \ jump out.
Number of metacharacters
*: Match 0-any
\ +: Match 1-any
\? : Match 0-1
\ {N, m}: Match n-m
\ {N}: Match n
\ {N ,}: match n-any number
\ {, M}: Match 0 to m
Symbol indicating the position
$: Matching the end of a row
^: Match the beginning of a row
\ <: Match the first word
\>: Match the end of a word

In case of any errors, please note.

For more Vim tutorials, see the following:

Build VIM into a simple and practical IDE

Vim Learning Guide

Quick learn Vi Editor

Powerful Vim Editor

Build a Vim Development Environment on CentOS 6.2

Install the highlighted Vim editing tool in CentOS 5.4

Vim tips: C language settings

Set the Vim row number in Ubuntu

Vim editor basic tutorial

This article permanently updates the link address:

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.