Thursday, January 6, 2011

Vim abbreviation not working - solution


Abbreviations in vi editor are the shortcuts for strings. For example, if you frequently need to write the text "Change review not needed", you can have a shortcut like "crn" for this. So every-time you require to type "Change review not needed", you type crn (then a space) and your abbreviation will be expanded (in insert mode).

With my vi editor version:

VIM - Vi IMproved 7.1 (2007 May 12, compiled Jan 8 2009 02:24:14)

I created an abbreviation on my .vimrc:

$ tail -4 ~/.vimrc
:se shiftwidth=4
:se ts=4
:se paste
:ab crn Change review not needed

This abbreviation ("crn") was not working as expected and later I came to know its because of the "set paste" option set on my .vimrc. Vi abbreviations are disabled if the 'paste' option is on. I removed the ":se paste" from my .vimrc and the above abbreviation worked.

Another alternative to set an abbreviation in vim

:inoremap crn Change review not needed

This also require the 'paste' option to be unset/not set.

To unset an abbreviation in vi editor, type in ex mode:

:una crn

To see what all abbreviations are already set, type in ex mode:

:ab

For help on vi abbreviation type, type in ex mode:

:h iab
:h ab

Sunday, January 2, 2011

Vi editor - directory modification time changes

Why last modified time of a directory changes when any file inside the directory is viewed (i.e. opened in vi editor but not saved) ?
e.g.

#Created a directory named 'testdir'
$ mkdir testdir
$ ls -lrt | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:37 testdir

#Created a file named 'testfile.txt' inside directory 'testdir'
$ cd testdir/
$ touch testfile.txt
$ ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 5 14:38 testfile.txt

#Modification time of the directory 'testdir' is changed to 'Dec 5 14:38' as expected.
$ ls -lrt ../ | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:38 testdir

#Opened the file 'testfile.txt' in Vi editor, but I quit using "!q", so nothing saved.
$ vi testfile.txt

#No Change in modification time of file 'testfile.txt'
$ ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 5 14:38 testfile.txt

#But modification time of the directory 'testdir' is changed.
$ ls -lrt ../ | tail -1
drwxr-xr-x 2 root root 4096 Dec 5 14:40 testdir

Its because when we open a file in vi, a hidden swap file (with .filename.swp extension) is created. The swap file is used for recovery of the vi session in case vi crashes (e.g. when system reboots, suppose you accidentally close your vi session with 'Ctrl q' etc). This swap file is deleted when we save and exit the file in vi editor. The creation of the swap file and later deletion of this swap file on normal vi exit is the cause of the change in modification time of a directory.

© Jadu Saikia http://unstableme.blogspot.com