[Tutor] newbie request for help in setting up emacs for python

Terry Carroll carroll@tjc.com
Mon Dec 30 16:03:26 2002


On 30 Dec 2002, ahimsa wrote:

> I'm totally unfamiliar with that complex beast called emacs (or xemacs)
> and need a guide for dummies on how to configure it so that I can use it
> for a python ide.


Wow, a question I can answer, or at least help on!  I justs started using
both Python and Emacs.

First, if you don't already have it, download the mode from
<http://www.python.org/emacs/python-mode/>.

I had to mess with emacs for quite a bit until I got it working well
enough for me.  Here's my .emacs file now (comments in the file  start
with ";;;"; my commentary (NOT in the file) is interspersed in
[brackets]):

;;; swap C-s & C-q and C-\ and C-^
(enable-flow-control)
[Normally, the CTRL-x CTRL-s sequence is used to save a file.  This screws
up on a lot of systems, which use CTRL-s to stop display and CTRL-q to
restart it.  The above line swaps CTRL-s and CTRL-\, and CTRL-q and
CTRL-^.  So for me, the command to save is now CTRL-x CTRL-\.  This is not
Python-specific, but I thought I'd include it.]

(global-set-key "\C-h" 'delete-backward-char)
(global-set-key "\C-xh" 'help)
;;;

[another non-python thing.  By default, emacs uses CTRL-x CTRL-h to get
help.  Every other system in the world uses CTRL-h for a backspace.  I set
it up so that CTRL-h is a backspace in emacs, and help is obtained with
CTRL-xh]

;;; set python mode:
(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))

[This line means that if you edit a file ending in .py, it goes into
python-mode]

(setq interpreter-mode-alist
      (cons '("python" . python-mode)
            interpreter-mode-alist))

[I'm not sure what this line does, but I found it in an example, it works,
so I don't mess with it.]

(autoload 'python-mode "python-mode" "Python editing mode." t)

[This line, I think, makes the python mode available in the first place.]

;;; configure python mode
;;; add these lines if you like color-based syntax highlighting
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
(require 'paren)
(show-paren-mode 1)
[The above lines are the ones that make the bells and whistle of
python-mode work.  It's useful to show parenthesis matching, which words
are keywords compared to variables, etc. -- that sort of thing.]

> Can someone point me in the right direction or specify
> some tips on how to do it. I've tried reading the documentation at both
> the python as well as the GNU emacs/Xemacs sites and find it more
> confusing than enlightening ... which is probably not a very good
> prognosis :-)

No, I'm right with you there.  emacs is a hell of a lot more easy to use
than to configure, at least for a beginner.  I got frustrated trying to
configure it based on what I found on web pages, FAQs and help files.  I
ended up going down to the library and borrowing the O'Reilley emacs text,
"Learning GNU Emacs."  I spend an hour or so with it and my .emacs file,
and got it configured in a way that worked for me.  I particularly
recommend chapter 11, "Customizing Emacs".

-- 
Terry Carroll        |
Santa Clara, CA      |   "The parties are advised to chill."
carroll@tjc.com      |       - Mattel, Inc. v. MCA Records, Inc.,
Modell delendus est  |         no. 98-56577 (9th Cir. July 24, 2002)