1213 字
6 分钟
Vim Cheatsheet
Vim Cheatsheet
See helpful vim commands right from your editor, narrow down your list by toggling off the once you’ve memorised/mastered. Make sure you’ve installed the Vim extension (link).
Cursor Movement
h– move cursor leftj– move cursor downk– move cursor upl– move cursor rightH– move to top of screenM– move to middle of screenL– move to bottom of screenw– jump forwards to the start of a wordW– jump forwards to the start of a word (words can contain punctuation)e– jump forwards to the end of a wordE– jump forwards to the end of a word (words can contain punctuation)b– jump backwards to the start of a wordB– jump backwards to the start of a word (words can contain punctuation)0– jump to the start of the line^– jump to the first non-blank character of the line$– jump to the end of the lineg_– jump to the last non-blank character of the linegg– go to the first line of the documentG– go to the last line of the document5G– go to line 5 (replace 5 with any line number)f<x>– jump to next occurrence of character<x>t<x>– jump to before next occurrence of character<x>}– jump to next paragraph (or function/block in code){– jump to previous paragraph (or function/block in code)zz– center cursor on screenCtrl+b– move back one full screenCtrl+f– move forward one full screenCtrl+d– move forward half a screenCtrl+u– move back half a screen
Tip: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Tip: Instead of b or B you can also use ( or { respectively to jump between words.
Insert Mode – inserting/appending text
i– insert before the cursorI– insert at beginning of the linea– insert (append) after the cursorA– insert (append) at the end of the lineo– append (open) a new line below the current lineO– append (open) a new line above the current lineea– insert (append) at the end of the wordCtrl+h– delete the character before the cursor during insert modeCtrl+w– delete the word before the cursor during insert modeCtrl+j– begin new line during insert modeCtrl+t– indent (move right) one shiftwidth during insert modeCtrl+d– de-indent (move left) one shiftwidth during insert modeCtrl+n– insert (auto-complete) next match before the cursorCtrl+p– insert (auto-complete) previous match before the cursorCtrl+rx– insert the contents of registerxCtrl+ox– temporarily enter normal mode to issue one normal-mode commandxEsc– exit insert mode
Editing
r– replace a single characterR– replace more than one character untilEscis pressedJ– join the line below to the current one with a spacegJ– join the line below without a spacegwip– reflow (format) paragraphg~+ motion – switch case up to motiongu+ motion – change to lowercase up to motiongU+ motion – change to uppercase up to motioncc– change (replace) entire linec$orC– change (replace) to the end of the lineciw– change (replace) entire wordcworce– change (replace) to the end of the words– delete character and start insert (substitute)S– delete line and start insert (same ascc)xp– transpose two letters (delete then paste)u– undoU– restore (undo) last change on lineCtrl+r– redo.– repeat last command
Visual Mode (marking text)
v– start visual mode (character-wise)V– start linewise visual modeo– move to other end of marked areaCtrl+v– start visual block modeO– move to other corner of blockaw– mark a wordab– mark a block with()aB– mark a block with{}at– mark a block with<>tagsib– inner block with()iB– inner block with{}it– inner block with<>tagsEsc– exit visual mode
Visual Commands
>– shift text right<– shift text lefty– yank (copy) marked textd– delete marked text~– switch caseu– change marked text to lowercaseU– change marked text to uppercase
Registers
:reg[isters]– show registers content"xy– yank into registerx"xp– paste contents of registerx"+y– yank into system clipboard register"+p– paste from system clipboard register
Tip: Registers are stored in ~/.viminfo and reloaded next time vim starts.
Special Registers
0– last yank"– unnamed register (last delete or yank)%– current file name#– alternate file name*– clipboard contents (X11 primary)+– clipboard contents (X11 clipboard)/– last search pattern:– last command-line.– last inserted text-– last small (less than a line) delete=– expression register_– black hole register
Marks and Positions
:marks– list of marksma– set markaat current position`a– jump to position of markay\'a– yank text to position of marka`0– go to position where Vim was previously exited`"– go to position when last editing this file`.`– go to position of last edit in this file“ – go to position before the last jump:ju[mps]– list of jumpsCtrl+i– go to newer position in jump listCtrl+o– go to older position in jump list:changes– list of changesg– go to newer position in change listg;– go to older position in change listCtrl+]– jump to tag under cursor
Tip: To jump to a mark use a backtick (') or an apostrophe ('). Apostrophe jumps to the first non-blank of the line holding the mark.
Macros
qa– record macro into registeraq– stop recording@a– run macroa@@– rerun last executed macro
Cut and Paste
yy– yank (copy) a line2y– yank two linesyw– yank to start of next wordyiw– yank inner wordyaw– yank word and surrounding spacey$orY– yank to end of linep– put (paste) after cursorP– put before cursorgp– put after cursor and leave cursor after textgP– put before cursor and leave cursor after textdd– delete (cut) a line2dd– delete two linesdiw– delete inner worddaw– delete word and surrounding spaced$orD– delete to end of linex– delete character
Indent Text
>>– indent line one shiftwidth right<<– de-indent line one shiftwidth left>%– indent a block with()or{}(cursor on brace)>ib– indent inner block with()>at– indent a block with<>tags3==– re-indent three lines=%– re-indent a block with()or{}=iB– re-indent inner block with{}gg=G– re-indent entire buffer]p– paste and adjust indent to current line
Exiting
:w– write (save) the file without exiting:w !sudo tee %– save file with sudo:wq,:xorZZ– write and quit:q– quit (fails if unsaved changes):q!orZQ– quit without saving:wqa– write and quit all tabs
Search and Replace
/pattern– search forward?pattern– search backward\vpattern– very magic pattern (regex shorthand)n– repeat search in same directionN– repeat search in opposite direction:%s/old/new/g– replace all occurrences in file:%s/old/new/gc– replace with confirmation:nohlsearch– clear search highlighting
Folding and Diff
za– toggle fold under cursorzo– open fold under cursorzc– close fold under cursorzr– reduce (open) all folds one levelzm– fold more (close) all folds one levelzi– toggle folding functionality]c– go to start of next change[c– go to start of previous change
Tip: Folding commands operate on one level; use uppercase (e.g. zA) to affect all levels.
部分信息可能已经过时




