The Monster known as VIM

In March 2017, Stack Overflow published an article that during weekdays, there are about 80 people per hour who struggle to exit out from the editor, and that’s just Stack Overflow’s traffic. 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 special 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.

158sw5

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.

a: append j: down s: substitute character
b: back k: up t: to / till
c: cut  l: right u: undo
d: delete m: mark v: visual
e: end (of the word) n: next (search) w: word
f: find o: open line x: cut (character)
h: left p: paste below y: yank
i: insert r: replace z: position line

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 –

# (Pound): Find previous occurence of word under cursor * (Astrix):  Find next occurence of word under cursor ^ (Caret): First Non-empty character in the current line
>>: Indent to Right <<: Indent to left /: Find in document
$: End of the line ~(Tilde): Convert to uppercase `(Backtick): Convert to lowercase
[[: Start of the document ]]: End of the document (Colon): Accept a vim command
{: Previous blank line }: Next blank line .(Dot): Repeat last action

Imperatives

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

d$: Delete till end of the line
d^: Delete till start of the line
d}: Delete till the next blank line
d]]: Delete till the end of the document
dw: Delete till start of next word (Excluding that char)
de: Delete till the end of current word (Including last char)
fd: Find character ‘d’ (in the current line)
v$: Visual till the end of the line

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.

dtd: Delete till next ‘d’
dt.: Delete till next full-stop

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 –

ci{ : Cut (Delete and switch to insert mode) everything inside curly braces
di”: Delete everything inside quotes

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.

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

:w  Write (Save)
:wq Write and quit (Save and quit)
:q! Quit without saving

Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *