Vi Editor

14 / May / 2010 by Abhishek Tejpaul 0 comments

I will try my best to give you some tips to make Vi look more interesting and easy to use for all you guys. I have read few articles and tutorials about Vi and will try to summarize some key points here.

First things, first : WHAT IS Vi?

Vi is NOT just an editor. It has evolved to be something more than just a plain text-editor with cool features. It can easily be an IDE for programmers (but only after you become an expert in Vi). But before going into how Vi can help you in technical terms, I would like to emphasize about how to ‘let go’ of the fear of using a Vi editor.

Vi is your friend. An interesting friend. Only you have to know its language (read commands). The good thing about Vi is that it lets you perform create, edit, delete, save, find, count, select, copy/paste and so many other operations without forcing you to move your hands away from the home keys/home row (and yes, I am talking about the home keys of ‘Touch Typing’ fame). Just with a single keystroke or a combination of few keystrokes you can literally achieve anything you want to do while text-editing your documents or coding a super-cool program and that too all in a comfort of your keyboard and nice placement of your hands.

The learning curve is steep, I agree but the ROI is steeper. It is more about how you see Vi. A capable editor or a demanding one. I read few articles; used it a little bit and am convinced that it is a capable editor.

Let’s start with the basics now:

Let’s DO away with mouse. I WANNA move around through the keyboard.

The home keys, again !!! Hit h,j,k,l – to move left, down, up, and right respectively.
A nice tip to remember the movement keys :

h is on the left – so left it goes,

l is on the right – so right it goes,

j looks like a down arrow key – so down it goes,

k  (well the only way to go now is UP) – so up it goes.

If you have a photographic memory, this picture might help you. This is an ADM3A terminal, the type of machine used originally by Bill Joy (the creator of Vi) to code the C shell on which Vi runs. This could be the reason why h,j,k,l are movement keys.

The modes in Vi : Command mode and Insert mode.

The name says it all. In the Command mode, Vi waits for your commands i.e. the keystrokes to let you copy, move, replace, delete, change, go to a particular line/word etc. The good thing about Vi is it gives you different ways to achieve the same thing. You want to go end of current line: you can achieve that in different ways

Way 1 – Pressing l (the small L) again and again till you reach end of line.

Way 2 – Press e (command to go to end of the word) again and again till you reach your destination.

Way 3 –  Press f (command to find a character) followed by the character (which is the last character of your line) again and again to reach the end of line.

Example: “This is a sample line.”

Now, to reach the end of this line(assuming your cursor is somewhere on the line) all you have to do is press ‘f’ followed by ‘.’  since ‘.’ – period is the last character of your line.

Way 4 – Just hit the dollar sign i.e. ‘$’.

And this list is still not exhaustive, I believe. The more familiar you will become with Vi the more ways you will have to achieve the things. Some smarter ways, some dumber ways. But they all will get the job done.

The second mode is the Insert mode. In this mode, the Vi takes a back seat (to say so) and lets you  modify the contents of your document the way you want to. It is the normal text edit mode that we work on while using Notepad, Wordpad, MS Word, gedit and so on.

Well, the good place to start learning Vi is vimtutor. Open the shell(terminal) for Linux, type in ‘vimtutor’ and ‘get going’ !! It is an interactive tutorial which lets you practice while you learn.

On the ending note, I wish you all good time with Vi and will try to continue writing more about Vi editor in the future blogs.

– Abhishek

[Intelligrape Software Pvt. Ltd.]

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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