logo
🛠️ 开发工具

Vim

Vim Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 开发工具🧭 Markdown 速查🏷️ 2 个标签
#vim#editor
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Motion Diagrams
BASH
滚动查看更多
▼/▶ Cursor   ▽/▷ Target

Left-right motions

BASH
滚动查看更多
┌───────────── |
├───────────── 0      $ ──────────────┐
│  ┌────────── ^      fe ────────┐    │
│  │  ┌─────── Fo     te ───────┐│    │
│  │  │┌────── To     30| ───┐  ││    │
│  │  ││ ┌──── ge     w ───┐ │  ││    │
│  │  ││ │ ┌── b      e ─┐ │ │  ││    │
│  │  ││ │ │  ┌h      l┐ │ │ │  ││    │
▽  ▽  ▽▽ ▽ ▽  ▽▼      ▼▽ ▽ ▽ ▽  ▽▽    ▽
   echo "A cheatsheet from quickref.me"

Up-down motions

BASH
滚动查看更多
                 - SCREEN 1 START
   ┌─┬─────────▷ #!/usr/bin/python
   │ │     ┌───▷
   │ │     │     print("Hello")
   │ │     { } ▶ print("Vim")
   │ │       │   print("!")
   │ │       └─▷
   │ │ ┌───┬───▷ print("Welcome")
G gg H M L k j ▶ print("to")
│        │   └─▷ print("cheatsheets.zip")
│        │       print("/vim")
│        │
│        └─────▷
│                - SCREEN 1 END
└──────────────▷ print("SCREEN 2")
Motions
ShortcutDescription
h | j | k | lArrow keys
<C-u> / <C-d>Half-page up/down
<C-b> / <C-f>Page up/down

{.shortcuts}

Words{.left-text}

ShortcutDescription
b / wPrevious/Next word
ge / ePrevious/Next end of word

{.shortcuts}

Line

ShortcutDescription
0 (zero) / $Start/End of line
^Start of line (non-blank)

{.shortcuts}

Character

ShortcutDescription
Fe / feMove to previous/next e
To / toMove before/after previous/next o
<code>|</code>/ <code>n|</code>Go to first/nth column

{.shortcuts}

Document

ShortcutDescription
gg / GFirst/Last line
:n | nGGo to line n
} / {Next/Previous empty line

{.shortcuts}

Window

ShortcutDescription
H / M / LTop/Middle/Bottom screen
zt / zz / zbTop/Center/Bottom this line

{.shortcuts}

Insert Mode
ShortcutDescription
i / aInsert before/after cursor
I / AInsert start/end of line
o / O (letter)Insert new line below/above
s / SDelete char/line and insert
C / ccChange to end of/current line
giInsert at last insert point
Esc | <C-[>Exit insert mode

{.shortcuts}

Saving and Exiting
ShortcutDescription
:wSave
:qClose file
:wq | :x | ZZSave and quit
:wqaSave and quit all files
:q! | ZQForce quit
:qaClose all files
:qa!Force quit all files
:w now.txtWrite to now.txt
:sav new.txtSave and edit new.txt
:w !sudo tee %Write to readonly file

{.shortcuts}

Normal Mode
ShortcutDescription
rReplace one character
REnter Replace mode
u / 3uUndo changes 1 / 3 times
UUndo changes on one line
JJoin with next line
<C-r> / 5 <C-r>Redo changes 1 / 5 times

{.shortcuts}

Cut and paste
ShortcutDescription
xDelete character (Cut)
p / PPaste after/before
xpSwap two characters
DDelete to end of line (Cut)
dwDelete word (Cut)
ddDelete line (Cut)
ddpSwap two lines
yyYank line (Copy)
"*p | "+pPaste from system clipboard
"*y | "+yPaste to system clipboard

{.shortcuts}

In visual mode

ShortcutDescription
d | xDelete selection (Cut)
sReplace selection
yYank selection (Copy)

{.shortcuts}

Repeating
ShortcutDescription
.Repeat last command
;Repeat latest f, t, F or T
,Repeat latest f, t, F or T reversed
&Repeat last :s
@:Repeat a command-line command

{.shortcuts}

Visual mode
ShortcutDescription
vEnter visual mode
VEnter visual line mode
<C-v>Enter visual block mode
ggVGSelect all text
> / <Shift text right/left

{.shortcuts}

Macros
--
qiRecord macro i
qStop recording macro
@iRun macro i
7@iRun macro i 7 times
@@Repeat last macro

{.shortcuts}

You can save macro for any letters not just i

Vim Operators

Usage
ShortcutDescription
d<yel>w</yel>
OperatorMotion

Combine operators with motions to use them

Available Operators
ShortcutDescription
dDelete
yYank (copy)
cChange (delete then insert)
pPaste
=Formats code
g~Toggle case
gUUppercase
guLowercase
>Indent right
<Indent left
!Filter through external program
Examples
CombinationDescription
d<yel>d</yel>Delete current line
d<yel>j</yel>Delete two lines
d<yel>w</yel>Delete to next word
d<yel>b</yel>Delete to beginning of word
d<yel>fa</yel>Delete until a char
d<yel>/hello</yel>Delete until hello
c<yel>c</yel>Change current line, synonym with S
y<yel>y</yel>Copy current line
><yel>j</yel>Indent 2 lines
ggd<yel>G</yel>Delete a complete document
gg=<yel>G</yel>Indent a complete document
ggy<yel>G</yel>Copy a whole document

{.show-header}

Counts
CODE
滚动查看更多
[count] <operator> <motion>
<operator> [count] <motion>

CombinationDescription
2d<yel>d</yel>Delete 2 lines
6y<yel>y</yel>Copy 6 lines
d3<yel>w</yel>Delete 3 words
d5<yel>j</yel>Delete 5 lines downwards
>4<yel>k</yel>Indent 4 lines upwards

Vim Text objects

Usage
<!-- Confusing and should be fixed -->
ShortcutDescription-
v<pur>i</pur> / <pur>a</pur><yel>p</yel>
Operator<pur>i</pur>nner / <pur>a</pur>roundText object

Operate with an operator inner or around text blocks

Text objects
ShortcutDescription
<yel>p</yel>Paragraph
<yel>w</yel>Word
<yel>W</yel>WORD <br/>(surrounded by whitespace)
<yel>s</yel>Sentence
<yel>[</yel> <yel>(</yel> <yel>{</yel> <yel><</yel>A [], (), or {} block
<yel>]</yel> <yel>)</yel> <yel>}</yel> <yel>></yel>A [], (), or {} block
<yel>'</yel> <yel>"</yel> <yel>`</yel>A quoted string
<yel>b</yel>A block [(
<yel>B</yel>A block in [{
<yel>t</yel>A HTML tag block

See :help text-objects

Delete
ShortcutDescription
d<pur>i</pur><yel>w</yel>Delete inner word
d<pur>i</pur><yel>s</yel>Delete inner sentence
d<pur>i</pur><yel>"</yel>Delete in quotes
d<pur>a</pur><yel>"</yel>Delete in quotes (including quotes)
d<pur>i</pur><yel>p</yel>Delete a paragraph
Selections
ShortcutDescription
v<pur>i</pur><yel>"</yel>Select inner quotes "...{.underline}"
v<pur>a</pur><yel>"</yel>Select quotes "..."{.underline}
v<pur>i</pur><yel>[</yel>Select inner brackets [...{.underline}]
v<pur>a</pur><yel>[</yel>Select brackets [...]{.underline}
v<pur>i</pur><yel>w</yel>Select inner word
v<pur>i</pur><yel>p</yel>Select inner paragraph
v<pur>i</pur><yel>p</yel><pur>i</pur><yel>p</yel>Select more paragraph
Misc
ShortcutDescription
c<pur>i</pur><yel>w</yel>Change inner word
c<pur>i</pur><yel>"</yel>Change inner quotes
c<pur>i</pur><yel>t</yel>Change inner tags (HTML)
c<pur>i</pur><yel>p</yel>Change inner paragraph
y<pur>i</pur><yel>p</yel>Yank inner paragraph
y<pur>a</pur><yel>p</yel>Yank paragraph (including newline)

Vim Multiple files

Buffers
--
:e fileEdit a file in a new buffer
:bnGo to the next buffer
:bpGo to the previous buffer
:bdRemove file from buffer list
:b 5Open buffer #5
:b fileGo to a buffer by file
:lsList all open buffers
:sp fileOpen and split window
:vs fileOpen and vertically split window
:hidHide this buffer
:wnWrite file and move to next
:tab baEdit all buffers as tabs
Windows
--
<C-w> sSplit window
<C-w> vSplit window vertically
<C-w> wSwitch windows
<C-w> qQuit a window
<C-w> TBreak out into a new tab
<C-w> xSwap current with next
<C-w> - / +Decrease/Increase height
<C-w> < / >Decrease/Increase width
<C-w> <code>|</code>Max out the width
<C-w> _Max out the height
<C-w> =Equally high and wide
<C-w> h / lGo to the left/right window
<C-w> j / kGo to the up/down window

{.shortcuts}

Tabs
ShortcutDescription
:tabe [file]<yel>E</yel>dit file in a new tab
:tabf [file]Open if exists in new tab
:tabc<yel>C</yel>lose current tab
:taboClose <yel>o</yel>ther tabs
:tabsList all <yel>tabs</yel>
:tabrGo to fi<yel>r</yel>st tab
:tablGo to <yel>l</yel>ast tab
:tabm 0<yel>M</yel>ove to position 0
:tabnGo to <yel>n</yel>ext tab
:tabpGo to <yel>p</yel>revious tab

Normal mode

ShortcutDescription
gtGo to <yel>n</yel>ext tab
gTGo to <yel>p</yel>revious tab
2gtGo to tab number 2

{.shortcuts}

Vim Search and Replace

Search
--
/fooSearch forward
/foo\cSearch forward (case insensitive)
?fooSearch backward
/\v\d+Search with regex
nNext matching search pattern
NPrevious match
*Search for current word forward
#Search for current word backward

{.shortcuts}

Replace LINE
VIM
滚动查看更多
:[range]s/{pattern}/{str}/[flags]

:s/old/newReplace first
:s/old/new/gReplace all
:s/\vold/new/gReplace all with regex
:s/old/new/gcreplace all (Confirm)
:s/old/new/iIgnore case replace first
:2,6s/old/new/gReplace between lines 2-6
Replace FILE
VIM
滚动查看更多
:%s/{pattern}/{str}/[flags]

:%s/old/newReplace first
:%s/old/new/gReplace all
:%s/old/new/gcReplace all (Confirm)
:%s/old/new/giReplace all (ignore case)
:%s/\vold/new/gReplace all with regex
Ranges
--
%Entire file
’<,’>Current selection
5Line 5
5,10Lines 5 to 10
$Last line
2,$Lines 2 to Last
.Current line
,3Next 3 lines
-3,Forward 3 lines
Global command
VIM
滚动查看更多
:[range]g/{pattern}/[command]

:g/foo/dDelete lines containing foo
:g!/foo/dDelete lines not containing foo
:g/^\s*$/dDelete all blank lines
:g/foo/t$Copy lines containing foo to EOF
:g/foo/m$Move lines containing foo to EOF
:g/^/m0Reverse a file
:g/^/t.Duplicate every line
Inverse :g
VIM
滚动查看更多
:[range]v/{pattern}/[command]

:v/foo/dDelete lines not containing foo<br/>(also :g!/foo/d)
Flags
--
gReplace all occurrences
iIgnore case
IDon't ignore case
cConfirm each substitution
Substitute expression (magic)
--
& | \0Replace with the whole matched
\1...\9Replace with the group 0-9
\uUppercase next letter
\UUppercase following characters
\lLowercase next letter
\LLowercase following characters
\eEnd of \u, \U, \l and \L
\EEnd of \u, \U, \l and \L
Examples
C
滚动查看更多
:s/a\|b/xxx\0xxx/g		         # Modifies "a b"      to "xxxaxxx xxxbxxx"
:s/test/\U& file/                # Modifies "test"     to "TEST FILE"
:s/\(test\)/\U\1\e file/         # Modifies "test"     to "TEST file"
:s/\v([abc])([efg])/\2\1/g	     # Modifies "af fa bg" to "fa fa gb"
:s/\v\w+/\u\0/g		             # Modifies "bla bla"  to "Bla Bla"
:s/\v([ab])|([cd])/\1x/g         # Modifies "a b c d"  to "ax bx x x"
:%s/.*/\L&/                      # Modifies "HTML"     to "html"
:s/\v<(.)(\w*)/\u\1\L\2/g        # Make every first letter of a word uppercase
:%s/^\(.*\)\n\1/\1/              # Remove duplicate lines
:%s/<\/\=\(\w\+\)\>/\U&/g        # Convert HTML-Tags to uppercase
:g/^pattern/s/$/mytext           # Find and append text to the end
:g/pattern/norm! @i              # Run a macro on matching lines
/^\(.*\)\(\r\?\n\1\)\+$          # View the duplicates lines
/\v^(.*)(\r?\n\1)+$              # View the duplicates lines (very magic)
:v/./,/./-j                      # Compress blank lines into a blank line
:g/<p1>/,/<p2>/d                 # Delete inclusively from <p1> to <p2>

Vimdiff

Usage
SHELL
滚动查看更多

$ vimdiff file1 file2 [file3]
$ vim -d file1 file2 [file3]
Editing
CODE
滚动查看更多
:[range]diffget [bufspec]
:[range]diffput [bufspec]

ShortcutDescription
do / :diffgetObtain (get) difference
dp / :diffputPut difference
:difRe-scan differences
:diffoSwitch off diff mode
:1,$+1diffgetGet all differences
ZQQuit without changes

{.shortcuts}

See: Ranges

Folds
ShortcutDescription
zo / zOOpen
zc / zCClose
za / zAToggle
zvOpen folds for this line
zMClose all
zROpen all
zmFold more (foldlevel += 1)
zrFold less (foldlevel -= 1)
zxUpdate folds

{.shortcuts}

Jumping
<br>
ShortcutDescription
]cNext difference
[cPrevious difference

{.shortcuts}

Quickfix List (Advanced)

Usage

The Quickfix List is primarily used to navigate a list of errors/warnings (or file locations) generated by a compiler or linter.

ShortcutDescription
:copenOpen the quickfix window
:ccloseClose the quickfix window
:cnextGo to the next error/location (:cn also works)
:cpreviousGo to the previous error/location (:cp also works)
:clistList all errors/locations in the list
:cf [file]Populate the list with errors from a file (e.g., a compiler log)

Quickfix Do (`:cdo` and `:cfdo`)

The :cdo (quickfix do) and :cfdo (quickfix file do) commands execute a command in every file listed in the quickfix list. :cdo iterates over every location in the list, while :cfdo iterates over every unique file in the list.

CommandDescription
:cdo [cmd]Execute [cmd] for every location in the quickfix list.
:cfdo [cmd]Execute [cmd] for every unique file in the quickfix list.

Miscellaneous

Case
ShortcutDescription
vUUppercase character
vuLowercase character
~Toggle case character
viw UUppercase word
viw uLowercase word
viw ~Toggle case word
VU / gUUUppercase line
Vu / guuLowercase line
V~ / g~~Toggle case line
gggUGUppercase all text
ggguGLowercase all text
ggg~GToggle case all text

{.shortcuts}

Jumping
ShortcutDescription
<C-o>Go back to previous
<C-i>Go forward
gfGo to file in cursor
gaDisplay hex, ascii value

{.shortcuts}

Misc command-lines
--
:hHelp open help view
:edit!Reload current file
:2,8m0Move lines 2-8 to 0
:nohClear search highlights
:sortSort lines
:terOpen a terminal window
:set pasteEnable Insert Paste sub-mode
:set nopastedisable Insert Paste sub-mode
:cqExiting with an error<br/>(aborting Git)
Navigating
ShortcutDescription
%Nearest/matching {[()]}
[( | [{Previous ( or {
]) | ]{Next ) or }
[mPrevious method start
[MPrevious method end

{.shortcuts}

Counters
ShortcutDescription
<C-a>Increase number
<C-x>Decrease number

{.shortcuts}

Tags
ShortcutDescription
:tag ClassnameJump to first definition of Classname
<C-]>Jump to definition
g]See all definitions
<C-t>Go back to last tag
<C-o> <C-i>Back/forward
:tselect ClassnameFind definitions of Classname
:tjump ClassnameFind definitions of Classname (auto-select 1st)

{.shortcuts}

Formatting
--
:ce 8Center lines between 8 columns
:ri 4Right-align lines at 4 columns
:leLeft-align lines

See :help formatting

Marks
ShortcutDescription
<code>`^</code>Last position of cursor in insert mode
<code>`.</code>Last change in current buffer
<code>`"</code>Last exited current buffer
<code>`0</code>In last file edited
<code>''</code>Back to line in current buffer where jumped from
<code>``</code>Back to position in current buffer where jumped from
<code>`[</code>To beginning of previously changed or yanked text
<code>`]</code>To end of previously changed or yanked text
<code>`<</code>To beginning of last visual selection
<code>`></code>To end of last visual selection
maMark this cursor position as a
<code>`a</code>Jump to the cursor position a
'aJump to the beginning of the line with position a
<code>d'a</code>Delete from current line to line of mark a
<code>d`a</code>Delete from current position to position of mark a
<code>c'a</code>Change text from current line to line of a
<code>y`a</code>Yank text from current position to position of a
:marksList all current marks
:delm aDelete mark a
:delm a-dDelete marks a, b, c, d
:delm abcDelete marks a, b, c

{.shortcuts}

Calculator
ShortcutDescription
<C-r> = 7*7Shows the result
<C-r> = 10/2Shows the result

{.shortcuts}

Do this in INSERT mode

Shell
--
:!<shell>Interpret Shell Command
:r!<shell>Read in output of shell
:r!dateInsert date
:!!dateReplace current line with date
Command line
ShortcutDescription
<C-r><C-w>Insert current word into the command line
<C-r>"Paste from " register
<C-x><C-f>Auto-completion of path in insert mode

{.shortcuts}

Tricks

Remove duplicate lines

SHELL
滚动查看更多
:sort | %!uniq -u

To number the lines in the file

SHELL
滚动查看更多
:%!cat -n

Copy whole doc to clipboard

SHELL
滚动查看更多
:%w !pbcopy            # Mac OS X
:%w !xclip -i -sel c   # GNU/Linux
:%w !xsel -i -b        # GNU/Linux

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572