VIM Editor

VI (Improved) aka Vim is a text editor for Linux. It is used for efficient editing of source code. The predecessor called VI comes prepackaged as the default editor for many flavors of Linux. It is a command line editor but there is also a version with GUI.  It is intended for power users and honestly, is quite hard to master. Do not expect Vim to hold your hand and walk you through it. However, once you learn it, the speed of writing code improves drastically.
The awesome power of the editor, staying true to the spirit of open source, comes from the fact that it is highly customizable. You can practically automate the entire task of writing programs.


Here I introduce you to the basic concepts that's just about enough to improve your performance.


Before we begin I would suggest you to set your terminal colour scheme to green text on black background. This reduces eyestrain, you also get the Matrix feel.


  • Starting off with Vim: 

Type vi<space><program_name>.<extension> in the terminal

For this demonstration, lets start with a C program, that means .c extension
What you see after opening Vim might not appear very intimidating, but don't let the looks fool you.

At this point, if you start typing, you might find all sorts of things happening but nothing on the screen.
To enter text, Vim has to be placed in the insert mode.
Please note that Vim is case-sensitive
If you happen to be in the wrong mode, hit escape and then i, otherwise just press i to enter insert mode
You may now start typing code. 

There's another problem, Vim doesn't seem to know to format your code very well. The indentation is all wrong and you have to enter the tabs yourself. If you find this frustrating, tell Vim to do it for you.
Hit esc to exit insert mode. Type :set cindent and hit enter. 
Now press i and continue with your program. You'll notice that Vim will start indenting the text, pretty!.

Once you have finished writing your code, hit escape then :w (stands for write) this will save the file.
To exit Vim, type :q
Vim is smart, it will not let you exit if you do not save your code first, if you want to force exit type :q!
You can combine save and exit by typing :wq

  • Navigating through your code:

Just like in other editors, you can move around using the arrow keys. But remember that Vim is not like other editors so it provides you with a quicker alternative.
Move to somewhere in between your text and enter normal mode by pressing esc.

Now try pressing , h, j, k, and l one after the other and observe the cursor after each keystroke.
Letters j and k control vertical motion while h and l control horizontal.
You may find this rather odd at first,I know I didn't like it, but the you'll soon start to realize why this is faster. Since you do not take your hands off the home row, moving around becomes much faster.
I will not focus on the navigation much because Vim has an inbuilt tutor to help you with this, exit Vim and type vimtutor in the command line. I suggest you go through this because it can really take a chunk out of your editing time.

  • Vim Essentials:

You would have noticed that all the text, including key words appear with the same colour. Enabling syntax highlighting makes the code more readable and also makes error detection easy. 
To enable syntax highlighting        :set syntax

Line numbers help a lot when it comes to error correction. Most compilers tell you the line number where the error has occurred 
To enable line numbers           :set number
To disable line numbers          :set nonumber

You may have noticed that Vim, by default does not allow selection using the mouse pointer. The keyboard is theoretically faster than the mouse. But if you find it uncomfortable, you can bring the mouse pointer back 
To enable mouse pointer             :set mouse=a

At times you may want to view two or more files at the same time, for example if you are cross referencing your code with someone else's. You'll have to shift back and forth between two windows. To make thing simpler and clutter free, you have the option to split the screen 
To split screen horizontally              :split <path to file>
To split screen vertically                  :vs <path to file> 


Split in action

You may want to change the cursor from one screen to anther, the mouse need not be touched for this, just hit CTRL-w CTRL-w (twice) to shift focus.

Abbreviations can boost your typing speeds. Vim allows you to assign short forms that automatically expand when written. For example , in Java newbies often have to write System.out.println to print lines on the console. Instead, you could assign it the short form sop. That way you can just write sop("hello") and it is automatically expanded to System.out.println("hello") on the fly.

To assign abbreviations                  :ab <abbreviation> <actual text>   
Eg. ab sop System.out.println

Note that abbreviations are expanded only if they are followed by a special character. So careful when assigning abbreviations.

Searching and Replacing text
Go to the normal mode, enter :s/<text_to_replace>/<alternate_text>/g
s stands for search
/ (forward slash) is used as a separator
g  is a flag that stands for 'replace for all occurrences' in the current line

Note that this command works for only a single line with cursor
For replacing the entire file the command must start with %
Example    :%s/char/int/g
% is a short hand for the range  you must supply. Look into the man
for details

You can also repeat a replacement action for certain lines
Go to the desired line and hit & to replace with flag options : &&

Indenting Text:
Text can be indented using the following procedure
1. set the filetype   :set filetype=c   (can be anything xml, java)
2. enable indentation  :filetype indent on
3. gg=G    
gg --- go to the start of file
=  auto indent
G  end of file

Folding Text:
1. :set foldmethod=marker
2. select the text in visual mode (V)
3. zf  will create a new fold

open fold zo
close fold zc

Using markers
1. quick marker m' or m`
2  go to marker  '' or ``
3. store marker in a-z    ma..mz
4. inter file marker (for marking in other files) mA...mZ

Key Mappings
Vim allows you to define your own key mappings to perform various tasks. 
If you have been coding using vim for sometime, you would find it frustrating to repeat the pattern of save, compile, execute.
Lets create a shortcut to this procedure

The syntax to define a key mapping is :map <key> <mapping>

In order to map special keys we need to enter it in a special way
Press Ctrl+Shift+k . You'll see that a ? appears. Now enter the special key and it would be converted
I assume that you have created a makefile. 

Hit escape and type :map <F1> :w<Enter> :!make<Enter>

Make sure you <Enter> is written in the special key method mentioned previously. It will appear as ^M. You may also make this entry into your configuration file (see below).

SAVING YOUR CONFIGURATION:
By now you would have realized that your configurations are lost every time you exit Vim. Setting up Vim every time you open it can be annoying. Here comes the use of the .vimrc file. ( Notice it is (DOT)vimrc )
Vim will search for the configuration files in predefined directories upon startup. The directories it looks inside are the users home directory and the root home directory. 

Creating a .vimrc file :
Open a terminal and type vi ~/.vimrc 
Enter the configurations you require, including the abbreviations and all key mappings(will explain later)
Save and close
Restart Vim and voila` !



No comments:

Post a Comment