VimCheatSheet
Here's a comprehensive Vim cheat sheet, summarizing key commands for efficient navigation, editing, and file management:
Global Commands
:h[elp] keyword- Open help for a keyword:sav[eas] file- Save file as:clo[se]- Close current pane:ter[minal]- Open terminal windowK- Open man page for word under the cursor
Cursor Movement
h- Move cursor leftj- Move cursor downk- Move cursor upl- Move cursor rightgj- Move cursor down (multi-line text)gk- Move cursor up (multi-line text)H- Move to top of screenM- Move to middle of screenL- Move to bottom of screenw- Jump forward to start of worde- Jump forward to the end of wordb- Jump backward to start of word%- Move cursor to matching character (e.g., parentheses)0- Jump to start of line$- Jump to end of linegg- Go to first line of documentG- Go to last line of documentfx- Jump to next occurrence of characterxtx- Jump to before next occurrence of characterx;- Repeat previousf,t,F,Tmovement
Insert Mode
i- Insert before cursorI- Insert at the beginning of linea- Insert (append) after cursorA- Insert (append) at end of lineo- Open a new line belowO- Open a new line aboveCtrl + h- Delete char before cursorEscorCtrl + c- Exit insert mode
Editing
r- Replace a characterJ- Join the line below with a spacegJ- Join the line below without spacecc- Change (replace) entire lineciw- Change (replace) entire wordu- UndoCtrl + r- Redo.- Repeat last command
Visual Mode
v- Start visual modeV- Start linewise visual modeCtrl + v- Start visual block modey- Yank (copy) marked textd- Delete marked text~- Switch case of marked textu- Convert marked text to lowercaseU- Convert marked text to uppercase
Registers
:reg- Show registers content"xy- Yank into registerx"xp- Paste from registerx"+y- Yank into system clipboard"+p- Paste from system clipboard
Marks and Positions
:marks- List marksma- Set current position for markA`a- Jump to position of markACtrl + i- Go to newer position in jump listCtrl + o- Go to older position in jump list
Macros
qa- Record macroaq- Stop recording macro@a- Run macroa@@- Rerun last run macro
Cut and Paste
yy- Yank (copy) a line2yy- Yank (copy) 2 linesdw- Delete wordp- Paste after cursorP- Paste before cursordd- Delete (cut) a line
Indent Text
>>- Indent line right<<- Indent line left>%- Indent a block with()or{}(cursor on brace)=%- Re-indent a block with()or{}
Exiting Vim
:w- Save the file but don't exit:wqorZZ- Save and quit:q!- Quit without saving:wqa- Save and quit on all tabs
Search and Replace
/pattern- Search for pattern?pattern- Search backward for pattern:%s/old/new/g- Replace all occurrences ofoldwithnew:%s/old/new/gc- Replace with confirmation for each
Tabs and Buffers
:tabnew- Open file in a new tabgt- Move to the next tabgT- Move to the previous tab:e[dit] file- Edit a file in a new buffer:bn[ext]- Go to the next buffer:bd[elete]- Delete a buffer (close a file)
Working with Multiple Files
:sp[lit] file- Open file in a new split window:vs[plit] file- Open file in a new vertical split windowCtrl + ws- Split window horizontallyCtrl + wv- Split window verticallyCtrl + wq- Close current window
This cheat sheet covers the essentials for working with Vim. For more advanced usage, refer to the :help command in Vim itself.
Here’s a summary and clarification of the Vim commands you’ve listed, categorized by functionality:
Working with Multiple Files (Buffers and Windows)
-
Buffers:
:e[dit] file– Open a file in a new buffer.:bn[ext]– Switch to the next buffer.:bp[revious]– Switch to the previous buffer.:bd[elete]– Close a buffer (delete from the buffer list).:b[uffer]#– Switch to a buffer by its index number.:b[uffer] file– Switch to a buffer by its file name.:lsor:buffers– List all open buffers.
-
Splitting Windows:
:sp[lit] file– Open a file in a new buffer and split the window horizontally.:vs[plit] file– Open a file in a new buffer and split the window vertically.:vert[ical] ba[ll]– Open all buffers in vertical windows.:tab ba[ll]– Open all buffers as tabs.
-
Window Navigation (with Ctrl + w commands):
Ctrl + ws– Split the window.Ctrl + wv– Split the window vertically.Ctrl + ww– Switch to the next window.Ctrl + wq– Close the current window.Ctrl + wx– Exchange the current window with the next one.Ctrl + w=– Make all windows equal size.Ctrl + wh– Move the cursor to the window to the left (horizontal split).Ctrl + wl– Move the cursor to the window to the right (horizontal split).Ctrl + wj– Move the cursor to the window below (vertical split).Ctrl + wk– Move the cursor to the window above (vertical split).Ctrl + wH– Make the current window full height at the far left.Ctrl + wL– Make the current window full height at the far right.Ctrl + wJ– Make the current window full width at the very bottom.Ctrl + wK– Make the current window full width at the very top.
Folding Commands (for Code/Content Management)
-
Folding (Hiding/Showing Code or Sections):
zf– Manually define a fold up to a certain motion (e.g.,zf2jto fold up to 2 lines down).zd– Delete the fold under the cursor.za– Toggle the fold under the cursor.zo– Open the fold under the cursor.zc– Close the fold under the cursor.zr– Reduce (open) all folds by one level.zm– Fold more (close) all folds by one level.zi– Toggle the folding functionality.
Tip: Uppercase letters (e.g.,
zA,zR) work on all levels of folding, not just the current one.
Diffing Files (Comparing Differences)
- Diff Commands:
]c– Jump to the start of the next change in the diff.[c– Jump to the start of the previous change in the diff.door:diffg[et]– Obtain the differences from another buffer/file.dpor:diffpu[t]– Put the differences into the current buffer/file.:diffthis– Make the current window part of the diff comparison.:diffupdate– Update the diff view.:diffoff– Disable diff mode for the current window.
Tip: To start Vim in diff mode directly, you can run vimdiff in a terminal. Additionally, Vim can be set as the default tool for Git diffs via git difftool.
Let me know if you need further clarification or examples on using these commands!
Reference