how to indent/dedent a region in emacs?

Alexander Schmolck a.schmolck at gmail.com
Wed Jun 11 18:49:53 EDT 2008


Grant Edwards <grante at visi.com> writes:

> I've recently switched from Jed to Emacs for editing python
> source, and I'm still stumped as to how one indents or dedents
> a region of code.  In Jed it's 'C-c <' or 'C-c >'.  Google has
> found several answers, but none of them work, for example I've
> tried bot "C-c tab" and "C-c C-r" based on postings Google has
> found, but neither appears to actually _do_ anyting.

In python-mode C-c > and C-c <  should work as well.

(note that you have to download python-mode.el; I don't know if the above also
works with python.el which comes with emacs; C-h m will tell you which mode
you're using)

Outside python-mode, you can use string-rectangle and kill-rectangle, but
that's inconvenient, so I've globally bound the below functions to these keys
(you can just use the plain py-shift-region{-left,right} if you like them
better).


(defun my-py-shift-region-left (start end &optional count)
  "Like `py-shift-region-left', but COUNT applies the command repeatedly,
  instead of specifying the columns to shift."
  (interactive
   (let ((p (point))
    (m (mark))
     (arg (* py-indent-offset (or current-prefix-arg 1))))
     (if m
      (list (min p m) (max p m) arg)
       (list p (save-excursion (forward-line 1) (point)) arg))))
  ;; if any line is at column zero, don't shift the region
  (save-excursion
    (goto-char start)
    (while (< (point) end)
      (back-to-indentation)
      (when (and (< (current-column) count)
                 (not (looking-at "\\s *$")))
        (error "Not enough left margin"))
      (forward-line 1)))
  (py-shift-region start end (- (prefix-numeric-value
                              (or count))))
  (py-keep-region-active))


(defun my-py-shift-region-right (start end &optional count)
  "Like `py-shift-region-right', but COUNT applies the command repeatedly,
  instead of specifying the columns to shift." 
  (interactive
   (let ((p (point))
    (m (mark))
     (arg current-prefix-arg))
     (if m
      (list (min p m) (max p m) arg)
       (list p (save-excursion (forward-line 1) (point)) arg))))
  (py-shift-region start end (prefix-numeric-value
                               (or count py-indent-offset)))
  (py-keep-region-active))
  

'as



More information about the Python-list mailing list