Vim, short for Vi Improved, is a highly configurable text editor that has been around for decades. Although it has a steep learning curve, once mastered, Vim can significantly boost productivity. In this article, I’ll share how I integrate Vim into my daily routine and how you can do the same.

Why Vim?

Vim is popular among developers and power users for several reasons:

  1. Speed - Vim allows you to perform complex text editing operations quickly using just the keyboard. Once you’re proficient, you can navigate your code at an impressive pace.
  2. Efficiency - With Vim, you can avoid moving your hands between the keyboard and mouse, saving valuable time and reducing strain on your hands.
  3. Cool - 😎

Cheatsheet

Existing

  • :q - Close the file
  • :w - Save/Write the file
  • :wq / :x - Save and close the file
  • esc / control + c - Exit insert mode

Editing

  • i - Insert before the cursor
  • a - Insert after the cursor
  • I - Insert at the beginning of the line
  • A - Insert at the end of the line
  • o - Insert a new line below the current line
  • O - Insert a new line above the current line
  • ea - Insert at the end of the word
  • x - Delete a character (cut)
  • r{character} - Replace a character with the new character
  • p - Paste
  • u - Undo
  • control + r - Redo
  • J - Join the line below with the current line

Operators

  • d - Delete
  • y - Yank (copy)
  • c - Change (delete then insert)
  • > - Indent right
  • < - Indent left
  • gU - Change to uppercase
  • gu - Change to lowercase

Operators can also be combined with movement keys. For instance:

  • d - Delete
    • dd - Delete the line (repeat the operator)
    • 2dd - Delete 2 lines
    • dw - Delete from the cursor to the end of the word
    • diw - Delete the word under the cursor
    • d$ / D - Delete from the cursor to the end of the line
    • d^ - Delete from the first non-blank character of the line to the cursor
    • dip - Delete the paragraph under the cursor
    • etc.
  • c - Change (delete then insert)
    • ci) - Change the text inside the parentheses (...)
    • ci} - Change the text inside the curly braces {...}
    • etc.
Arrow
  • h - Move cursor left
  • j - Move cursor down
  • k - Move cursor up
  • l - Move cursor right
If you want to move the arrows multiple times, such as moving down 5 lines, you can use 5j instead of holding down j or pressing j 5 times.
Word
  • w -  Jump to the start of the next word
  • b - Jump to the start of the previous word
  • e - Jump to the next end of a word
  • ge - Jump to the previous end of a word
Line
  • 0 - Jump to the start of the line
  • ^ / 0w - Jump to the first non-blank character of the line
  • $ - Jump to the end of the line
Document
  • gg - Go to the first line
  • G - Go to the last line
  • :{number} - Go to the line {number}
Paragraph
  • % - Jump between matching {}, [], ()
  • } - Go to the next paragraph
  • { - Go to the previous paragraph
Window
  • H - Jump to the top of the window
  • M - Jump to the middle of the window
  • L - Jump to the bottom of the window
Other
  • gD - Go to the global declaration (variable)

Scrolling

  • zt - Position the cursor at the top of the window
  • zz - Position the cursor at the middle of the window
  • zb - Position the cursor at the bottom of the window
  • control + e - Scroll down one line (without moving the cursor)
  • control + y - Scroll up one line (without moving the cursor)
  • control + f - Scroll down one page
  • control + b - Scroll up one page
  • control + d - Scroll down half page
  • control + u - Scroll up half page

Searching

Character
  • f{character} - Jump forward to the given character
  • F{character} - Jump backward to the given character
  • ; - Repeat the search forwards
  • , - Repeat the search backwards
Current Word
  • * - Jump to the next word under the cursor
  • # - Jump to the previous word under the cursor
Pattern
  • /{ pattern } - Search forward for the pattern
  • ?{ pattern } - Search backward for the pattern
  • /{ pattern }\c - Search forward for the pattern case insensitively
  • n - Repeat the search in the same direction
  • N - Repeat the search in the opposite direction
  • / + enter - Shows the last search
  • / + (control + p) - View the backward search history
  • / + (control + b) - View the forward search history
Replace
  • :%s/{ old }/{ new }/g - Replace all old with new
  • :%s/{ old }/{ new }/gc - Replace all old with new with confirmations

Marking Text (Visual Mode)

  • v - Start visual mode
  • V - Start visual line mode
  • control + v - Start visual block mode

After entering visual mode, you can combine it with movement or operator keys. For instance:

  • v - Start visual mode, then...
    • 2j - Mark 2 lines, then...
      • p - Paste something
    • ip - Mark the paragraph under the cursor

Tabs

  • :tabnew { filename | optional } - Open new tab
  • :e { filename } - Edit the file
  • gt / :tabn - Go to the next tab
  • gT / :tabp - Go to the previous tab
  • { number }gt - Move to tab number
  • :tabo - Close all tabs except
  • :qa - Quit all tabs

Windows

  • sp { filename | optional }
  • vsp { filename | optional }
  • ctrl+ws - Split windows horizontally
  • control + wv - Split windows vertically
  • control + ww - Switch between windows
  • control + wq - Quit a window
  • control + wL

Tips

  • Delete a line without override yank
    • "_ + dd - Register the line to _
  • Repeat new replace of search
    • cgn
  • :ter - Open terminal
  • :set nu - Show number of line
  • "+y - Copy a selection to the system clipboard
  • "+p - Paste from the system clipboard
  • :ju - Jumplist
    • control + i
    • control + o
  • :noh

Conclusion

Vim is an incredibly powerful tool for everyday tasks. By incorporating Vim into my daily workflow, I’ve significantly increased my productivity. Give it a try, and you might find yourself wondering how you ever lived without it. Happy Vimming! ⌨️

Kudos to...