Sunday, February 5, 2012

A better way to lauch code from vim or a text editor

0 comments
In a previous post I wrote about a simple vim map to launch the code I'm currently working on in an editor session without leaving the current file or without opening another window or tab in your terminal emulator. (See http://sphyrnalewini.blogspot.com/2012/02/another-nice-vim-trick-for-multi.html)

As I add plugins and shortcuts to vim and add the need to setup more complex environments to run my scripts I find that using the map on multiple keys doesn't work anymore. Plugins consume FKey mappings and sometimes you need to have path and config variables set in order to run your code.

To handle this I've written a simple shell script that takes the output of the same vim mapping syntax

:w |!interpreter_name %

And lets you run all of the interpreters from the one script. This reduces the number of key combinations you have to use for programs and also lets you do more with the environment around the script.

#!/bin/bash
# Make sure we have at least one argument
if [[ -z $1 ]]
then
 exit 1
fi

# Save our script name
script=$1

# Get the extension
extension=$(echo $1 | sed 's/\(.*\)\.//' )

# Remove the first argument
shift

# Create the argument string
args=$@

# Go through our list of extensions and
# call the appropriate interpreter.
# We can also add command line args to
# the interpreter or do redirect statements.
case $extension in
 py)
  # Add an environment varialbe for the  script.
  export FOO='YOUFOUNDFOO'
  /usr/bin/python $script $args
  ;;
 php)
  /usr/bin/php $script $args
  ;;
 rb)
  /usr/bin/ruby $script $args
  ;;
 pl)
  /usr/bin/perl $script $args
  ;;
 sh)
  /bin/bash $script $args
  ;;
 *)
  echo OOOPS $script $args
  exit 1
  ;;
esac

Notice in the python case the environment variable 'FOO'. This could be simple things like paths or maybe linking strings or paths to python resources you need for this script or it could be as complex as setting up a full Django environment and logging into and out of virtualenv.

Then if you run the following simple python script:

import os

print os.getenv('FOO')
print 'foobar'

You'll see the value of the environment variable 'FOO' 'YOUFOUNDFOO'.

A more practical use might be to include a remote system path that has resources the script needs like testing code for pytest or nose as in the example below.

# in runme shell script
py)
  export RESOURCEDIR="/home/dev/libs/python/mylibdir"

Then in your python script:

import sys
import os
sys.path.append(os.getenv('RESOURCEDIR'))

Remember that you can also use special command line args from vim to add these as well, because your python script can simply ignore any positional args. They would be strictly for use in the shell script.

Using the practice outlined with this script it would be easy enough to create more complex handling of individual scripts or change the behavior based on what the current directory is that you are working in.

All the best.

Comments

0 comments to "A better way to lauch code from vim or a text editor"