lunes, 4 de julio de 2011

Autocompletado de Django en VIM

Para poder usar el autocompletado de VIM en projectos Django es necearios tener estas lineas en nuestro archivo .vimrc


syntax on

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS


Y con esto listo, copiar este script que podemos colocar en alguna dirección dentro del path como lo es /usr/local/bin o crear un directorio para el path local en el home.


#!/bin/bash

# Name: djvim
# Description: Script to automate the setup of the
# enviroment vars to use omicomplete in vim
# for Django projects.
# Usage: djvim filename
#
# Author: Jesús Manuel Mager Hois
# Copyright: GPL v3 or later


#Set the global vars to the Django project
function startvim(){
local arr=$(echo $1 | tr "/" "\n")
for x in $arr
do
#echo "[$x]"
local PROJECT=$x
done
echo $PROJECT
cd ..
local DIR=`pwd`
export PYTHONPATH=$PYTHONPATH:$DIR
export DJANGO_SETTINGS_MODULE=$PROJECT.settings
vim $ARGS
}

#Iter the directory tree to find setting.py
function finddjbase(){
cd ..
local DIRF=`pwd`
local FILEF=`echo "$DIRF/settings.py"`
if [ $DIRF == '/' ]
then
echo "Couldn't find the Django base directory"
exit
fi
echo "Trying in $DIRF"
if ( ! fileexists $FILEF )
then
finddjbase
else
startvim "$DIRF"
exit
fi
}

#Check if we can find setting.py
function fileexists(){
local f="$1"
if [ ! -f $f ]
then
echo "This is not the Django base directory... trying"
return 1
else
echo "settings.py FOUND at $1"
return 0
fi
}

#Main block

ARGS=$@
DIRN=`pwd`
FILEN=`echo "$DIRN/settings.py"`

if ( ! fileexists $FILEN )
then
finddjbase
else
startvim $DIRN
exit
fi



Y listo, podemos abrir nuestro archivo con fjvim nombredelarchivio.py