1. Search
Command |
Function |
Note |
* |
Search down the word where the cursor is located |
n Next, N prev |
# |
Search up the word where the cursor is located |
Ditto |
[+<ctrl>+i |
Jump to the declaration of the variable where the cursor is located |
Use tag |
[+<ctrl>+d |
Jump to the definition of the macro where the cursor is located |
Use tag |
YCM Positioning commands |
Jump to the declaration or definition of the variable where the cursor is located |
Youcompleteme plug-in functionality, you can use the following 3 commands, However, the shortcut key is usually used: : Ycmcompleter gotodeclaration : Ycmcompleter gotodefinition : Ycmcompleter gotodefinitionelsedeclaration |
/<pattern> |
Search down mode <pattern> |
<pattern> is not just a simple string, it can also be a complex regular expression |
?<pattern> |
Search up Mode <pattern> |
Ditto |
: Vimgrep <pattern> <files> |
Search Patterns in Files <pattern> |
For example, search for ABC in all. cpp files in the current directory: : Vimgrep ABC *.cpp If you want to search subdirectories, this is : Vimgrep ABC **/*.cpp The results come out in the Quickfix window, with the command: CW Exhale |
2. Replace
Command |
Function |
Note |
<scope>s/<pattern>/<str>[/<option>] |
All strings that match the pattern in the range specified by scope Replace with Str,option as an optional parameter |
With option, the front is added/ Example :%S/ABC/XYZ/GC Replace all ABC in the current file with XYZ and confirm before replacing Option g: replace each line with all ABC, if not add only the first one Option C: Ask the user for confirmation before replacing, the user can select Y to confirm, N denies and jumps to the next match, Q Stop, a don't ask for all yes |
: Args <files> : Argdo <scope>s/<pattern>/<str>[/<option>] |
Match pattern within scope specified in file <files> Replace all strings with str,option as optional parameters |
The parameters are described above. Example : args *.txt *.cpp : Argdo%S/HATE/LOVE/GC | Update Replace the hate in all TXT and CPP files in the current directory with Love, Before each replacement, the update command occurs when the file is changed Save. Note: The replacement operation in the file is not recoverable, please do it carefully and make a backup |
VIM many operations have range parameters, and the Replace command is no exception. The% in the above command represents all rows, plus m, n means from line m to nth row.
3. Tip 3.1 Copy a piece of text from the body to the command line
First copy, to the command line, <ctrl>+r+ "(" is <shift>+ ').
Also, please remind me:
<ctrl>+p is the automatic input of the last command, <ctrl>+n is automatically entered the next command,
<ctrl>+b is jumping to the beginning of the command line, <ctrl>+e is jumping to the end of the command
3.2 Replace \ n line break
Scene:
Using VIM to edit a text file, you want to add a character, such as ";", before each line wraps.
Think of the replacement command:
:%s/\n/;\n/g
The result is always wrong.
Later found a solution:
:%s/\n/;\r/g
It was concluded that:
When searching: \ n is newline, \ r is CR (carriage return = Ctrl-m = ^m)
When replacing: \ r is newline, \ n is a null byte (0x00).
It means:
String lookup, "\ n" is a newline, "\ R" is a carriage return, which is often seen ^m (note-1).
When a string is replaced, "\ r" is a newline, and ' \ n ' is a null character (0x00).
For more details, refer to Http://vim.wikia.com/wiki/Search_and_replace
3.3 Clear All ^m some files that are edited on Windows are copied to Linux on the end of the ^m, and sometimes you want to replace them with the following commands
:%s/<ctrl>+v <ctrl>+m//g
Is the control key +v, and then the control key + M, it becomes the ^m, and then replaced by empty on it.
"Vim" Search and replace