Thursday, September 2, 2010

Emacs with Django/Python

The indent-region command is useful for reformatting....Here are some short-cuts when doing Python/Django editing within Emacs:

Ctrl-Alt-a - goes to the beginning of the class
Ctrl-Alt-e - goes to the end of the class
Ctrl-c + > - shift region right
Ctrl-c + < - shift region left
Ctrl-c + Ctrl-h - lists all the related Python keys
Ctrl-x + 2 - split screen horizontally
Ctrl-x + 3 - split screen vertically
Ctrl-x + Ctrl-o - switch between windows
Alt-x + ansi-term - goes into full ANSI terminal shell
Ctrl-Alt-\ - reindent the current region (useful for fixing up blocks)

Things to add into your .emacs file:
  • Uniqify - makes it less confusing what files you're actually viewing within various Django dirs (i.e. views.py<1> becomes mymodule/views.py)

    The key is to use the 'forward' naming scheme to put part of the directory at the beginning of the buffer name.
  • PyFlakes - Shows you unused imports, mistakes on the-fly, etc. See Stack Overflow discussion at: http://stackoverflow.com/questions/1257236/django-emacs-as-textmate-replacement
  • Yasnippet - Useful for Django template editing.  You need to install Yasnippet (use the normal install instructions) before using the Django HTML snippets code here:
    mkdir ~/.emacs.d/plugins
    cd ~/.emacs.d/plugins
    wget http://code.google.com/p/yasnippet/downloads/detail?name=yasnippet-0.6.1c.tar.bz2&can=2&q=
    tar xvjf yasnippet-0.6.1c.tar.bz2
    
    apt-get install bzr
    
    cd /tmp
    bzr branch lp:python-mode
    bzr branch lp:django-mode
    
    mv /tmp/python-mode/python-mode.el ~/.emacs.d
    mv /tmp/django-mode/*.el ~/.emacs.d
    mv /tmp/django-mode/snippets ~/.emacs.d/plugins
    

My .emacs file looks like:
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)

(when (load "flymake" t) 
(defun flymake-pyflakes-init () 
(let* ((temp-file (flymake-init-create-temp-buffer-copy 
'flymake-create-temp-inplace)) 
(local-file (file-relative-name 
temp-file 
(file-name-directory buffer-file-name)))) 
(list "pyflakes" (list local-file)))) 
(add-to-list 'flymake-allowed-file-name-masks 
'("\\.py\\'" flymake-pyflakes-init)))
(add-hook 'python-mode-hook 'flymake-mode)

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-0.6.1c")
(add-to-list 'load-path "~/.emacs.d")
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
(require 'django-html-mode)
(require 'django-mode)
(yas/load-directory "~/.emacs.d/plugins/django-mode/snippets")
The key is that I have to unpack the yasnippet code first. The django-mode yasnippets also depend on python-mode.el, so I have to install it. Note: there is a difference between python.el (which comes with the native Python install) and python-mode.el, which has to be downloaded separately.

One extra change was to modify django-html-mode.el to recognize .HTML files by default as Django/HTML files:

(add-to-list 'auto-mode-alist '("\\.html$" . django-html-mode))

No comments:

Post a Comment