Sunday, August 7, 2011

Explore VIM


"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.”
– Abraham Lincoln
I start the post with an interesting note. We must have our tools ready before we start our work, it makes our work more fun otherwise it becomes a pain. A similar case is with the programmers. They spend most of there time in front of the terminal either writing new code or refactoring the old code. Having an environment customized as per one's need can make a huge difference.

Vim is one of the pieces of software that I use almost everyday. I have been a VIM user since the college days. I started with vi, When I started using it I was a bit annoyed because I had to remember all those commands to browse through the file, changing the modes to move from one line to another was a pain. But when I first used VIM, I was like I have been waiting for something like this because it had all the features that were provided by vi and it had many more. It was definitely more powerful than vi. In this post I will share some of the features which I personally have found to be very useful.
  • In VIM searching a word in the given file is achieved  by typing "/" in the command mode. But adding the following lines to your .vimrc file can make a huge difference.

    To move the cursor to the matched string while typing the search pattern, set
    set incsearch
     With 'ignorecase', searching is not case sensitive:
    set ignorecase
     If 'ignorecase' is on, you may also want:
    set smartcase
    When 'ignorecase' and 'smartcase' are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, "/The" would find only "The", while "/the" would find "the" or "The" etc.

    We can also search for a word in the file by placing the cursor on the word to be searched and pressing "*", this will take you to the next instance of the word in the file.
    Press "n" to move to the next instance of the searched word and "N" to goto the previous instance of the searched string.
  • Searching and replacing is one of the very common task. One can achieve this in VIM by typing
    :%s/search-string/replace-string/
    This will search for "search-string" and replace only the first matched instance of that string with "replace-string. To replace all the instances of the "search-string" add "g" at the end of the command.
    :%s/search-string/replace-string/g
    To search and replace only in few lines, provide the range of lines in the command. To do a find and replace only in lines 50-100, run
    :50,100s/search-string/replace-string/g
    To delete "search-string", donot pass any parameter in the replace-string, it will delete the "search-string". To delete all the "search-string" in lines 50-100, run
    :50,100s/search-string//g
  • Type "gf" in command mode to open the file containing the word the cursor currently points to.
  • Most of the time while browsing the code we feel the need to have more than one file opened in the terminal, either we open each file in a seperate terminal or close the current file and open the other. But VIM has tabs. Yes, it provides tabs just the way the browser's provide tabs. To open a file in the new VIM tab, type
    :tabedit file_name


    Use "ctrl+alt+pgup/pgdn" to traverse between the VIM tabs.

    Type :help tabedit in the command mode to explore more.
  • You use some words frequently in your document and wish there was a way that it could be quickly filled in the next time you use the  same word, VIM provides autofill feature. Press
    ctrl+n
    to achieve this.


  • To add line numbers to the file type
    :set number
















  • Good programming is all about making the code more readable, one of the ways  of achieving it is through indenting the code. Most of the time coping a piece of code from one block to another breaks this, VIM provides a way to perform some actions on the selected block. Press shift+v to select a block, as shown in the figure below:
     
Selecting a block with shift+v



       
          Once a block is selected we can
              *  Move the block 8 spaces to the left by pressing shift + < or 8 spaces to the right by pressing
                  shift + > so as to indent the code
              *  Copy the block by pressing "y" and then pasting it somewhere else by pressing "p".
              *  Search and replace a word only in the selected block, press
                  ":'<,'>s/search_string/replace_string/g"


        Or we can select column's by pressing "ctrl + v" as shown in the figure below.

         
Selecting a Column by pressing cntl + v














  • This is my favorite, VIM allows to split the page horizontally and vertically. This is of great use when we want to view two different files at a time. To split the file vertically press :vsp in the command mode.

    Vertical split















              Press ":sp" to split the screen horizontally. Press ":help vsp" to explore more about how to
              switch between the window etc.
    • Upper case and Lower case: VIM provides an easy way to change the case of the line to UPPER case or LOWER case. Here is the command,

                gUU or guu 

      Place the cursor on the line which you want to convert the case and in the command mode type these letters. gUU will convert the text to upper case and guu will convert the text to lower case.

      You can also flip the case of a set of character with the help of "~" command. To explore more on this type

              :help gUU
    • To open files located in other directories, you can use the full or relative paths such as 
    :e file name 
    • redo and undo: In Vim to undo the last command press

                undo- u

      and to redo the last command press

                ctrl+r

      in command mode
    • When you copy some code from the browser or other editors like gedit and paste it into Vim, you may loose the alignment. To keep the alignment intact, type

                :set paste

       and then paste the code. To change back to normal mode, type

                :set nopaste

      in command mode.
    • To find more help on Vim type ":help" command mode.
    • Find below a few other options that can be set in your .vimrc

                * set scrolloff=100
                * set cursorline
                * set autoindent " always set autoindenting on
                * set backup " keep a backup file
                * set backupdir=~/bkp/vim
                * set history=50 " keep 50 lines of command line history
                * set background=dark

    Some useful links:
    [1]  http://vim.wikia.com/wiki/Vim_Tips_Wiki
    [2]  https://github.com/mja054/Random-tweeks/blob/master/vimrc - to find a sample vimrc
    [3] http://dan.hersam.com/docs/vim.html
    [4] http://vimdoc.sourceforge.net/htmldoc/options.html#'shiftwidth'