A brief introduction to Vim

Vim is a minimalistic but powerful editor. The best part is that it is free and open source software.

I do not want to convert you to Vim from your favourite text editor in this post. But, if you have already inclined to use Vim and are wondering how to make Vim your main editor, you are at the right place.

The first prerequisite is that you should be conversant with the terminal. Since I spend so much time on the terminal, I want it to look awesome. I use iTerm with a dark and semi-transparent background. Also, I have configured the terminal to appear or disappear just by pressing a hotkey. To run Vim, just type vim in the terminal. To open a file in Vim, type vim ~/path/to/my/file. Again, I usually locate my files using auto-completion or a fuzzy finder program called fzf.

We navigate a Vim document using commands. When you press any key after opening Vim, it is like clicking on a menu button that will perform some action on your document. Press j to move the cursor down to next line, k to move the cursor to the line above, h to move the cursor left by one character, and l to move the cursor right by one character. If you need to move the cursor in any of these directions by 2 units, press 2j2k2h, and 2l etc. The idea is that you can prefix a command with a number to repeat it that many times. Tired of moving character by character? Press 0 to move to the start of the line and $ to move to the end of the line. Press w to jump from word to next word. Of course, you can press 5w to jump by five words. Press b to jump word to word in the backward direction. If you are in the middle of a word and want to move the cursor to the end of the word, press e. My favourite commands are f and F. Press f followed by the character to which you want to jump. Is your target character to the left of the cursor? Use F instead of f. Press ; if you want to repeat any of your f or F movements.

You can navigate your document in bigger and bigger motions. Jump to previous or the next sentence by pressing ( or ); to the previous or next paragraph by pressing [ or ]. Remember to prefix a number if you need to make many such jumps. If you have an urge to press page up or page down keys, simply press and hold the control key and press b or f respectively. Want to move up or down by half page only? Press d or u with the control key instead of b or f. Wondering that the screen be redrawn so that your current cursor location comes to the center and looking for your mouse? Just press the control key and I together. This is the magic of Vim: your keyboard is your menu bar and tool bar and all your keys are your navigation and editing buttons! This is the normal mode of the Vim.

Unless you want to look up the meaning of various keys described in the previous paragraph every time you need to make the movements, device some mnemonics to remember them. It is fun! The key b means backward, f means forward, d means down and u means up. The key w means word. Well, the keys jkh, and l mean nothing that matches their functions in Vim. Yet, you can remember their functions like this: j looks like a down arrow, k looks like an upward arrow (if you self-hypnotize your mind to believe so),  h is to the left side, and l is to the right side in this set of four keys.

Once you are comfortable moving up and down in your document, you would like to know how to write and edit in Vim. Whenever you are in such a mood, press i to activate the insert mode of Vim. When you are done, press the escape key to revert back to the normal mode. As soon as you press i, the magic of Vim goes away. You press any key and it no longer works as a command. Instead, the corresponding letter is typed on the screen. The idea is simple: while writing (after pressing i), the special functions of the keys are not available. So, pressing j will type j on the screen instead of moving the cursor to the right. Plain usual editor, isn’t it?

You might be wondering that you need to hit i at the right place. Just at the point where you need to insert something. If you press i without first moving the cursor to the desired place of insertion, you have to move cursor using arrow keys in the non-Vim fashion. Vim has many commands to relive you of this extra effort. Press a instead of i if you want to insert not at the current location but after the end of the word. (Remember a as the append key.) Press I when you need to insert something at the beginning of the current line (instead of moving the cursor to the beginning of the line by pressing many h keys and then pressing i, or by pressing i and then moving left by pressing the left arrow key). Press A to append the text at the end of the current line. You can open a new line above or below the cursor, move the cursor to it and start typing there, by pressing O or o. Always remember to press the escape key whenever you are done with writing something so that you are back to the magical movements of Vim.

The best part of Vim is the way it edits a document. Whenever you finished writing, inserting, or appending something, and want to edit your writing (because you are a writer!),  just press the escape key and the editing world of Vim welcomes you. Your keyboard becomes your toolbar and keys become powerful editing buttons.

The simplest editing function is replacing a single character and still remaining in the normal mode. (Who does want to hit the escape key after deleting and inserting a single character?) So, here we go. Take the cursor to the unwanted character by pressing f and then the character key. Press x to delete the character. Press r followed by a new character if you want it to replace the old character. There are many such editing commands in Vim that keeps you in the normal mode. Press dw to delete the entire world at the cursor and dd to delete the entire line. Press D or d$ to delete everything after the cursor to the end of the line.

If you want to paste what you deleted, press p. If you want to copy something, press y and followed by some movement commands that tell Vim what to copy. So, y2w will copy two words from the cursor location. Press yy to copy the entire line and y$ to copy from the cursor location to the end of the line. Of course, pressing p will paste the copied text at the cursor location. These are the basic editing commands in Vim.

It is a little abstract to copy using the command y followed by some movement commands. If you want to make a visual selection and then copy the selected region, press v to go to the visual mode. Make your movements and see the selected region as highlighted. When satisfied with your selection, press y and to copy it.

When your document becomes large like the present post, you need to navigate by searching. Press /word to search for the word “word“. Press n for moving to the next match and N  for moving to the previous match. Press / followed the return key to repeat the search in the forward direction and press ? followed by the return key to repeat the search backward.

Here, I have described the basic Vim commands I use for viewing, writing and editing documents in Vim. There is much more to Vim than what I find useful. I want to finish this introduction to Vim by emphasising the Vim way. The way Vim approaches writing is like painting. A painter changes the modes frequently. Sometimes, he paints. Often, he simply watches the painting scanning its detail. Occasionally, he makes corrections here and there to give a finishing touch to the painting. You will feel like this painter while writing in Vim and changing modes. Another analogy is from dictation. You may think of Vim as your assistant who does jobs for you on your commands. You say, “Find (f) and move cursor to next occurrence of ‘n’ in the current line and replace (r) it by ‘m’  ” (by pressing fnrm). Vim obeys.

I hope that you will enjoy Vim more now!






Mirrors and images

Have you ever observed birds or animals encountering a mirror? They misidentify their image as someone looking at them from the backside of the mirror. If possible, they run around the mirror to verify whether someone is there! Of course, they find nothing there and get puzzled by this behavior. An image that appears to be there but is not actually there is called a virtual image. Contrast this with an image formed by a camera lens. When the lens of the camera forms an image of an object, the image is real. That’s why we can put a photographic plate or sensor at the image location and actually record the image.
Suppose you are standing in front of a verticle mirror. The image of your face will be behind the mirror at a distance equal to the distance between the mirror and your face. This is because the light travels in a straight line and reflects back when it hits a mirror at an angle equal to the angle at which it was incident on the mirror. The light rays from the different parts of your face reflect back from the mirror and our eyes detect them. Yet, our mind doesn’t interpret the whole event this way. The mind creates a perception in which the light rays traveled from the other side of the mirror toward our eyes. Hence, the rays appear to form an image behind the mirror.
If the mirror is a plane, all the reconstructed points on the other side of the mirror are of the same size and at the same distance from the mirror as their corresponding points on your face. This is because of the fact that the rays are reflected at the same angle at which they were incident on the mirror. If the mirror is not a plane, the reconstructed points on the other side of the mirror will produce a distorted image of your face. Visit a hall of mirrors, if you have not, to witness this effect.
Sometimes we have to ignore the perception of virtual image created by the mind reconstruct the location of the original objects by looking in the mirror. This is a difficult process requiring conscious thinking. One simple example is cutting our own hair by looking into the mirror. You will notice how difficult is it to judge the location of the hair and the scissors. The problem doesn’t arise in combing the hair. We have combed our hair for such a long time that our minds have adjusted to this reconstruction in this case. Another example is driving your car backward by looking in the mirrors.

(My answer to a question at Quora)