Vim tips-quick Edit

Source: Internet
Author: User

Original article: Vim tips-quick Edit

 

Common basic commands related to VIM settings:

Display row number: set number or: Set nu

Hide row number: Set nonumber or: Set Nonu

Auto indent: Set autoindent or: Set AI

Not auto indent: Set noautoindent or: Set noai

Show tab and line tail characters: set list

Hide tab and line tail characters: Set nolist

Set the tab width to 4 characters: Set tabstop = 4

View Current Settings: Set all

Define the current Keyboard Command: Map keys new_seq

 

 

1. Insert

A // Enter text data after the current character position of the cursor.

A // Enter text data at the end of the row (that is, the last character position) where the cursor is currently located.

I // Enter text data before the current character position of the cursor.

I // Enter text data before the beginning of the row where the cursor is currently located (that is, the first non-blank start character.

O // Enter text data at the beginning of the row under the current row of the cursor.

O // Enter text data at the beginning of the row where the cursor is currently located.

: AB USA United States of American// When you insert USA into the file and press enter, it becomes United States of American.

 

2. Fast copy and paste/cut and paste

0) basic method:

Y $ // copy from the current position to the end of the row
YY // copy the current row
Nyy // or NY, copy the following n rows of the current row (including the current row)

Nyw // copy n words after the cursor
P // paste below the cursor

P // paste it above the cursor

DD and NDD can also be considered as "cut ".

1) Method 1:

In command mode (use set nu to display the number of trips, which is easier to handle ):

: N, m Co K // copy n to m rows and paste it to k rows (starting from the next row)
: N, m K // cut n to m rows and paste it to k rows (starting from the next row)
: N, m d // Delete n to m rows

Note that m must be greater than N, and M, N, and K are all within the body row number. For example, if there are 10 lines in the file and 11 lines in your operation, an error will occur. M can be represented by a special symbol $ to the end.


2) method 2
If you want to process a large number of lines of text, you can use the label method. The details are as follows:

Move the cursor to the starting line and enter Ma
Move the cursor to the end line and enter MB
Move cursor to paste line, input MC

Then: 'a, 'B co' C, and copy and paste are implemented. If Co is changed to M, cut and paste are implemented.

3. Delete

NX // Delete the character at the current position of the cursor, or delete the character from the current position of the cursor(Later)N characters

NX // Delete the first character in the current position of the cursor or delete the current position of the cursorBeforeN characters

DD // Delete the current row
NDD // Delete the following n rows of the current row (including the current row)
d l // Delete the current character (same as the X command)
D 0 // Delete to the start position of a row
d ^ // Delete to the first character position of a row (excluding spaces or ta B characters)

D w // Delete to the end of a word
D 3 W // Delete to the end of the third word
D B // Delete to the start position of a word
D w // Delete to the end of a word with a space as the Separator
D B // Delete to the start position of a word with spaces as the Separator
D 7 B // Delete to the start position of the first seven words with spaces as separators
D) // delete it to the end of a statement
D 4) // delete it to the end of the fourth statement
D (// Delete to the start position of a statement
D} // Delete to the end of a paragraph
D {// Delete to the beginning of a paragraph
D 7 {// Delete the first 7th paragraph positions before the start position of the current paragraph
D // Delete the current row
D/t e x T // Note: There is no space between D and. Delete the position specified in "t e x T" from the text until the next occurrence of the word (but not including the word ).
D f c // Delete the position where the character 'C' appears from the text, until the content between the position (including the character) where the next character appears
D t C // Delete the content of the current row until the next character 'C' appears (excluding this character)
D // Delete to the end of a row
D $ // Delete to the end of a row (priced at the top command)
5 d // Delete the contents of the 5 rows starting from the current row
D l // Delete the content until the last line on the screen
D H // Delete the content until the first line on the screen
D G // Delete the content until the end of the work cache area (that is, the end of the entire file)
D 1g // Delete the content until the beginning of the work cache area (that is, the beginning of the entire file)

: Line #1, line #2 D // delete all text lines from the specified line number line #1 to line #2

 

4. modify command operations
C l // change the current character
C w // modify to the end of a word
C 3 W // modify to the end of the third word
C B // modify to the start position of a word
C w // modify to the end of a word with a space as the Separator
C B // modify to the start position of a word with spaces as the Separator
C 7 B // modify to the start position of the first seven words with spaces as separators
C 0 // modify to the end of a row
C) // modify to the end of a statement
C 4) // modify to the end of the fourth statement
C (// modify to the starting position of a statement
C} // modify to the end of a paragraph
C {// modify to the start position of a paragraph
C 7 {// modify the position of the first paragraph before the starting position of the current paragraph
C TC // modify the content between the current row and the position where the next character C appears
C // modify to the end of a row
C // modify the current row

NCC // modify the n lines of text starting from the current row and end with the ESC key
5 C // modify the content of the 5 rows starting from the current row

[N] CC // Replace the current line or n lines of text starting from the current line, ending with the ESC key.

[N] S // replace a single character in the current position of the cursor, or n characters starting from the current position of the cursor, ending with the ESC key.

S // Replace the current row and end with the ESC key.

R // replace a single character at the current position of the cursor.

R <enter> // disconnect a row. You can also use the "a" or "I" command to add the enter and ESC keys.

R // replace all subsequent characters starting from the current character position of the cursor,Until you press the ESC key.

XP // delimiter. Switch the starting character position of the current position of the cursor.

DDP // swap the upper and lower rows

: G/^/M0 // reverse the File Content

~ // Convert the case sensitivity of Characters in the current position of the cursor.

J // merge the upper and lower rows

 

. // Repeat the last modification (DOT symbol)

 

5. Replace commands
S replaces the current character with one or more characters
S replaces the current row with one or more characters
5 s Replace the 5 characters starting from the current character with one or more characters

 

General replacement method: Line 1, line 2 S/string 1/string 2/g.

Here, "Row 1, Row 2" specifies the method example:
5th rows to 30th rows: 5, 30
Current row to 30th rows:., 30
5th rows to the end: 5, $
5th rows to rows containing end: 5,/end/
All rows: %

 

VI replacement rules:
: G/S1/S/s2/S3/g
The first G indicates that each row including S1 is replaced, and the second G indicates that all S2;

S indicates replacement. S2 indicates the string to be replaced. It can be the same as S1 (use // If the string is the same), and S3 indicates replacement string.

 

Supplement: search and replace

 

1. Simple replacement expression

The replacement command can replace another word with one word in the full text:

: % S/four/4/g

The prefix of "%" indicates that replacement is performed in all rows. The last "G" Mark replaces all matching points in the row. If you only operate on the current row, remove %.

If you have a word like "thirtyfour", the above command will fail. In this case, the word is replaced with "thirty4 ″. To solve this problem, use "\ <" to specify the start of the matching word:

: % S/\ <four/4/g

Obviously, errors will still occur when processing "fourty. Use "\>" to solve this problem:

: % S/\ <four \>/4/g

If you are coding, you may just want to replace the "four" in the comment and keepCode. Since this is difficult to specify, you can add a "C" mark in the replacement command, so that Vim will prompt you before each replacement:

: % S/\ <four \>/4/GC

2. Delete unnecessary spaces.

To delete unnecessary spaces behind each line, run the following command:

: % S/\ s \ + $ //

The command specifies that the range is "%", so this will apply to the entire file ." The matching mode of the substitute command is

\ S \ + $ ". This indicates one or more (\ +) spaces (\ s) before the end of the line ($ ). The "to" part of the replacement command is empty :"//". In this way, the matching spaces are deleted.

3. Matching repetition Mode

The asterisk (*) specifies that the items before it can be repeated at any time. Therefore:

/*

Match "A", "AA", "AAA", and so on. But it also matches "" (Null String), because zero times are also included. The asterisk "*" is only applied to the item next to it. Therefore, "AB *" matches "A", "AB", "ABB", and "abbb. To repeat the entire string multiple times, the string must be an item. Add "\ (", followed by "\)" before the component. Therefore, this command:

/\ (AB \)*

Matching: "AB", "Abab", "ababab", and so on. It also matches "".

To avoid matching null strings, use \ + ". This indicates that the previous item can be matched once or multiple times.

/AB \ +

Match "AB", "ABB", "abbb", and so on. It does not match "A" that follows "B ".

To match an option, use "\ = ". For example:

/Folders \ =

Matches "folder" and "Folders ".

4. specify the number of repetitions

To match a specific number of times of repetition, use the form "\ {n, m. "N" and "m" are numbers. The item before it will be repeated "N" to "M" Times (| aggressive sive | contains "N" and "m "). For example:

/AB \ {3, 5}

Match "abbb", "abbbb", and "abbbbb ".

When "N" is omitted, It is 0 by default. When M is omitted, It is infinitely large by default. When ", M" is omitted, it indicates that the repetition is exactly "N" times. For example:

Number of pattern matches

\ {, 4} 0, 1, 2, 3, or 4

\ {3,} 3, 4, 5, and so on

\ {0, 1} 0 or 1, same as \ =

\ {0,} 0 or more, the same *

\ {1,} 1 or more, same as \ +

\ {3} 3

5. Select one or more matches.

In a search mode, the "or" operator is "\ | ". For example:

/Foo \ | bar

This command matches "foo" or "bar ". More options can be connected to the following:

/One \ | two \ | three

Match "one", "two", or "three ".

To match multiple times, the entire decision structure must be placed between "\ (" and:

/\ (FOO \ | bar \) \ +

This command matches "foo", "foobar", "foofoo", "barfoobar", and so on.

Another example:

/End \ (if \ | while \ | \)

This command matches "endif", "endwhile", and "endfor ".

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.