Last modified May 24, 2012, 1:14 a.m.
Getting emacs right when it comes to making it Django dev friendly can be fiddly. Here's how I did it.
I did this on Debian 7 "wheezy", so your mileage may vary. I would imagine you will have similar results on a recent version of Ubuntu.
Step 1 - Install and configure django html syntax highlighting
First, get the files in place
cd ~/.emacs.d
wget http://ourcomments.org/Emacs/DL/elisp/nxhtml/zip/nxhtml-2.08-100425.zip
unzip nxhtml-2.08-100425.zip
Next, edit your .emacs file. I've used vim as the editor here but feel free to use what you're most comfortable with. Even emacs will do!
Add the following lines of code to the end of the file.
;; ---------------------------------------------------------------------------
;; For django html
(autoload 'django-html-mumamo-mode "~/.emacs.d/nxhtml/autostart.el")
(setq auto-mode-alist
(append '(("\\.html?$" . django-html-mumamo-mode)) auto-mode-alist))
(setq mumamo-background-colors nil)
(add-to-list 'auto-mode-alist '("\\.html$" . django-html-mumamo-mode))
Try running emacs. If you get the following error:
Warning: 'font-lock-beginning-of-syntax-function' is an obsolete variable (as
of Emacs 23.3); use 'syntax-begin-function' instead.
Run this from the command line:
replace font-lock-beginning-of-syntax-function syntax-begin-function -- .emacs.d/nxhtml/util/mumamo.el
Step 2 - Configure a colour scheme
I generally work in a terminal with white text on a black background so my theme choice was color-theme-hober. After the colour scheme is installed there is a ton to chose from. From within emacs META-x, then type "color-theme-". Tap the tab key a couple of times to see all the options.
If you're using Ubuntu/Debian the colour theme should be an installable package via apt. I took most of these steps from this resource here.
If you can't use apt, you can download the colour themes from here. Otherwise run:
sudo apt-get install emacs-goodies-el
From a terminal (once again you can use your editor of choice here).
Add the lines:
;; ---------------------------------------------------------------------------
;; Color-theme
(add-to-list 'load-path "/usr/share/emacs/site-lisp/emacs-goodies-el/color-theme.el")
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-hober)))
Replace "color-theme-hober" with another option if you prefer a different colour scheme.
Step 3 - Add an 80 character column marker
This step is optional but it's a nice way to get emacs to tell you that you're going over the standard 80 character terminal width.
Taken from instructions here.
Install the colomn-marker feature by running the following from a terminal:
cd ~/.emacs.d
wget http://www.emacswiki.org/emacs/download/column-marker.el
Once again, edit your .emacs file and add the following lines to the bottom:
;; ---------------------------------------------------------------------------
;; Column Marker
(require 'column-marker)
(add-hook 'python-mode-hook (lambda () (interactive) (column-marker-1 79)))
Note that the column-marker-1 value is 79 not 80. This is because emacs will wrap one character early if a line reaches 80 characters.
Run emacs again. If you get loading issues add the following to the top of your .emacs file:
(add-to-list 'load-path "~/.emacs.d/")
Step 4 - Additional configurations
Below are some more configurations you can add to emacs that I find rather handy.
Add any of these code snippets to the end of your .emacs file
For adding the shortcut key combination META-g to bring up the goto-line feature:
;; Set goto line
(global-set-key "\M-g" 'goto-line)
Ensure files when saved end with a newline character:
;; Always end a file with a newline
(setq require-final-newline t)
Switch off the menu bar I find pretty useless and free up another line for text!
;; Turn off menu bar
(menu-bar-mode -1)
For posterity - here's my whole .emacs file with all configs in one:
(add-to-list 'load-path "~/.emacs.d/")
;; ---------------------------------------------------------------------------
;; Column Marker
(require 'column-marker)
(add-hook 'python-mode-hook (lambda () (interactive) (column-marker-1 79)))
;; ---------------------------------------------------------------------------
;; For django html
(autoload 'django-html-mumamo-mode "~/.emacs.d/nxhtml/autostart.el")
(setq auto-mode-alist
(append '(("\\.html?$" . django-html-mumamo-mode)) auto-mode-alist))
(setq mumamo-background-colors nil)
(add-to-list 'auto-mode-alist '("\\.html$" . django-html-mumamo-mode))
;; ---------------------------------------------------------------------------
;; Color-theme
(add-to-list 'load-path "/usr/share/emacs/site-lisp/emacs-goodies-el/color-theme.el")
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(color-theme-hober)))
;; Set goto line
(global-set-key "\M-g" 'goto-line)
;; Always end a file with a newline
(setq require-final-newline t)
;; Turn off menu bar
(menu-bar-mode -1)