Showing posts with label vi. Show all posts
Showing posts with label vi. Show all posts

Thursday, June 24, 2010

Prevent vi from opening files for which you do not have read permission

Found an annoying thing in one of the new servers I was playing around. It generated some log files as root and permissions of 600 and others as the designated user. Now the problem is that when I open log files with 600 permission in vi it happily opens up but, shows a blank buffer with 'Permission Denied' error. After a couple of times I got really annoyed and decided to fix it.


Attempt 1: ~/bin is ahead in the path to /usr/bin/vi so, wrote a simple script to check for file permissions.
#!/bin/sh
if [ -e $1 -a ! -r $1 ]; then
  echo No read permission for $1
  exit 1
fi
/usr/bin/vi $1
But, sadly that did not cut it
Attempt 2: Stuffed the above in an alias. I'll leave as an exercise for you to figure out what happens
Attempt 3: Then converted the same logic to a shell function called vi and w00t! it worked :)
vi() {
  if [ -e $1 -a ! -r $1 ]; then
    echo No read permission for $1
  else
    /usr/bin/vi $1
  fi
}

Sunday, October 19, 2008

vi vi vi is the editor of the beast

I'm never satisfied with the text-editors I've been using so far. But, now I may have found _the one_. I started off with the humble MS-DOS edit, moved on to the GUI notepad then tried some serious text editors like EditPad, EditPlus, TextPad, SciteFlash, jEdit, Notepad++ and lots of others. There is always some feature that I really like accompanied with some equally irritating annoyance. Like for e.g. I love the beanshell find/replace in jEdit but, I had to give it up as it would choke for any file a little over 900KB. Editplus was great till I had to move to linux and Editpad was just too clunky.

I've used vi on and off mostly to quickly edit some file on the server to save the hassle of ftping the file back and forth. I never enjoyed these sessions much because I wasn't yet initiated to the magic of vi. Then one day I decided that all this brouhaha about vi and emacs must have some substance to it and hence resolved to use one of them as my primary editor henceforth. Its a tall order to master each of these editors but, since I was already familiar with vi and couldn't bear the ctrl+meta sequences of emacs for too long. For over 2 months now I've uninstalled every other text editor and use only gVim.

Its great. Everything you ever wanted to do with a piece of text can be done in vi. There are a wealth of cheatsheets and tutorials. Here are some of them.

Best of VIM Tips, gVIM's Key Features zzapper
A slightly advanced Introduction to Vim LG #152
Vi Reference Card
100 Vim commands every programmer should know

vi may be the editor of the beast because once you get to know it you start seeing its inner beauty :)