Speed Up Your Editing with Vim

What is Vim?

Vim is a free, open-source, lightweight, and highly customizable text editor for code and plain text. It's fast and efficient because it's designed for keyboard-driven editing, so you can rarely need a mouse. It's an improved version of the classic Unix editor vi. Vim runs on almost every platform, including Linux, macOS, and Windows.

I've used Vim daily at work for about five years. In my experience, it has a steep learning curve and can intimidating at first, especially for beginners or occasional users. This guides rounds up the most useful shortcusts, organized by category and illustraded with examples, to help you get productive quickly.

Cursor Movement (Words, Symbols, syntax tokens)

Words and Symbol

Note
  • 3w jumbs three word ahead.
  • 5j jumbs down by line five.

Line Navigation

Find with a line (single character)

Note
  • Type f, → cursor lands on the comma after foo.
  • Type t, → cursor lands before that comma.
  • After f,, press ; to jump to the next comma.
  • Press , to go back to the prev comma.
  • Go left, from anywhere on the line, F= lands on previous =; T= lands just after it.
  • To replace foo, bar, baz; - /\w\+ Enter cgnHello<Esc> then n .

By sentences and paragraph

By Screen and scrolling

File Navigation

Editing and Inserting

Cursor anywhere on the line - A; <Esc> - adds ; at the end

Delete / Change

Change

Copy (yank) / paste

Undo / Redo

Dot repeat (very important)

Visual mode

v - start selection
vww - move 2 words forward
Note

Common action on selection

  • d - delete
  • y - yank
  • c - change (delete + insert)
  • ~ - toggle case
  • gU / gu - upper / lower

Line wise (V)

  • V - select current line
  • Vjj - select current + 2 lines below

Then

  • > / < - indent / outdent

Example: Vjj< / Vjj >

Visual Block

Suppose you have foo = 1; bar = 2; baz = 3;

  • Put cursor on = of the first line
  • Press ctrl+vjj, column block over three lines.
  • Type I then add a spcae. e.g., I, then <Esc>.

Search and Replace

Basic searching

Substitue

Tip

With confirmation

Type - :%s/old/new/gc

Then:

  • y - yes
  • n - no
  • a - all
  • q - quit

using regex

:%s/\vfoo\d+/bar/g

  • \d+ = one ore more digits
  • Replace foo123 to bar

Text Objects (the real power)

Pattern to remember: operator + text object - daw, ci" vi(, etc.

Use them with:

  1. Word text objects: iw / aw
foo bar baz
    ^

Cursor on b:

  1. Sentence and paragraph: is / as, ip / ap
This is a sentence. Here is another one.
 
This is a new paragraph.

Cursor inside the first sentence:

  1. Quotes i" / a" etc
const msg = "Hello world!";
               ^

Cursor on Hello:

Works for ', " ```:

  1. Paranthese / brackets / brackets
foo(bar, baz + qux)
      ^

Cursor somewhere inside (...):

Tip

Same pattern for { / }, [ / ], < / >

  1. Tags: it / at (HTML / XML) For HTML/XML and many setups:
<div class="box">
  <span>Hello</span>
</div>

Cursor inside span:

Note

If this doesn't work, you may need :set matchpairs+=<:> or a plugin, but many Vim configs support t by default.

Thank you for taking the time to read this post. I hope it helps you become faster and more confident with Vim - happy hacking!