Vim Keyboard Shortcuts Cheatsheet For Productivity and Efficiency Boost
Vim, a powerful and versatile text editor, is a favorite among developers and sysadmins due to its extensive set of keyboard shortcuts that enhance productivity. Mastering these shortcuts can significantly improve your editing experience, making it essential to have a comprehensive cheat sheet at hand.
Navigation and Cursor Movements
Navigating through a file efficiently is crucial in Vim. Here are some key shortcuts to help you move around:
- Basic Movements:
h– Move cursor leftj– Move cursor downk– Move cursor upl– Move cursor right
- Screen Movements:
H– Move to top of screenM– Move to middle of screenL– Move to bottom of screen
- Word Movements:
w– 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)
- Line Movements:
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 line
- File Movements:
gg– Go to the first line of the documentG– Go to the last line of the document5G– Go to line 5
- Character and Paragraph Movements:
fx– Jump to next occurrence of characterxtx– Jump to before next occurrence of characterx}– Jump to next paragraph (or function/block, when editing code){– Jump to previous paragraph (or function/block, when editing code)
Text Manipulation
Vim offers a variety of commands to manipulate text efficiently:
- Insert Mode:
i– Insert before the cursorI– Insert at the 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 line
- Change and Replace:
r– Replace a single character (and return to command mode)cc– Change (replace) entire linecw– Change (replace) to the end of the wordc$– Change (replace) to the end of the lines– Delete character and substitute text
- Joining Lines:
J– Join line below to the current one with a space in betweengJ– Join line below to the current one with no space in between
- Undo and Redo:
u– UndoCtrl + r– Redo.– Repeat last command
Cutting, Copying, and Pasting
Understanding how to cut, copy, and paste text is fundamental in Vim:
- Yanking (Copying):
yy– Yank (copy) entire line#yy– Yank (copy) the specified number of linesyw– Yank (copy) the characters of a word
- Deleting (Cutting):
dd– Delete (cut) entire line#dd– Delete (cut) the specified number of lines
- Pasting:
p– Paste after the cursorP– Paste before the cursor
Visual Mode
Visual mode allows you to highlight text and perform operations on it:
- Entering Visual Mode:
v– Start visual mode, mark lines, then perform an operationV– Start linewise visual modeCtrl + v– Start blockwise visual mode
- Visual Mode Commands:
>– Shift text right<– Shift text lefty– Yank (copy) marked textd– Delete marked text~– Switch case
Marks and Jumps
Marks and jumps help you navigate quickly to specific positions in your file:
- Setting Marks:
ma– Set marker “a” at the current line'a– Jump to marker “a”
- Jumping:
`– Jump to position marked`0– Jump to position where Vim was last exited`.– Jump to last change in fileCtrl + i– Move to next instance in jump listCtrl + o– Move to previous instance in jump list
Macros
Macros allow you to record and replay sequences of commands:
- Recording Macros:
qa– Record macro “a”q– Stop recording macro@a– Run macro “a”@@– Rerun last run macro
Advanced Commands and Tips
Using Registers
Registers in Vim allow you to store and retrieve text:
- Showing Registers:
:reg– Show registers content"xy– Yank into registerx"xp– Paste contents of registerx
Advanced Navigation and Editing
- Screen Adjustments:
zz– Center cursor on screenzt– Move cursor to the top of the screenzb– Move cursor to the bottom of the screen
- Arithmetic and Expression Register:
<C-r>0– Paste text in insert mode<C-r>=– Do arithmetic and paste (using the expression register)
- Incrementing and Decrementing:
<C-a>and<C-x>– Increment/decrement next number of line
- Traversing Jump List:
<C-o>and<C-i>– Traverse jump list
Ex Commands
Ex commands provide powerful ways to manipulate text and navigate:
- Search History and Command History:
q/– Opens a window with your search historyq:– Opens a window with your ex command history
- Sorting and Copying:
:%sort– Sorts all lines:/text/,24 t .– Finds next occurrence of "text", captures up to line 24, and copies to the current line
Folding
Folding allows you to hide and show sections of your file:
- Toggling Folds:
za– Toggle fold
By mastering these shortcuts and commands, you can significantly enhance your productivity when using Vim, making it an indispensable tool in your workflow.