schlitt.info - php, photography and private stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Tobias Schlitt :Date: Wed, 19 Nov 2008 23:29:46 +0100 :Revision: 1 :Copyright: CC by-nc-sa ================================== WI - [W]riteable checker for v[I]m ================================== :Description: 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. `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. .. __: http://www.westhoffswelt.de/ .. __: http://www.phpugdo.de .. __: http://vim.org :: #!/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`__! ;) .. __: http://www.vim.org/scripts/script.php?script_id=1355 .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79 Trackbacks ========== Comments ======== - Aaron Wormus at Wed, 23 Nov 2005 16:21:50 +0100 cool - Pablo at Wed, 18 Jan 2006 01:06:52 +0100 I tried using this script on OSX 10.4.4 and I get the following error upon trying to execute it
 /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" ]]' 
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 at Wed, 18 Jan 2006 07:51:14 +0100 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? - Pablo at Wed, 18 Jan 2006 10:55:50 +0100 I'm running GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0) - Toby at Wed, 18 Jan 2006 11:07:11 +0100 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: http://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 at Wed, 18 Jan 2006 11:58:50 +0100 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.
 #!/bin/bash
  ################################################################################
  # (w)ritable checker for v(i)m # (c) 2005 - Jakob Westhoff
  
  ################################################################################

  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
  
- Toby at Wed, 18 Jan 2006 12:56:54 +0100 This is kinda wiered. I thought that regex support is an essencial part of the bash. Thanks for adding this hint here!