Learning Vim like a Martial Art

man doing karate stunts on gym
Photo by Uriel Soberanes on Unsplash

Learning Vim is a challenge. There are a lot of keys to remember and Vim works in a different way to other editors. I had an idea about structuring a Vim tutorial like learning a martial art, where each milestone is represented by a belt colour. We start at white belt where we learn the foundations. As we progress, we end up at black belt where we’ll be whizzing around super quick, making edits along the way.

I wrote six separate articles to get to black belt, and in this article, I’ve summarised them all in on place.

White Belt

At white belt level, we’ll learn the basics to edit a file.

  • In terminal, vim <filename> to start editing.
  • i – enter insert mode. Write.
    <Escape> – exit to return to normal mode.
  • u – undo.
    <Ctrl>+r – redo.
  • h – move left.
    j – move down.
    k – move up.
    l – move right.
  • x – delete a character.
  • :w – save your file.
    :q – quit Vim.
    :wq – write the file and close Vim.

Yellow Belt

At yellow belt level, we learn to move faster using motions and introduce copying and pasting. We also learn how to repeat commands.

Horizontal Motions

  • w – to move by word.
    b – move backwards by word.
  • 0 – move cursor to the start of the line.
    ^ – move to start of first character on line.
    $ – move to the end of the line.

Vertical Motions

  • H – move to the top of the screen.
    M – move cursor to middle of screen.
    L – move cursor to bottom of screen.
  • ( – move backwards by a sentence.
    ) – move forwards by a sentence.
  • { – move up by a paragraph.
    } – move down by a paragraph.

Copying & Pasting

  • yy – copy whole line.
  • p – paste below cursor.
    P – paste above cursor.
  • dd – delete a whole line and put into clipboard (cut).

Repeating

  • <number> + <motion> to repeat a motion.
  • . – repeat last change.

Green Belt

At green belt level, we’ll learn to get to insert mode faster, in all directions. Then, we’ll cover more motions. Finally, we introduce jumps.

Insert Mode Shortcuts

  • I – go into insert mode before the first non-blank character on the line1.
  • A – go into insert mode after the last character on the line2.
  • o – go into insert mode on the next line below.
    O – go to insert mode on the line above.
  • a – go to insert mode after the cursor.
  • s – delete character and go into insert mode.

Motions

  • W – go forwards by a WORD.
    B – go backwards a WORD.
  • e – go forwards to the end of a word.
    ge – go backwards to the end of a word.
  • <Ctrl> + u – move up by half a screen.
    <Ctrl> + d – move down by half a screen.
  • <Ctrl> + f – move up a page.
    <Ctrl> + b – move down by a page.
  • <line-number> + G – go to the line number.

Jumping

  • <Ctrl> + o – go backwards in jump list.
    <Ctrl> + i – go forwards in jump list.

Blue Belt

At blue belt level, we’ll introduce visual mode, cover more motions and combine operators with counts and motions.

Visual Mode

  • v – enter visual mode.
    Use any motion to highlight text.
  • V to highlight the entire line.
    Then use a motion to select more lines.

Vertical Motions

  • gg – go to top of the file.
    G – go to bottom of the file.
  • / <search term> <Enter> – search forwards for search term.
    n – go to the next match.
    N – go to previous match.
  • ?<search term> <Enter> – search backwards for search term.
    n – go to the next match.
    N – go to previous match.

Horizontal Motions

  • f + <character> – move cursor to the first match.
  • F + <character> – move cursor to first match before target.
  • t + <character> – move cursor just before the first match.
  • T + <character> to move your cursor just before first match.
  • For all of the above, type ; to repeat the search forwards.
    Type , to repeat search backwards.

Operator Count Motion

  • Certain commands are called operators, such as delete (d) or copy (y) or change (c). These work with counts and motions:
    • d5w – deletes 5 words.
    • V10j – highlight 10 lines below.
    • y4b – copy 4 words backwards from cursor.
    • c2W – change 2 WORDS.

More Shortcuts

  • D – to delete text from the cursor to the end of line.
  • C – cut text from the cursor to the end of line and go into insert mode.
  • cc – cut a whole line and go into insert mode.

Red Belt

At red belt level, we’ll introduce text objects and a useful commands, including a new mode.

Text Objects

Text objects select whole objects, regardless of where the cursor is. We use them in combination with operators.

  • Inner word (iw) text object examples with operators:
    • ciwchange inner word. Will put you in insert mode, ready for new text.
    • diwdelete inner word.
    • yiwyank (copy) inner word.
    • viwvisualise (highlight) inner word:
  • A word (aw) text object examples:
    • dawdelete a word.
    • cawchange a word. Will put you in insert mode to change text.
    • vawvisualise (highlight) around word:
  • Inside speech marks (i”) example:
    • vi”visualise (highlight) everything inside the (speech marks):
  • Around speech marks (a”) example:
    • ca”change around and including the (speech marks). Not that this puts you in insert mode.
  • Inside bracket (ib) example:
    • vibvisualise (highlight) everything inside the brackets.
  • Inner bracket (ab) example:
    • cabchange around bracket, works for ( ).
  • Inner tag (it) example:
    • citchange inner tag, works with any type of <tag>.
  • Around tag (at) example:
    • vatvisualise (highlight) around tag.

More Useful Commands

  • j – join, useful when want to join two split lines.
  • * (asterisk) – search for next occurrence of word under cursor. You can search for the previous occurrence using the hash key.
  • R – enter replace mode. Now everything you type overwrites existing text. <Esc> to exit replace mode.
  • r – replace one character only.

Black Belt

At black belt level, we cover searching and replacing, macros, splitting windows, scrolling and getting to the next level.

Search & Replace

  • :s/<search term>/<replace term>/g – replace everything on current line.
  • %s:/<search term>/<replace term>/g – replace everything in whole file.

The g at the end stands for global, which means all occurrences.

Macros

To record a macro:

  • q, followed any letter to start recording.
  • Next, type the sequence of commands you’d like to automate.
  • q again to stop recording.
  • @ followed by the letter above to replay macro.

For an example of how to use a macro, see this article.

Window Splits

  • <Ctrl> + w, then v for vertical split, or s for (horizontal) split
    • <Ctrl> + w, h to move left.
    • <Ctrl> + w, l to move right.
    • <Ctrl> + w, j to move down.
    • <Ctrl> + w, k to move up.
    • <Ctrl> + w, c to close the window you’re in.
    • <Ctrl> + w, o to close all windows except the one you’re in.

Once you’ve split a window, you can use e: <filename> to edit that file.

Scrolling

  • zz – scrolls the screen so the cursor is in the centre.
    zt – scroll so the cursor is at the top.
    zb – scroll so the cursor ends up at the bottom.

Beyond Black Belt

We’ve just scratched the surface with what we can do with Vim. Here are some topics you can explore to get beyond black belt:

  • Marks
  • Tags
  • Folding
  • Files
  • Tabs
  • Buffers
  • Undo Tree
  • Vim Script
  • Keymapping

Getting Help in Vim

  • help: <command> – help on command in Vim.
Published
Categorized as Vim Tagged