[BangPypers] BangPypers Digest, Vol 8, Issue 15
Anand Balachandran Pillai
abpillai at gmail.com
Mon May 5 13:08:58 CEST 2008
All right. Since vimmers are on a roll, Emacs-ians cannot be left behind ;)
Here are the customizations for emacs.
First you need "python-mode" for editing Python code. Get it from
http://sourceforge.net/projects/python-mode/ and put the python-mode.el
file in some directory which is part of your Emacs "load-path" variable.
Now, in your .emacs file, add the following lines
;; These make sure that when you open a Python file
;; the python-mode is loaded automatically
(add-to-list 'auto-mode-alist '("\\.py$" . python-mode))
(add-to-list 'auto-mode-alist '("\\.pyw$" . python-mode))
;; A set of prog-modes which require tabs, example, Makefiles.
(setq tabify-modes '(makefile-gmake-mode))
;; Convert tabs to spaces and saves the buffer
(if (not (member major-mode tabify-modes))
(untabify (point-min) (point-max)))
Bind the function "untabify-save-buffer" to some convenient key.
I have bound it to F2. Then press the short-cut key everytime to
save an edited buffer to disk.
(global-set-key [f2] 'untabify-save-buffer)
That's it. Keep using tabs to format your Python source code
and at the end hit F2 to save the buffer and emacs takes care
of the rest.
As an added customization, you could make sure you remove
any additional Ctrl-Ms which are seen when you open a file edited
in DOS/Windows in Unix.
;; Remove all Ctrl-Ms from a region
(defun ^m-region (min max)
"Remove all ^M's from the region."
(while (re-search-backward "\C-m$" min t)
;; Remove all Ctrl-Ms from a buffer
(defun ^m-buffer ()
"Remove all ^M's from the buffer."
(^m-region (point-min) (point-max)))
Then you could define an umbrella function which performs both
Ctrl-M removal and untabification in one, something like,
And bind it to a global key.
(global-set-key [f2] '^m-untabify-save-buffer)
Ah, the beauties of Elisp...
On Mon, May 5, 2008 at 4:27 PM, Kiran Jonnalagadda <jace at pobox.com> wrote:
> On 02-May-08, at 11:46 AM, Biju Chacko wrote:
> > If you are a vim user you can add:
> > set expandtabs
> > to your .vimrc to ensure that vim indents with only spaces.
> I'd recommend being more elaborate:
> set et " Expand tabs
> set ai " Auto-indent
> set sw=4 " Indent to 4 spaces
> set ts=8 " Treat tabs as 8 columns
> set si " Smart indent
> set sta " Smart tab key handling
> syn on " Make syntax visible
> " Make tabs visible if you're running gvim
> if has("gui_running")
> set lcs=tab:»·,trail:·,extends:»,precedes:«
> BangPypers mailing list
> BangPypers at python.org
More information about the BangPypers