The Monster known as VIM

shantanu alshi
5 min readJul 3, 2016

--

VIM can be real daunting and unfriendly to the beginner. It mysteriously does things right when you start typing in it. Unless you press ‘i’ or some other key, it just refuses to type in a new file. Some say its a good random string generator! It is this recondite nature of VIM scares tyro developers off the editor. But once you get the hang of it, its like a jetpack right inside your keyboard.

I started using it 2 years ago, and have been addicted to it since. So much so, that even my Sublime text operates in VIM mode. To start loving VIM, you must first learn to speak its language.

“Wait! Why VIM?” you might ask.

Once you start programming, you will start noticing that the component that drastically slows down your programming speed is not your computer’s memory or your own. Its the mouse. With VIM, your fingers rarely will have to leave the home row, which means you’ll be able to edit text real quick. You might as well completely ditch that mouse. Moreover, VIM is ubiquitous. It is present on all *nix systems, and is lightweight and fast. It is powerful as it supports multiple registers, macros, plugins, themes, selections, and much more.

My objective in this post is to get you familiar with the style in which VIM commands are constructed so that you make them on your own without having to mug up a list of useful commands. I’d suggest you to open a sample document in VIM and start playing around as you read.

Let’s dive right in.

Modes

First some theory. What makes VIM really powerful are it’s modes and registers. There are several modes in vim. Three of the most useful being –

  1. Normal Mode: This is vim’s default mode when you fire up the editor. There is a reason why it is called the ‘Normal’ mode. This is the one in which you speak with the Vim diction.
  2. Visual Mode and Block visual modes: Allows you to manipulate and speak to VIM on selections of texts.
  3. Command Mode: Allows you to issue editor commands to vim

So how do you speak the vim lingo? Here are some things to get started –

The Alphabet

The table below shows the alphabet in normal mode.

We will divide these into four categories –

Category 1: Verbs that are marked in red — b (back), e (end), h(left) , j(down), k(up) ,l(right), n (next), p(paste), u(undo), w(word), x(cut) do not need any objects. They act immediately on the position below the cursor.

Category 2: Others marked in green — d(delete), f(find), m(mark), t(till), v(visual), z(position) first character issued in normal mode tells the verb (action) to be done. The next character tells vim the object for the verb to act upon.

Category 3: The remaining marked in blue a(append), c(cut), i(insert), o(open line), s(substitute character) act accordingly while simultaneously switch VIM to Insert Mode.

Category 4: Special characters –

Imperatives

Now that we know a bit of the alphabet, let’s start talking. Have a look at some of the commands

Do you see a pattern?

Like the subject, verb and object make a sentence in English, an imperative in VIM consists of just a Verb or Verb + Object. The subject is always assumed to be the position under your cursor.

Its usually Category 2 or 3 commands again followed by a category 2, 3 or 4 command or any character.

Go on.. Try constructing some more commands like these and see if they work.

Prepositions

You might have noticed the ’till’ word in the descriptions of the commands above. Sometimes it isn’t so obvious. For instance, to delete till next occurrence of the word ‘d’, You cannot use ‘dd’ as that will delete the complete line. Most of the times, in cases where the object is not a special character, we use ‘t’ to denote till.

Another useful preposition is ‘in’. Apart from entering into insert mode, letter ‘i’ also denotes ‘in’. We use this especially with special characters that come in pairs — quotes, single quotes, braces, brackets etc. Look at these commands –

This is extremely useful while editing code — deleting parameters or arguments inside parentheses, deleting function code blocks, replacing contents inside an array etc.

I hope this post helped you get started with VIM and understand the logic behind its commands. You will be faster when you spend some more time using VIM and trying hard to not use it as a usual text editor. In the next post, I will cover the repeating commands, extended alphabet and visual modes.

And yeah, before you go, here’s how you exit that shit –

Cheers!

Like this:

Like Loading…

Originally published at blog.shantanualshi.com on July 3, 2016.

--

--