The Computer Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands

--

Chapters
00:00 Emacs: How To Indent/Unindent Region Of Python Code By 4 Spaces?
01:13 Accepted Answer Score 76
01:24 Answer 2 Score 19
01:45 Answer 3 Score 11
02:02 Answer 4 Score 3
02:31 Thank you

--

Full question
https://superuser.com/questions/173765/e...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#keyboardshortcuts #emacs #python #textediting #indentation

#avk47



ACCEPTED ANSWER

Score 76


Assuming that the variable python-indent is 4:

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



ANSWER 2

Score 19


indent-rigidly takes a prefix argument indicating how much to indent by, so C-u 42 C-x TAB indents by 42 columns, and since the default prefix argument is 4, C-u C-x TAB indents by 4 columns.

If you want to select the region again, do C-x C-x afterwards.




ANSWER 3

Score 11


Use the indent-rigidly command with a numeric prefix.

C-u 4 M-x indent-rigidly to indent the region by four spaces, C-u -4 M-x indent-rigidly to remove four spaces.




ANSWER 4

Score 3


You could also use column mode.

Select the lines in column mode, then add four spaces to all of them at once:

  1. Jump to column 1 of the first line you want to indent.
  2. C-<space> to set mark
  3. move the point down to the last lines (stay in column one)
  4. C-x r t to enter column mode (or C-<RET> if you've got CUA mode)
  5. <space><space><space><space><RET>

You can do a similar maneuver to delete four spaces -- just include 4 columns in your marked region (steps 2-3) and hit <DEL>