How to quit Vim: Or how I stopped fearing Vim, and started loving it!

What is this?

Oh, this aims to be a non-comprehensive introduction to the world of Vim, or rather Vim motions. It's important to stress the non-comprehensive-part here, and in the same breath acknowledge that there is a lot, lot more to the vim-way of working with text.

Who is this for?

Anyone who works with text... No, really. I am using Vim motions to write this blogpost in Obsidian. You may find that there exists a Vim mode or Vim keymap in many a text editor, issue tracker, or even as a Chrome Extension. The idea of Vim is not so much the software you use, but how you move around in and manipulate the text that you are working with.

Why should I care?

If you find yourself reaching for the mouse or trackpad often, or perhaps you sometimes wrote something regret it, and now you are pressing until you reach the offending text. Perhaps you simply wish it was easier to wrap some text in quotation marks or brackets. Vim makes it easier to navigate and manipulate text using only the keyboard.

Why Vim, what, who, where, how?

(Neo) (Vi)sual (Im)proved editor

Vi, Vim, Nvim (and some other editors); same same, but different. To reiterate. It is not import where, but how. So let's start, unironically, with the where.

As mentioned, and apart from the Chrome Extension and Obsidian mentioned above, you are likely to find a Vim keymap or Vim mode in your favorite text editor. Here are a few:

  • vi or vim are usually installed in your commandline. Try them out in your favourite terminal.
  • You can download Neovim, a more modern approach to Vim (where Vim was a modernization of vi).
  • VSCode has several plugins: Vim Keymap and my favourite VSCode Neovim.
  • In addition you can find Learn Vim which I definitely recommend trying out after this intro.
  • JetBrains IDEA has IdeaVim.
  • Other JetBrains products have built in emulations
  • Sublime, Zed, Emacs and many more also have their own.

Now that you have some form of Vim installed, why not... trot along‽


Music

Vim utilises melodies, whereas you may be used to chords.

A chord may be thought of as a combination of keys entered simultaneously. E.g. alt+f4. A melody on the other hand, would then be a sequence of key presses. E.g. ciw.

By using a navigation layer separate from an input layer, Vim lets you use melodies to navigate and manipulate the text without having to resort to a bunch of difficult keyboard shortcuts. You will soon see how Vimming (the act of using Vim) is akin to playing music.


Moving about

Try it out (open a document like a blog post you are writing or some code)! If you get lost, try hitting Esc a few times.

The basics

The primary mode of navigation in Vim is h (left),j (down),k (up), and l (right).

Perhaps unintuitive, given that you have arrow-keys dedicated to moving around normally, but the change is rooted in a wider design philosophy that is to keep your fingers firmly on the Home Row.

It may be tempting to navigate like this and call it a day, slightly better placement of keys, a small win. But honestly, we can do better. Try typing a number before typing the navigation key. E.g. 5j. As anticipated, this takes you five rows down, just like 3l will navigate three letters to the right.

But wait, there's more!

Contextuals

What if Vim understood text, or even better context? Turns out it does, and can! Let us look at some ways to navigate text:

  • Move to the start of a word.
  • Move to the end of a word.
  • Move back a word.

You may have noticed that special characters—like .,", or (—are treated as their own words. You can skip over them by instead pressing W ,E,B.

Moving between words is not the only power of Vim motions, however. ( and ) navigates between sentences, and {}, navigates paragraphs. Finally, 0 moves you to the start of a line, and $ moves you to the end of the same line.

And that is enough for now...

... But let me blow your mind for just one second.

I have the power

Try typing f followed by any character that is on the same line as your cursor. E.g. fi. Notice how it jumps to the first instance of that character on that line. Now press ; to cycle forward, and , to cycle backwards... \\\\٩( 'ω' )و ////


Modes, do the NVIC dance

What you probably should have known from the beginning, but also what is a bit too complex to begin with.

How do I know where I am?

Navigating text in Vim by pressing a sequence of letters is made possible by a feature we call modes. By default you start in the Normal-mode often indicated by a status indicator; "NOR " somewhere in the footer of your editor (or by your cursor width).

There are a total of seven different modes. But we will only look at the four basic ones here. Starting with...

Normal

Esc / Ctrl+c

Normal-mode is where we navigate text, it is where you want to escape back to whenever you are not inputing text, and it can be entered by pressing the Esc-key.

Insert

i / I

Insert-mode is the mode you will likely find yourself most in, actually editing text. This mode can be entered by pressing i from Normal-mode, or I from Visual-mode. Speaking of...

Visual

v (simple) / V (line) / Ctrl+v (Block)

Visual-mode is for selecting text. It can be useful if you want to replace a selection of text, or wrap text in. There are different types of Visual-mode that allows for various ways of selecting text from Normal-mode.

Replace

r (single) / R (continuous)

Finally, Replace-mode is useful for replacing a single or several characters under the cursor. Simply press r in Normal-mode to replace the character under the cursor.


Flowing edits

Almost there... You are doing great! Now, let us look at the features that make Vim motions truly powerful.

replace

Replace a single character under the cursor by pressing r followed by the character you want to replace it with. E.g. ro will replace the character under the cursor with an o.

change

Perhaps the most useful of the edit commands. Change is a combination of delete and insert. It can be used to replace a single character, a word, a sentence, or even a paragraph. E.g. ciw will change the word under the cursor, and ci) will change the sentence under the cursor.

delete

Self explanatory. E.g. diw will delete the word under the cursor, and di) will delete the sentence under the cursor.

insert

Enters Insert-mode. E.g. i will enter Insert-mode at the cursor, and I will enter Insert-mode at the start of the line.

add

Enters Insert-mode, but after the cursor. E.g. a will enter Insert-mode after the cursor, and A will enter Insert-mode at the end of the line.

x-out

Removes a single character under the cursor. E.g. x will remove the character under the cursor. X will remove the character before the cursor.

yank

Copies a single character, word, sentence, or paragraph. E.g. yiw will copy the word under the cursor, and yi} will copy the paragraph under the cursor.

paste

Pastes the last copied—or deleted—text. E.g. p will paste the last copied text after the cursor, and P will paste the last copied text before the cursor.

And that is it for now. You are now ready to start Vimming! Try to type :tutor in Normal-mode to get a more comprehensive introduction to Vim. Or vimtutor in your terminal. Or check below for even more learning resources.


So, how do I quit Vim?

:qa!

You are welcome! But really, try to give Vim a chance and use wq instead.


Learning resources