type
status
date
slug
summary
tags
category
icon
password
Prelude:
Yes, You can always type :h to see help, for example, :h windows
Vim in not so short
Preliminaries
- When vim is not available, you can temporarily use
cat
ornano
as alternatives
- When using a new IDE, you should first learn or configure:
- save
- copy / paste
- duplicate line
- delete word / delete line
- line movement
- navigate forward / back
- undo / redo
- page navigation (page down, page up, center to middle, etc.)
- indent / unindent
- multi-cursor
- search / content search
- input commands (
Ctrl + P
in VSCode, doubleShift
in IntelliJ,:
in vim…) - completion, suggestion, parameter hint, hover hint
- go to / peek definition, type, declaration, reference, implementation
- rename symbol / refactor (if available)
- AI controlling: toggle chat box, trigger inline chat…
- tabs / windows management
- Use the
:help
command to query anything you don’t understand
:set option?
can view current settings
Windows and Tabs
Buffer: Can be understood as a temporary file. Even if this file is edited, it doesn’t mean it’s written to the actual file. We can observe buffers through windows and manage them through tabs.
- Use commands starting with
b
in Ex (command line) mode for buffer operations: bd
buffer delete;bwipeout
clears more thoroughlybn
orbnext
switch to next bufferbp
orbprevious
switch to previous bufferctrl + ^
switch to the last buffer
Ctrl + W
window shortcuts and :sp
split commands are the basics of window management. :sp
can use parameters to specify files to split (open a new window for this file)Ctrl + W
+w
cycle windows,Ctrl + W
+hjkl
switch focus left/down/up/right (move cursor)
Ctrl + W
+=
(make window panes equal size)
Ctrl + W
++
Ctrl + W
+Ctrl + W
+<
Ctrl + W
+>
(fine-tune window size)
:vsp
performs vertical split, dividing into two sides
:resize 20
sets height to 20 lines;:vertical resize 80
sets width to 80 columns
:tabs
is the basic tab management command.:tabnew
opens a new tab,:tabe
(edit) opens a tab for editing. Can be followed by filename.
:tabn
:tabp
switch tabs, or usegt
gT
to switch
- Display current buffer in new tab:
:tab split
:tabmove
move tab, for example:tabmove 2
- Batch execution:
:tabdo {cmd}
execute on all tabs, e.g.,:tabdo set nocursorline
:windo {cmd}
execute on each window in current tab, e.g.,:windo diffthis
:only
and :close
are universal management methods- Type
:close
:only
directly to manage windows
- Type
:tabclose
:tabonly
to manage tabs
Key Remapping
Here’s the key remapping configuration in VSCode vim extension:
Common key aliases:
<Backspace>
<C-c>
-> Ctrl + C
<A-c>
-> Alt + C
<Shift>
<CapsLock>
<Tab>
<Enter>
<Esc>
<Cmd->
Mac specific
<Up>
<Down>
<Left>
<Right>
vim Configuration File
Use the
.vimrc
file in the home
directory:Unbound Keys
In VSCode and other IDEs, you can return Vim-controlled keys to the IDE, since they have no use in GUI interfaces.
Ctrl + D
scroll down
Ctrl + Z
suspend Vim
Ctrl + X
decrease number at cursor by 1, in insert mode it’s completion, decrease by 1 is quite useful
Ctrl + C
can be considered
Ctrl + F
scroll forward, in insert mode used to view command history
Ctrl + W
window command, delete one word before cursor
Ctrl + P
completion
Ctrl + S
pause output
Ctrl + Q
same asCtrl + V
, onlyq
alone is for recording macros, VSCode defaults to Add cursor below, multi-cursor editing tool
Ctrl + Y / E
scroll window up/down by 1 line
Ctrl + T
depends on situation
Quick Reference
(jut for memory, may not be accurate)
w
-> word
b
-> back
e
-> end
H
-> page head;L
-> low;M
-> middle
zz
move current line to the middle position
Ctrl + U
&Ctrl + D
half screen move up & downCtrl + F
&Ctrl + B
full screen move forward & back
g
-> go (sometimes obviously more than that)gn
go to next (matched), commonly usedgi
go to (last) insertgd
go to definitiongf
go to file
d
-> delete (actually puts deleted content into register); sometimes means downdd
delete one line
x
->dl
;X
->dh
s
-> (x
+i
)c
-> change (d
+i
)
y
-> yank
p
-> do paste | as paragraph
{
}
-> paragraph
f
-> find occurrence (char);
-> next occurrence,
-> last occurrencet
-> till next occurrenceF
-> backward to occurrence
/
-> search item with patternn
-> next item?
-> back search
u
-> undo;Ctrl + R
redo
123…
-> as line number / do multiple times
i
-> do insert | as inner (inside);o
-> open a line below;a
-> append | around
s
-> as sentence | do substitute
q
-> record macro
v
-> visual
s
-> surrounding.cs{old}{new}
. plugin commandv
S{desire pair}
-> surrounding by.
J
join
r
replace
Special Characters
@
-> execute register content
.
> as current line | do repeat last change, very commonly used, for repeating edit operations
%
-> as all lines(all contents); in normal mode represents matching symbols like{[(
0
-> beginning of line$
-> end of line^
first meaningful character_
first meaningful character (can be operated byg
etc.)g_
last meaningful character
;
repeat lastf, F, t, T
command. For example, if you usedf
to find a character, pressing;
will continue to find the next matching character.,
reverse repeat
'
for mark navigation. For example, pressing'a
jumps to the beginning of line of mark ‘a’.
`
(backtick) for mark navigation. For example, pressing`a
jumps to the exact position of mark ‘a’.
~
change char case.
&
repeat last substitution command
Modes
- Normal mode: Enter normal mode when first starting, press
i
(insert) to enter insert mode, press:
to enter command input mode
- Insert mode: Edit file, press Esc to exit.
- Command input mode: Input commands, press Esc to exit.
- Press
v
to enter visual mode, generally used for selecting paragraphs
Command Structure
- Normal mode commands are almost based on the
verb
+adj(count)
+obj(motion, text-object)
pattern, e.g.:d3j
,ca(
- Omitting
verb
means cursor movement, which can actually be understood as a type ofverb
. - Example:
3j
, move down 3 lines - Example:
0
,$
, these are special characters - Example:
v3w
,v
only switches mode, doesn’t count asverb
, but subsequent behavior withoutverb
is treated as cursor movement - Special case: Omitting
adj.
means based on current position, or only operating on one object. - Example:
cw
,f{
- Special case: Omitting both
adj.
andobj.
generally means some single character/line shortcuts - Example:
dd
,yy
,gg
- Example:
s
,x
,~
,u
- Command line mode has almost no pattern. If anything, they can almost all be categorized as command-like modes.
- verb + obj pattern:
:set number
,:r file
(read file) - adj + verb pattern:
:3,5 d
- adj + verb + obj:
:3,5 d0
- obj pattern:
:map
- verb pattern:
:w
,:q
- Special pattern (can be viewed as mode switching):
:!command
(execute external command) - Command-like pattern:
:s/foo/bar/gc
Repeat Operations
.
for modifications.
;
for f, etc.
r
means replace
f
sth
->r
->;
->.
->;
->.
-> … : guess what will happen?
gn
for/
etc. Need to operate on current search content once first, then use.
to repeat.
Tips
verb
is almost never absent, omittingverb
generally means movement (v
provides selection function, but still needs to move to some position to trigger)
- At the same time,
obj
is also almost never absent, omittingobj
generally means current position
When inputting commands, if you’re unclear what else to input, consider these questions:
1. Does the command have an object? E.g.,
w, p, s
.
(Specifically, line operations generally omit objects).
Selection in v
mode actually provides objects. Similarly, movement actions actually also provide objects.
So, this part should be considered first, but input last.
2. Does the command have modifiers?
If there’s a clear area to operate on, you must pay attention to modifier input.
Because if not provided, it will immediately operate based on current position, very easy to cause misoperations.
3. What does the command need to do?
Please treat this as the last part to think about, to control vim behavior more precisely.
But when inputting commands, generally need to input first.
So, input this first.
Seqs: 3 → 2 → 1
Commonly used Commands
:w
save
:q
quit
:q!
force quit without saving
:w filename
save file as filename
:w! filename
force save to filename
:set nu
:set number
display line numbers, commonly used
/word
search downward (note, no:
, when pressing/
, command line becomes/
mode)- Press
n, N
to search next/previous ?word
search upward
u
and:u
undo
Ctrl + R
redo
x
=dl
,X
=dh
s
=ch
Compound Commands
:5 d
delete line 5, commonly used
:3,5 d
delete lines 3 to 5
:$ d
delete last line of document
:% d
delete all content in document
:n move m
move line n to after line m, commonly used
:n,n+3 t m
copy lines n to n+3 to after line m, commonly used- Summary:
:n,m
generally means from line n to line m
:%s/hello/nihao/g
replace all hello with nihao, similar tosed
operation:s/hello/nihao/g
only replace in current line
:5,8s/hello/nihao/g
replace hello with nihao in lines 5-8
Common Shortcuts
Movement
Ctrl + O
previous position,Ctrl + I
next position
- If you have
Home
End
PageUp
andPageDown
keys, you can use them directly, addingCtrl
is more convenient PgUp
PgDown
behavior varies, specifically use withCtrl
andShift
Ctrl + U
andCtrl + D
half screen scroll;Ctrl + F
andCtrl + B
full screen scroll
Ctrl + Up
to beginning of line, vice versa
Normal Mode
Cursor Movement
Character-based Movement
h
l
move left/right,k
j
move up/down, arrow keys also work
- Use
f
to find next character, uppercase for reverse search; usingt
stops before that character - Use
;
and,
to repeat forward/backward search
^
move to first non-whitespace position
g_
find first non-whitespace position from end of line
- Use
$
to move to end of line
- Use
%
to match brackets like( { [
Word-based Movement
w
move to next word, commonly used, remember as word
b
move to beginning of current/previous word, commonly used, remember as back
e
move to end of current/next word, commonly used, remember as end- Uppercase
W, B, E, gE
for including characters after words ge
move to end of previous word3w
can move 3 times
- Use
/
and?
to search forpattern
, usen
andN
to jump to next/previous
Line-based Movement
g
is the most basic line movement command
0
move cursor to beginning of line,$
move cursor to end of line
Home
key to beginning of line,End
key to end of line
G
(Shift+G)/gg
move cursor to end/beginning of file
- Actually
G
is go, so you can input500G
or500gg
to locate to line500
, commonly used - Or directly use
:line_number
to locate gi
return to last insert location, commonly used
Block or Screen-based Movement
{
}
move by paragraph
H
move to top line of screen;L
move to bottom line of screen;M
move to middle of screen
Mark-based Movement
ma
set mark a
'a
jump to beginning of line of mark a
`a
jump to exact position of mark a
''
jump to beginning of line of previous position, commonly used
``
jump to exact position of previous position, commonly used
Operation Commands
Follow
{operator}{count}{motion}
pattern, e.g.: d2w
c
change,d
delete,y
yank (copy),p
paste- change means delete corresponding content and directly enter insert mode
gu
gU
or directly press~
to change char’s case (commonly used)
=
format, for formatting, need to set code standards in advance, e.g.:
Common formatting commands:
-
==
: Format current line.
- =motion
: Format range specified by motion
.
- =ap
: Format current paragraph (ap
means “a paragraph”).
- =%
: Format content within brackets matching current cursor position.
- gg=G
: Format entire file.- Use
>>
for indentation, use>
when target is provided
Text Objects (delete content of specific patterns)
Follow
{operator}{a/i}{text object}
pattern, e.g.: di'
(delete inside ’)- Generally used with
c
,d
- a -> around, i -> inside
- text-object identifiers:
w
words
sentencep
paragraph'"[{<
` corresponding to respective special blockst
tag, commonly used in html language, between><
Copy and Delete
yy
copy current line, commonly used
dd
cut current line, commonly used
3dd
delete 3 lines, commonly used
p
/P
paste content after/before cursor, commonly used
x
delete character at cursor,X
delete previous character
r
modify character at cursor
R
enter replace mode
Undo
u
undo, commonly used
Ctrl + R
redo, commonly used
Enter Insert Mode
i
enter insert mode at current position
a
enter insert mode at next character
A
enter insert mode at end of line
o
enter insert mode on next line, often used withG
andEnd
, commonly used
s
delete current character and enter insert mode
S
delete current line and enter insert mode
cw
delete next word and enter insert mode
cb
delete previous word and enter insert mode
Insert Mode
Generally use shortcuts for operations
Cursor Movement
Ctrl + A
move cursor to beginning of line, uppercase moves to beginning of document
Ctrl + E
move cursor to end of line
Text Operations
Ctrl + W
: Delete one word before cursor.
Ctrl + U
: Delete all content from current cursor position to beginning of line.
Ctrl + T
increase indentation to the right
Ctrl + D
decrease indentation to the left
Ctrl + R
: Insert register content. After pressing, need to input register identifier, e.g.,Ctrl + R, 0
inserts content of register 0.
Completion Features
Ctrl + N
: Auto-complete next matching word. commonly used
Ctrl + P
: Auto-complete previous matching word.
Ctrl + X
,Ctrl + L
: Full line completion, shows dropdown menu
Ctrl + X
,Ctrl + N/P
: Word completion based on current file.
Ctrl + X
,Ctrl + F
: Filename completion.
Ctrl + X
,Ctrl + K
: Dictionary lookup completion.
Others
Ctrl + C
: Exit input mode. Replace Esc, commonly used
Ctrl + O
: Temporarily switch to normal mode to execute one command, then return to input mode. commonly used
- What if you press wrong key? In normal mode use
Ctrl + O
to return to previous position, meaning in input mode pressCtrl + O
x2
Visual Mode
Follow
{trigger v}{motion}{operator}
pattern, e.g.: v2wd
(select two words then delete)- Press
v
for character selection,V
for line selection,Ctrl + V
for block selection
- Generally only used for copy-paste, operation speed is slow
:
Command Mode
Batch Processing (delete, move, copy, paste)
:2,5d a
: Delete lines 2 to 5 and save in register a
0
line 0; `# Vim in a Nutshell
:.,+3d
delete current lint & plus 3 lines
- In
visual
mode,:'<,'>d
means “delete selected content”
@:
repeat last:
command- After using
q
for macro recording we can use@@
repeat it - After
@:
,@@
is still available
:'<,'>move .
:m 10
move (current line) to 10 line after
:/
looking for last found pattern
Quick Reference
Find and Replace
:%s/old/new/g
: In entire file range, replace all strings matchingold
withnew
. Addc
to ask before each replacement.
:s/old/new/g
: Replace allold
withnew
in current line.
:g/pattern/d
: Delete all lines matchingpattern
.
:'<,'>s/old/new/g
: In visual mode selected range, replace allold
withnew
.
:'<,'>normal command
: Executenormal
mode command on each line selected in visual mode.
Line Operations
:d
: Delete current line (similar todd
).
:t .+1
: Copy current line and paste to next line.
:m .+1
: Move current line down one line.
:m .-2
: Move current line up two lines.
:-3,. m .+1
: Move 3 lines above current line to below current line
:-3,.y | :.+1put
: Copy above three lines, paste to next line.put
creates new line,p
pastes at cursor position
sed-like Command Usage
- Replace all matching patterns at once:
:%s/pattern/replacement/gc
for entire file range (%
), substitute (s
)pattern
withreplacement
gc
areflags
, commonflags
include:g
: Global replacement (apply command to all lines)c
: Ask before each command execution.i
: Ignore casen
: Maximum number of replacementsp
: Print resultsv
: Invert results!
: Pass to external tool ->%!sort
will sort entire text
- Explicitly use normal commands in
Ex
mode. Usenorm
- Very useful with v mode
- Example:
:'<,'>norm A!
, will for content selected in v mode (‘<,’>), uniformly add a “!” at the end of lines (A)
%
itself is shorthand for **`1,# Vim in a NutshellTips
- Accidentally pressed
Ctrl + S
? UseCtrl + Q
to release
- It’s best not to use the right numeric keypad for editing, there might be key conflicts
Copy-Paste Issues
Vim editor has its own clipboard system called registers. Vim uses registers to store text, which are different from X11 clipboards. If Vim is compiled with X11 clipboard support (through +clipboard option), then you can use special registers
"*
and "+
to access X11’s PRIMARY and CLIPBOARD.Paste Mode
To prevent auto-formatting from corrupting pasted content, Vim provides paste mode. Before pasting, execute
:set paste
command to enter paste mode, which will temporarily disable auto-indent, auto-wrap and other functions. After pasting, execute :set nopaste
to return to normal mode.You can also install some programs to access X11 clipboard, such as xclip or xsel.
Right mouse button enters visual mode?
:set mouse-=a
Register Overview
Vim uses a set of registers as its “clipboard”. These registers can be used to save text and paste later. Each register has an identifier, which can be a single letter or special character. Here are some basics about Vim registers:
Note, to use registers, first press
"
, then switch to the desired register, then perform operationsTo view register status, type
:reg
in command line modeBasic Registers
""
- Default register, if you don’t know where copied content went, look here
"0
- Number 0 register. Stores content of your last yank (copy).
"-
- Small delete register. Stores content of your last delete or change that’s less than one line.
"1
to"9
- Number registers. Store your recent delete or change history ("1
is most recent,"9
is oldest).
"a
to"z
or"A
to"Z
- Named registers. You can use lowercase letters to store text, uppercase letters to append text to corresponding lowercase register.
Special Registers
"*
and"+
- Clipboard registers. In Vim with clipboard support (usually GUI version Vim or Vim with+clipboard
feature), these two registers communicate with system clipboard:"*
register corresponds to X11’sPRIMARY
clipboard (usually the select-to-copy one)."+
register corresponds toCLIPBOARD
clipboard (usually used withCtrl+C
/Ctrl+V
key combinations). Most commonly used.
Using Registers
- Copy to register: You can use
"ay
to copy to registera
, wherea
is the register name,y
is the yank command. - Almost the same in visual mode
- Paste from register: To paste from register, you can use
"ap
to paste content of registera
, wherea
is the register name,p
is the paste command.
- In insert mode: Use
Ctrl + R
to call up"
symbol
Using Registers in Insert Mode
Press
Ctrl + R + register name
For example
Ctrl + R + "
uses default register, Ctrl + R + +
uses system register.Using Registers in Terminal
When you use Vim in terminal instead of GUI version, accessing system clipboard becomes more complex, because Vim needs to include clipboard support at compile time. You can run
vim --version
to see if your Vim supports clipboard (look for +clipboard
or +xterm_clipboard
).If your Vim supports clipboard, you can interact with system clipboard in the following ways:
- Use
"*y
to copy to system’s PRIMARY clipboard.
- Use
"+y
to copy to system’s CLIPBOARD clipboard.
- Use
"*p
or"+p
to paste from corresponding system clipboard to Vim.
If your Vim doesn’t have clipboard support, you won’t be able to directly access system clipboard from Vim. In this case, you need to use external programs (like
xclip
or xsel
) to help you copy Vim register content to system clipboard, or paste from system clipboard to Vim.External Commands
Using :!
to Execute External Commands
You can use
:!
followed by command to execute any external command. For example, if you want to see current directory contents, you can type in Vim’s command mode:Using :r
to Read External Command Output
If you want to insert output of
sed
, awk
or other commands into current Vim editor text, you can use :r !
command. For example, if you want to insert ls
command output at current position, you can type:Using :%!
to Process Entire File Content Through External Command
If you want to apply
sed
or awk
command to entire file content and replace current file content, you can use :%!
followed by corresponding command. For example, use sed
command to replace all “foo” with “bar” in document:Partial Usage
You can also specify a range to use external command only on certain parts of the file. For example, only process lines 5 to 10 through
awk
command:Macro Recording
- Use
q
for macro recording in normal mode - Give register a name, say
a
, the full name of this register is@a
- Press
q
again to end recording after recording is complete - Type
@a
to execute macro,@@
to execute last macro,9@a
to execute macro 9 times
- +1 operation on numbers.
<C-a>
, can be used with macro recording to write ordered lists: 1. sth
, thenyyp
, perform<C-a>
on 1, repeat
Other Features
- Spell check.
:set spell! spelllang=en_us
:set rnu
set relative line numbers. Convenient for jumping
- Author:Parker Chen
- URL:www.parkerchenca.com/article/244f0ccf-d7f8-8047-bbe6-e4a2c0088d27
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts