Highlight keywords in VIM
syntax enable
Highlighted keywords: usually followed bysyntax on
Enable syntax highlighting
syntax clear
Clear keyword highlighting
When securecrt is not in xterminal mode, try it and it does not work. If it is in xterminal mode, it will work.
Search in VIM
/
Followed by the string to be searched: search from the current cursor position up
?
Followed by the string to be searched: searches down from the current cursor position
n
Find Next
N
Find the previous
Clear and redraw Screen
Vim shortcut key Customization
- For keyboard symbols, you can use: Help key-notation to view help
Custom editing Environment
The SET command is used to customize the editing environment.
set showmode
When VI is in input mode, a prompt is displayed.
set sm
Instant display of paired) or}
set ts=n
Set the tab to n (the default value is 8)
set ai
The indention position of the previous line in the next line begins.
set number
Display row numbers in the editor
set nonu
Undisplay the row number in the editor
set hlsearch
Highlight the searched string
set incsearch
The matching point is displayed immediately after the input string is entered.
set ruler
Ruler, used to display the row number and column number at the cursor position, separated by commas. Each window has its own ruler. If the window has status lines, the ruler is displayed there. Otherwise, it is displayed on the last line of the screen.
set cindent
: Cindent controls the number of indentations through the value of the shiftwidth option, instead of the value of tabstops. The default value of shiftwidth is 8.
set shiftwidth
: Change the shiftwidth value.
set helplang=cn
: If the Vim Help document cannot display Chinese characters, add this sentence.
set encoding=utf-8
:
colo murphy
: Sometimes when you use Vim to open a file, the color displayed is not suitable for reading. You can use this command to change the color to the black background.
I used the above method in gvim to set it, but it seems that it does not work. I don't know if it is correct or why
Move cursor
h
Move cursor left
L (or space key)
Move cursor right
K (or [Ctrl + P])
Move cursor up
J (or [Ctrl + N])
Move cursor down
[Ctrl+f]
Flip forward
[Ctrl+b]
Flip back
[Ctrl+d]
Half page forward
[Ctrl+u]
Flip back half page
1G
Jump to the beginning of the file
40G
Jump to row 40th
G
Jump to the end of the file
[Ctrl+g]
Display the current row number and percentage of Files
Ctrl +E
Scroll down
Ctrl +Y
Scroll up
Move the cursor within a row
Commands B, E, and W serve the same purpose as commands B, E, and W, but the latter ignores punctuation marks.
b
Move backward to the beginning of a word
e
Move forward to the end of a word
w
Move forward to the beginning of a word
0
Or | move to the beginning of the line
30|
Move to column 30th
^
First character of the first word to be moved to the row
$
Move to end of line
: num
Jump to the specified row number
Insert text
i
Insert characters on the left of the cursor
20i-[Esc]
Insert 20 characters
I
Insert characters at the beginning of a row
a
Add text to the right of the cursor
A
Add text at the end of a row
o
Insert an empty row under the current row
O
Insert an empty row to the current row.
Select text
ve
: Select the entire word
yw
: Copy the word where the cursor is located
5yl
: Copy 5 Characters of input
Delete and move text
x
Delete the character of the cursor
6x
Delete the characters at the cursor position and the five characters on the right
X
Delete the previous character
dd
Delete current row
4dd
Delete four rows
dw
Delete a word
d0
Delete to the beginning of a row
d$
Delete to end of line
d
Delete a region (only for VIM)
p
Paste the cut text below (whole line) or right side (partial line)
P
Paste the cut text to the top (whole line) or left (partial line)
add
Paste the current row into buffer
ap
Paste the content in buffer A to the current position
J
Merge the current row into the next row
xp
Change the location of two characters (in fact, it is to delete one character and then paste another character)
Some shortcut keys that are particularly useful to programmers
[[
= Move to the beginning of the previous line{
It is equivalent?^{
: Actually, it is to find the previous function.
]]
= Move to the beginning of the next row{
It is equivalent/^{
: Actually, it is to find the previous function.
[]
= Move to the beginning of the previous line}
It is equivalent?^}
: It is not very convenient to use
][
= Move to the beginning of the next row}
It is equivalent?^}
: It is not very convenient to use
f
= To the previous empty line
g
= To the next blank line
gd
= To the definition of the current local variable (the current meaning is the word on which the cursor stays).: after use, all words matching the word where the current cursor is located are highlighted.
gD
: Jump to the variable definition
*
= To the next word that is the same as the current word
#
= To the previous word that is the same as the current word
‘‘
= Line to the last cursor stop
%
: Allows the cursor to move to the matching brackets: This is very useful.
>
Increase indentation,x>
The indentation of the following X rows is increased.
<
Reduce indentation,x<
Reduce the indentation of the following X rows
syntax enable
+syntax on
Enable syntax highlighting
Modify text
rch
Replace the character at the cursor position with CH
R
Replace text on the right of the cursor
s
Replace the character of the cursor with any character
S
Replace the entire line of content
cw
Modify a word
c
Modify text in a region (Vim)
~
Change the case sensitivity of letters in the scanned or area.
!tr‘[a-z]‘‘[A-Z]‘
Convert letters in the region to uppercase (Vim)
!tr‘[A-Z]‘‘[a-z]‘
Convert the letters in the region to lowercase (Vim)
Vim tab Problems
Vim has added the multi-tag switching function since vim7, which is equivalent to multiple windows. Although the previous version also has multiple file editing functions, it is not as convenient as it is.
:tabnew
[++ Opt option] [+ cmd] File: Create a new tab for the specified file
:tabe
Open the specified file in a new tab
:tabc
Close current tab
:tabo
Close all other tabs
:tabs
View All open tabs
:tabp
Previous
:tabn
Next
In standard mode:
gt
,gT
You can switch between tabs directly.
:help table
: For detailed usage, see the help documentation
Vim compares two files
vim -d file1 file2
Directly open two files for comparison
vim file1
Open the first file, and then usevert diffsplit file2
Open the second file for comparison;vert
Vertical Split Window. The default value is horizontal split window.
- Directly compare the differences between the two files: Use
diffthis
Input in two windows respectively;
[c
,]c
Locate differences: Jump to the first and last differences;
ctrl-w w
: Window jump: jump to the next window
ctrl-w h/l
: Jump to the left (right) window
ctrl-w j/k
Jump to the upper (lower) window
dp
Merge documents: merge the current document content of the difference to another document (diff put)
do
Copy the content of another document from the difference point to the current document.
zo
Temporarily expand the same collapsed content
zc
Fold again
Copy text
yy
Copy current row
6yy
Copy six rows
yw
Copy a word
y
Copy region (Vim)
p
Paste the copied text to the right or below
P
Paste the copied text to the left or above
"ayy
Copy the current row to buffer.
"ap
Restore content in buffer
Start Editor
vi +100 foo
Open File Foo and move the cursor to line 3
vi +/pat foo
Open File Foo and place the cursor in the row that Pat appears for the first time.
vi +foo
Open File Foo and place the cursor at the end of the file
vi foo1 foo2
... Open the file foo1, use N to switch to foo2, etc.
vi -r foo
Restore contents in the swap file to the buffer zone
vi -R foo
Open the foo file in read-only mode
Save and exit
:w
Save the file and continue editing
:w bar
Same as "Save as..." in Microsoft Windows ......"
:w! bar
Same as above, but overwrite the existing file bar
:n1,n2w foo
Save the line from N1 to N2 to the file foo
:n1,n2w>>foo
Add the line between N1 and N2 to the end of the foo File
:.w foo
Save the current row to the file foo
:$w foo
Save the last row to the file foo
:x
Save and exit the editor
:wq
Same as above
:q
Exit the editor if the file content does not change.
:q!
Discard modification and exit Editor
Directory browsing in VIM
:E
: Open the folder in the default Vim path.
–
: Go to the parent directory
D
: Delete an object (uppercase)
R
: Change the file name (uppercase)
s
: Sort files in lower case)
x
: Execution File
Multi-file editing
If the current row is not saved (and autowrite is not set), The: E,: N, and: rew commands cannot be used. Command! Makes the security feature invalid.
:e foo
Stop editing the current file and start editing the foo file.
:e! foo
Same as above, but the modification to the current file is canceled
:e!
Load the latest saved content of the current file
[Ctrl+^]
Return to the recently edited File
:n
Edit the next file (when you use VI to edit multiple files at the same time)
:set autowrite(aw)
Automatically save the current file before switching to the next file with: N
:rew
The first file returned to the file list (when multiple files are edited simultaneously using VI)
:r foo
Insert the content of the foo file below the current row
Multi-Window (Vim)
:sq
Split the current window into two
:new
Open a new blank window
:[Ctrl+w][Ctrl+w]
Switch Ctrl + W + r between windows to move the window to the right Ctrl + W + L to move the window to the left
:on
Change the current window to a unique window.
:q
Exit current window
:qa
Exit all windows
:xa
Save and exit all windows
[Ctrl]+
Increase window size
[Ctrl]-
Reduce window size
Search and duplicate
The search mode pat can be a string or a regular expression. VI uses a set of dedicated character sequences to perform searches within a row.
/pat
Forward search pat
?pat
Search for Pat
/printf
Search for printf
?[0-0]$
Search for numbers at the end of a row
/^#
Search for the beginning of a row #
n
Repeat the search in the last search direction
N
Repeat the search in the opposite direction of the previous search
:set wrapscan(ws)
Continue searching until the end of the file is reached
:set ignorecase(ic)
Case Insensitive during search
:set magic
Meaning of retaining regular expression characters
fc
Search for the forward character C in the current row
Fc
Search for character C in the current row
;
Repeat the previous search in the current row
,
Repeat the previous reverse search in the current row
*
: Look down at the word where the cursor is located, similar/
#
: Look up the word where the cursor is located, similar?
Replace
The source mode S1 can also be a regular expression. When the Champions League address is used, "." indicates the current row, "$" indicates the last row, and "1, $" indicates the entire file.
:.s/s1/s2
Replace S1 that appears for the first time in the current row with S2.
:1,$s/s1/s2
Replace S1 in the entire file with S2.
:1,$s/echo/printf/g
Replace all the echo with printf.
:1,$s/s1/s2/gc
Replace all S1 WITH S2 in interactive mode
:1,$s/#//g
Delete all "#" in the file
:3,10s/^/#/g
Insert "#" at the beginning of Line 3 to 10
:$s/$/;/
Insert ";" at the end of the last line
:1,$s/s1/s2/g
Replace all S1 WITH S2
:1,$s/HEAD/<&>/g
Replace all headers
Logos and bookmarks
ma
Set a tag
‘a
Move to tag
"
Switch between the current flag and the previous flag position
Redo and restore
.
Repeat the previous command
u
Cancel the last edit command (cancel all previous commands in VIM)
[Ctrl+r]
Redo the last canceled Operation (Vim only)
U
Cancel all operations on the current row
"4p
Restore 4th most recent delete operations from the buffer zone
Abbreviation
:ab name stg
Abbreviated name as STG
:unab stg
Delete STG abbreviations
:ab
List all abbreviations
Ing key
:map key commands
Map key to commands
:map! key commands
Map keys to commands in input mode
:unmap key
Cancel key ing in command mode
:unmap! key
Cancel key ing in input mode
:map
Display All mappings in command mode
:map!
Show all mappings in input mode
Interface commands with Unix
:!cmd
Run the Unix cmd command
:!%
Execute the current file as a shell or Perl script
:r !cmd
Insert the result of the CMD command below the current line
:r !head -n 3 foo
Insert the first three lines of the foo file to the current row.
sh
Temporarily exit the Unix shell Environment
[Ctrl+z]
Pause editing (use the FG command to return to the VI editor)
:!cc%
Compile the C program being edited
:!javac %
Compile the Java program being edited
Others
- To change the indentation of some text in the file: In command mode, press V to select multiple lines, and then press SHIFT +> (<) to change the indentation.
- You can also press SHIFT +> (<) to change the indent (3 times)
Plugin installation
- Common plug-ins
*.vim
Copy ~ /. Vim/plugin.
*.vmb
Plug-in: Use Vim to open the plug-in, and then use the command:so %
Install
*.vba
Plug-in: first use Vim to open the plug-in, and then useUseVimBall ~/.vim
Install
Vim folding
- Set the foldmethod: You can use the 'foldmethod' option to set the foldmethod: Set OFDM =*
- There are 6 Ways to select the fold:
- Manual manual definition folding
- More indent indicates more fold
- Expr uses expressions to define collapse
- Syntax uses syntax highlighting to define collapse
- Diff folds Unchanged text
- Marker folds the mark in the text
Note that each folding method is incompatible. If expr is not used and marker is used, the indent and marker modes are used in turn.
When using this function, use the: Set OFDM = marker command to set it to the marker foldmethod ).
To make the folding take effect every time Vim is opened, add the settings in the. vimrc file, such as adding: Set OFDM = syntax, just like adding other initialization settings.
- Fold command
After the folding method is selected, we can implement the folding we need for some code. Because I use more indent and marker, their usage is used as an example:
If the indent method is used, VIM automatically folds the middle part of the braces, and we can directly use these ready-made folding results.
In a foldable place (in the middle of braces ):
ZC folding
ZC folds all nested folding points in the range
Zo unfold and fold
Zo expands all nested folding points in the range
[Z to the start of the current fold.
] Z to the end of the currently opened fold.
ZJ moves down. To the start of the next fold. The closed folding is also included.
ZK moves up to the end of the previous fold. The closed folding is also included.