Thursday, April 2, 2009

Customise vi/vim - I

Correcting spelling mistakes automatically
------------------------------------------------
You can customise vi/vim according to your requirement using the ~/.vimrc file. You can even make vim learn the spellings of words also.

Suppose, you identified that usually commint some typoes ( teh for the, for example). We will see how to teach vi/vim to correct these mistakes automatically.
Open the file ~/.virmrc file and key in the contents as below and save the file.

iab teh the

The syntax is as follows.
ia incorrect_word correct_word

A plug-in for vim is available from http://www.vim.org/ , which is actually a collection of common mistakes we make, it is quite big repository which we can make use of.

Installing the autocorrect.tar plug-in
------------------------------------------
wget -O autocorrect.tar http://www.vim.org/scripts/download_script.php?src_id=9900

$ tar xf autocorrect.tar
Edit the ~/.vimrc file and key in the contents as below
$ vi ~/.vimrc
:source ~/autocorrect.vim

Note: If you dont want the auto correction permenently, you can add the source while in a vim session as below
:source

If you want to temporarly stop abbreviation for a word, you can use ' : uab teh' while in vim
:uab teh


Tuesday, March 31, 2009

Directory stack

Navigating directories using dirs, pushd and popd


Stack is a temporary data storage based on LIFO. The last item you push to stack, will be the first item you pop out.
we need pushd, popd and dirs to play around with stack
pushd: pushes or adds the directory to the directory stack
popd: pops or removes the directory from the directory stack (last in first out LIFO)
dirs: displays the contents of the stack
Directory stack can be really usful if you need to navigate to some directories which is very lengthy and you need to do it very often.


/tmp/very/long/directory/structure/that/is/too/deep
/tmp/hard/to/navigate/this/directory/structure
/some/structure/which/i/donot/prefer/navigating/often


Let us take the sample directory structures given above.
One way to make the navigation easy if you have to do it requenty is to set aliases, as given below
$ alias godeep="cd /tmp/very/long/directory/structure/that/is/too/deep"
OR, you can make use of the the directory stack.

The dirs command list the contents of the directory stack. The first entry it shows is not the contents, but the current directory. In this case the stack is empty.

$ cd /tmp/very/long/directory/structure/that/is/too/deep
$ pushd .

$ cd /some/structure/which/i/donot/prefer/navigating/often

$ pushd .

$ cd /some/structure/which/i/donot/prefer/navigating/often

$ pushd .

The out put of the dirs command will be as follows,






If you want to navigate to the third directory issue the following command
$ cd `dirs +3`

To remove the element from the stack issue the popd command as follows
$ popd