VIM Editor [Some cool features]

Swami Nikhilaananda said:
"There was a time when I thought Vim was just a bar of dish washing soap."

A text editor is one of the most important tools when writing code. There are tons of editors available. But from time immemorial two editors have always been very popular (and reasons for flame-wars) One is Emacs and the other is VI.

Most Linux distributions come pre-installed with a VI called VIM - Vi Improved. However, the editor that comes pre-installed is not the full version. So, first step, go ahead and get the full package so that you can unlock all features..

VIM is also available on Windows.

Download VIM here.

Why VIM?
Before I go to the cool features, I would like to clarify a couple of points. When there are so many awesome graphical editors out there, why should anyone even bother learning Vi? I had this exact question. I used to get annoyed with Vi in the beginning. The reason is simple. When you connect to other machines via ssh, you don't have a GUI. You HAVE to use a text based editor. And if you are not fully equipped, you will find editing in a console extremely hard, annoying and time consuming. However, even in a GUI based environment, Vim can give other editors a run for money.

Do you know Vim?
If you are a complete beginner who has never used Vi before, then this post is not for you.  Well, anyway, for you to learn, Vim has an awesome tutorial. Once you start Vi, just type :help and the help file will open with a cool tutorial. By the way, if you want to use your standard shortcuts with Vim, have a look here.

Ok, now for the cool stuff:


Did you know you can split windows!
-> Split Window
In the escape mode, type :split. This will split the window horizontally.


Switch between windows
You can press Ctrl + w Ctrl + w (that's twice) to switch between windows. You can split it several times. You can then operate on each window independently, like opening files, saving, etc.

Close window
You can window by typing :close in any window.

Vertical split window instead
Instead of :split, you can do :vsplit

Maximise a window
Ctrl + w Ctrl + _

Make windows equal again
Ctrl + w Ctrl + =

You have multiple clipboards!
You have several buffers. Ok, here's an example. Create a file which has three lines. In the escape mode, go to the first line. Then type the following.
" a yy
This like first tells which buffer we are using. Double quotes " says buffer, a is the name of the buffer, yy is the vi command to copy the entire line.

Similarly go to the second line and say
" b yy
here we have stored the second line in buffer b.

View the buffers by typing :reg in the escape mode.

To paste that, just go to a different place and in the escape mode type the following.
" b p
" a p
And you should see the contents of those buffers getting pasted. So essentially you choose a buffer before copying and again before pasting. This way you can have many buffers. Don't you think this is very useful?

Fold your code!
A feature that you have seen in several modern editors. Say you have a function that is completed. You want to minimise (or fold) that part of code so that you can free up some space and reduce clutter. Simple! Just select the code. For this, in the escape mode, type v (Visual mode) and then move your cursor around and select the code. Once done, type :fold.

This will fold the code up. If you want to expand the code, just go there and type zo. To fold again, type zc.

Align your code - no sweat!
Many times you have badly aligned code. If you want to align it, just type G=gg in the escape mode.

Tabbed interface
Did you know, apart from split windows, you also can have tabbed windows. In the escape mode, type :tabnew. You can open multiple tabs like this. To navigate between tabs, type tabn. This will move to the next tab. To move to a tabbed window, type :tabn2 to move the second tab and so on.

To close a tab, type :tabc or :tabclose or just :close.

If you are in a terminal emulator (basically terminal in GUI) then you can try doing :set mouse=a. Once that's done, you can click inside the editor with your mouse. And this will also help you navigate between  tabs by clicking, and also closing by clicking on the close button at the right side.

Macros to prevent repetition!
There are several times when you want to do repetitive tasks. Like perhaps creating a list of 100 numbers, or adding semi colons to the end of every line. You can record a macro and run it N times. Let's see an example.
Type in the number 1.
Press Esc => q => a => yy => p. Move cursor over 1 of new line. Ctrl + a => q => 98@a

What we did here was, record a macro. q indicates starting recording a macro, a is the name of the macro. yy is copying the line, p is pasting the line. Then, Ctrl + a is used to increment the value under the cursor. The next q stops recording macro. @a is used to execute the macro a. You can execute a macro N times by typing number and then running the macro. 98@a basically runs the macro 98 times. So you should have a list of numbers.

Trying creating a macro for putting semi colons at the end of every line or commenting a bunch of lines.

Bookmarks to move back and forth
Most modern editors have bookmarks. If you want to mark a particular line, then just type ma in that line. Here, m stands for marking, and a is the name of the mark. This can, of course be any name you choose, like b, 4, etc. Now that you have marked it, go to some other line. To move back to the bookmarked line, type 'a. Here, a was that bookmark

Navigate through folders with ease
If you open a directory in Vi, you will see it opening the files and folders in that directory as a list. To go into a folder or open a file, simply place cursor over it and type gf. Typing - will take you one step up.

Run shell commands without quitting the editor
In the escape mode, type :!pwd where pwd can be replaced by any command.
To open the shell however, you can do :shell. This will open the shell and you can run all your commands. Output of a command to the editor can be done like :r!ls where ls can be any command

Sort
You can quickly sort by running the :sort command. You can select a bunch of lines by using Shift + v and then do :sort for example. If you just run sort, entire file will be sorted.

That's all for now, but there are tons of features out there!
These are a bunch of awesome features I felt should be shared. I will keep updating with some more if I find them. In the beginning, it is hard - But once you get used to it, you will find it very simple to do hard things quickly.


=== Comments from Facebook ===


Aditya Kiran: 
Thanks! Was searching for some of those!
It has some fancy search and replace stuff that you can throw in there as well.
Liked the multipe buffer one. have been searching that one


***
You may also like to read
Comment and Uncomment Multiple Lines in Vim
Common Vim mappings to make your life easier (For Beginners)

Comments

Popular posts from this blog

THE CURIOUS CASE OF RAHUL GANDHI - Nitin Gupta(Rivaldo)

The (fake) Quest To Eradicate AIDS with Mythical Mystical Indian roots

The Consumer Court Case - Samsung vs Gulagulaananda