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.

Navigating through a file efficiently is crucial in Vim. Here are some key shortcuts to help you move around:

  • Basic Movements:
    • h – Move cursor left
    • j – Move cursor down
    • k – Move cursor up
    • l – Move cursor right
  • Screen Movements:
    • H – Move to top of screen
    • M – Move to middle of screen
    • L – Move to bottom of screen
  • Word Movements:
    • w – Jump forwards to the start of a word
    • W – Jump forwards to the start of a word (words can contain punctuation)
    • e – Jump forwards to the end of a word
    • E – Jump forwards to the end of a word (words can contain punctuation)
    • b – Jump backwards to the start of a word
    • B – 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 line
    • g_ – Jump to the last non-blank character of the line
  • File Movements:
    • gg – Go to the first line of the document
    • G – Go to the last line of the document
    • 5G – Go to line 5
  • Character and Paragraph Movements:
    • fx – Jump to next occurrence of character x
    • tx – Jump to before next occurrence of character x
    • } – 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 cursor
    • I – Insert at the beginning of the line
    • a – Insert (append) after the cursor
    • A – Insert (append) at the end of the line
    • o – Append (open) a new line below the current line
    • O – 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 line
    • cw – Change (replace) to the end of the word
    • c$ – Change (replace) to the end of the line
    • s – Delete character and substitute text
  • Joining Lines:
    • J – Join line below to the current one with a space in between
    • gJ – Join line below to the current one with no space in between
  • Undo and Redo:
    • u – Undo
    • Ctrl + 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 lines
    • yw – 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 cursor
    • P – 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 operation
    • V – Start linewise visual mode
    • Ctrl + v – Start blockwise visual mode
  • Visual Mode Commands:
    • > – Shift text right
    • < – Shift text left
    • y – Yank (copy) marked text
    • d – 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 file
    • Ctrl + i – Move to next instance in jump list
    • Ctrl + 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 register x
    • "xp – Paste contents of register x

Advanced Navigation and Editing

  • Screen Adjustments:
    • zz – Center cursor on screen
    • zt – Move cursor to the top of the screen
    • zb – 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 history
    • q: – 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.

Leave a Reply

Your email address will not be published. Required fields are marked *