Summary of common replacement modes in vim.
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 "\ (" and "\)" 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 ".
copy and paste
: Use the following command to enter visual mode
: V to enter the block selection
: Shift + V to enter the line selection
: CTRL + V select the columns
: After the selection, Y is selected and then to the destination P
copying and pasting multiple files in VIM requires the cooperation of multiple commands, the concept of the current selection area and clipboard can easily lead to confusion. The following feasible steps are summarized (install vim-full first):
1. in the first file, use the visual mode V, select the text to be copied, and execute the command "+ Y" to copy the content to the clipboard.
2. in the new file, execute the command "+ P, you can copy it, + refers to the Register meaning, it seems to be the operating system clipboard, after copying, in other places, for example, you can use Ctrl + V in a text file.