Mac – emacs: how to indent/unindent region of python code by 4 spaces

emacsindentationkeyboard shortcutspythontext editing

I have a region of python code selected in emacs. What are the most efficient ways to indent and unindent every line in it by 4 spaces?

Ideally I'd like something that's not context sensitive (eg. it'll allow me to indent passages within docstrings where the indentation does not relate to python syntax).

My mode line shows (Python). Searching the web I found a lot of discussion of using python-mode instead of python, but I only started using emacs a few days ago so I'm wary of changing defaults before I have had a chance to understand and them. If changing to python-mode is the best option I'm happy to accept that as an answer.

What I've tried (from the manual):

  • <TAB> no effect
  • C-M-\ no effect
  • C-x <TAB> (indent-rigidly) indent every line by one column (and deselects the region)
  • M-i indents one first line by a tab (length of 8 spaces)
  • C-M-q – "C-M-q is undefined"

The only way I've found is using regex-replace, but that's an awful number of keystrokes for such a simple task.

Best Answer

Assuming that the variable python-indent is 4:

M-x python-shift-right (C-c >)
M-x python-shift-left  (C-c <)
Related Question