Jakob Westhoff (one of my usergroup fellows) created a tiny but very efficient bash script to avoid a big annoyance: I'm using "sudo" on my work station to not have the need to completly switch to root for administrative tasks. Still I usually forget to type "sudo" when I intend to edit a file with is not writeable for my current user. I asume people with similar environments know that problem. WI ([W]riteable checker for v[I]m) wraps around VIM on startup and checks the file permissions. If you may not edit the file, WI asks you if you want to start VI through sudo or quit.
#!/bin/sh
VIM=/usr/bin/vim
SUDO=/usr/bin/sudo
if [ -e $1 ] && [ ! -w $1 ]
then
read -er -p "The file $1 is not writable. Do you want to edit it as root? [yes/NO] " answer
if [[ "$answer" =~ "y|yes|yeah|j|ja" ]]
then
$SUDO $VIM $*
fi
else
$VIM $*
fi
Really nice. Thanks Jakob!
P.S.: Don't forget to install phpDocumentor for VIM! ;)
If you liked this blog post or learned something, please consider using flattr to contribute back: .
Fields with bold names are mandatory.
Aaron Wormus
cool
Link to commentPablo
I tried using this script on OSX 10.4.4 and I get the following error upon trying to execute it
Link to comment<pre>
/usr/bin/wi: line 8: conditional binary operator expected
/usr/bin/wi: line 8: syntax error near `=~'
/usr/bin/wi: line 8: ` if [[ "$answer" =~ "y|yes|yeah|j|ja" ]]'
</pre>
I tried removing the '~' from after the equals sign and the code executes but does nothing after I select yes. Any idea why it might be doing that? I'm running GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0)
Toby
I've no idea what is wrong there. The =~ indicates a regex match and the regex is quite correct. Which bash version are you using?
Link to commentPablo
I'm running GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0)
Link to commentToby
That should usually work. At least, the script works fine for me, here. I have uploaded it to my website to ensure, that there is not something stripped from it by my blog software:
Link to commenthttp://schlitt.info/misc/wi.sh
Please try out this version. If this still does not help, I have no clue what's going wrong. Sorry. :( Maybe you should try a place where more experienced shell-scripters hand around.
Would be nice, if you can inform me if and how you made it finally work!
Pablo
Well, I got it to work, what I had to do was remove the regexp entirely. I can only type 'y' for the script to execute properly but that shouldn't be a problem. Thanks for this script, very useful! Don't know why I didn't think of it before =) Here is what my script looks like now in case you are curious. I commented the lines that I changed from the original.
Link to comment<pre>
#!/bin/bash
################################################################################
# (w)ritable checker for v(i)m
# (c) 2005 - Jakob Westhoff <jakob@westhoffswelt.de>
################################################################################
VIM=/usr/bin/vim # This is where my vim binary is located
SUDO=/usr/bin/sudo
if [ -e $1 ] && [ ! -w $1 ]
then
read -er -p "The file $1 is not writable. Edit it as root? [yes/NO] " answer # This line was to long for my default terminal size, so I shortened it
if [[ "$answer" = "y" ]] # had to remove regex and make it a static comp
arison
then
$SUDO $VIM $*
fi
else
$VIM $*
fi
</pre>
Toby
This is kinda wiered. I thought that regex support is an essencial part of the bash. Thanks for adding this hint here!
Link to comment