I am writing python code so shifting/moving blocks of lines is important to avoid going through each line individually. How can I do this without an add-on? Is there a type of operation keystroke command to do this?
-
4For non-Python-specific answers, see http://emacs.stackexchange.com/q/11 and http://emacs.stackexchange.com/q/18. – legoscia Feb 22 '15 at 03:17
5 Answers
Use command indent-rigidly
, which is bound to C-x TAB
by default.
You can specify the number of spaces to indent by using a prefix argument, such as C-5 C-x TAB
.
Or you can invoke the command and then use the left/right arrows to interactively adjust the indent level.

- 20,175
- 1
- 51
- 83
-
-
2
-
great, but why does the first line not move as well? I have to grab the last part of the line above the selection to make the set of lines I want move. – Vass Feb 21 '15 at 18:52
-
1You shouldn't need to select anything from the previous line, but you do want to select from the beginning of the first line you want to indent. For example, `C-a` to move to the beginning of the line, `C-SPC` to start marking the region, `C-n C-n` to move down a couple lines, `C-5 C-x TAB` to indent those two lines. – glucas Feb 21 '15 at 18:56
-
1"use the left/right arrows to interactively adjust the indent level" Really? Great! I didn't know this, and sometimes I had to guess how much spaces I need to add or remove. I hope this can help. – imz -- Ivan Zakharyaschev Feb 21 '15 at 19:02
-
I was surprised by the interactive feature as well. It must be fairly new, I guess. – Harald Hanche-Olsen Feb 21 '15 at 19:49
-
Just checked the NEWS file, the interactive behavior of `indent-rigidly` was introduced in Emacs 24.4. – glucas Mar 01 '15 at 21:15
If you are used python-mode.el C-c >
or C-c <
to shift blocks left or right

- 1,214
- 10
- 11
-
how come the first line of the selection remains unmoved if I don't select the last part of the line above it? – Vass Feb 21 '15 at 18:50
-
I cannot reproduce this behaviour. all line of a selections are moved. Emacs 24.4 – Lompik Feb 21 '15 at 18:54
-
glucas' comment says that this will happen if you don't select from the start of the line – Vass Feb 21 '15 at 21:39
The string-rectangle
command ( C-x r t
) can be used to insert any arbitrary text (spaces included) in a selected region.
Let's say you have this block of text and you want to insert 5 spaces in front of all lines.
abc
def
ghi
First select a "0 column" region as shown below (the point is on the character 'a' and the mark is in the same column in the row containing 'ghi':
▮bc
def
▯ ghi
Now using string-rectangle
, insert the text you want to insert in the selected region. In this example, we will be inserting 5 spaces on all the rows including and inbetween the point and the mark.
C-x r t M-5 SPC RET
That will give the below force indented text.
abc
def
ghi

- 25,203
- 3
- 74
- 179
There is C-x TAB
(bound to indent-rigidly
). Give it a prefix argument to indicate how many spaces you wish to indent by, negative removes that many spaces.

- 2,401
- 12
- 15
I found that by pressing Alt-4 and then SPC, I get the desired result. Not sure if this is the best way, but it works as well.

- 713
- 1
- 5
- 12