Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.
I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am using Emacs on the command line(Ubuntu), not as a gui, if that makes any difference.
I use the following snippet. On tab when the selection is inactive, it indents the current line (as it normally does); when the selection is inactive, it indents the whole region to the right.
I'm an Emacs newb, so this answer it probably bordering on useless.
None of the answers mentioned so far cover re-indentation of literals like
dict
orlist
. E.g.M-x indent-region
orM-x python-indent-shift-right
and company aren't going to help if you've cut-and-pasted the following literal and need it to be re-indented sensibly:It feels like
M-x indent-region
should do something sensibly inpython-mode
, but that's not (yet) the case.For the specific case where your literals are bracketed, using TAB on the lines in question gets what you want (because whitespace doesn't play a role).
So what I've been doing in such cases is quickly recording a keyboard macro like
<f3> C-n TAB <f4>
as in F3, Ctrl-n (or down arrow), TAB, F4, and then using F4 repeatedly to apply the macro can save a couple of keystrokes. Or you can doC-u 10 C-x e
to apply it 10 times.(I know it doesn't sound like much, but try re-indenting 100 lines of garbage literal without missing down-arrow, and then having to go up 5 lines and repeat things ;) ).
I've been using this function to handle my indenting and unindenting:
And then I assign it to a keyboard shortcut:
If you are programming Python using Emacs, then you should probably be using python-mode. With python-mode, after marking the block of code,
C-c >
orC-c C-l
shifts the region 4 spaces to the rightC-c <
orC-c C-r
shifts the region 4 spaces to the leftIf you need to shift code by two levels of indention, or some arbitary amount you can prefix the command with an argument:
C-u 8 C-c >
shifts the region 8 spaces to the rightC-u 8 C-c <
shifts the region 8 spaces to the leftAnother alternative is to use
M-x indent-rigidly
which is bound toC-x TAB
:C-u 8 C-x TAB
shifts the region 8 spaces to the rightC-u -8 C-x TAB
shifts the region 8 spaces to the leftAlso useful are the rectangle commands that operate on rectangles of text instead of lines of text.
For example, after marking a rectangular region,
C-x r o
inserts blank space to fill the rectangular region (effectively shifting code to the right)C-x r k
kills the rectangular region (effectively shifting code to the left)C-x r t
prompts for a string to replace the rectangle with. EnteringC-u 8 <space>
will then enter 8 spaces.PS. With Ubuntu, to make python-mode the default mode for all .py files, simply install the
python-mode
package.In addition to
indent-region
, which is mapped toC-M-\
by default, the rectangle edit commands are very useful for Python. Mark a region as normal, then:C-x r t
(string-rectangle
): will prompt you for characters you'd like to insert into each line; great for inserting a certain number of spacesC-x r k
(kill-rectangle
): remove a rectangle region; great for removing indentationYou can also
C-x r y
(yank-rectangle
), but that's only rarely useful.indent-region
mapped toC-M-\
should do the trick.