Re: how to indent/dedent a region in emacs?

Discussion in 'Python' started by Alexander Schmolck, Jun 11, 2008.

  1. Grant Edwards <> 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
     
    Alexander Schmolck, Jun 11, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dt
    Replies:
    4
    Views:
    528
    CBFalconer
    Dec 31, 2006
  2. SAL

    #Region #End Region issue

    SAL, Aug 29, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    Alexey Smirnov
    Aug 29, 2008
  3. Rustom Mody

    reifying indent and dedent into braces

    Rustom Mody, Oct 13, 2009, in forum: Python
    Replies:
    4
    Views:
    362
  4. David Combs
    Replies:
    0
    Views:
    208
    David Combs
    Apr 4, 2006
  5. David Combs
    Replies:
    0
    Views:
    173
    David Combs
    Jan 6, 2007
Loading...

Share This Page