Skip to content
sluedtke edited this page Sep 24, 2013 · 10 revisions

PYTHON GRASS plugin for (G)VIM

Installation

This is a simple filetype plugin. So either merge the content of the ""ftplugin"" folder with your $HOME/.vim/ftplugin folder, use PATHOGEN or VUNDLE. I highly recommed the last one, it is just great!!

You need the screen-plugin and vim compiled with python support.

Features

After start editing a pyhton script that is supossed to run GRASS commands you might want to test parts of your script without copy/paste between your favorite text editor and a running GRASS session. With this plugin, you can send single lines or visually selected text to a running GRASS session.

Options

####1- GRASS SESSION By pressing F2, the plugin will search for all lines that match the strings that are listed in the variable python_grass_pattlist. My .vimrc defines that variable as:

let g:python_grass_pattlist=["gisdbase=","location=","mapset="]

NOTE: If this variable is not set in your .vimrc, the plugin will use "gisdbase=", "location=" and "mapset=" as a default. The order matters of course.

People working with GRASS will recognize them as variables necessary to start a GRASS session and immediately running commands. The strings in the list are evaluated separately through python and the modules sys and os are available at this stage by default. For example, a script containing the lines

gisdbase=os.path.join(os.environ['HOME'], "grassdata")
location="nc_spm_08"
mapset="PERMANENT"

and using the variable python_grass_pattlist as stated before, F2 will launch GRASS in a terminal as:

grass --text $HOME/grassdata/nc_spm_08/PERMANENT

and ipython right after that.

At this point, one might send commands via different key strokes to the GRASS session. At the moment these are:

  • <localleader> l to send the current line under the cursor
  • <localleader> ss to send selected parts
  • <localleader> rq to quit the GRASS session

Other option by now are:

  1. If you want to start the GRASS session with a GUI (namely wxpython), set the variable python_grass_gui to 1. My .vimrc says let g:python_grass_gui=0, what is the default if nothing is set and if we run vim and not gvim.

  2. If the variable python_grass_import is set to 1, the following lines will be send to the terminal after ipython was started.

import os
import sys
import grass.script as grass
import grass.script.setup as gsetup

If the variable is set to 0, nothing is imported. Might be a good idea to change this in a way that a user specific statement or file will be loaded.

####2- IPYTHON SESSION

Pressing F3 will start just a simple ipython shell providing the same mappings like the GRASS session.

###BUGS

  • Fix error messages at start-up and quit.
  • Each of the screen calls (F2/F3) can only be executed once, after that, the file has to be closed in order to get another screen session.
  • If a ".gislock" file was found when GRASS starts, it will fail, but ipython will be started anyway.