47

In my Emacs, space key can indent correctly 1 line. If I select several lines, and press space key, the indenting does not work.

By following this link, I try C-M-\ or C-M-q, but C-M deletes directly the whole block selected.

Here is my .emacs, could anyone help?

SoftTimur
  • 7,806
  • 30
  • 123
  • 245

4 Answers4

113

Select multiply lines, then type C-u 8 C-x Tab, it will indent the region by 8 spaces.

C-u -4 C-x Tab will un-indent by 4 spaces

kev
  • 146,428
  • 41
  • 264
  • 265
  • 3
    This is the **only** one that's worked for me, and can't be found it [GNU Emacs Indent Multiple Lines](https://www.gnu.org/software/emacs/manual/html_node/emacs/Multi_002dline-Indent.html). Terrific contribution – benjaminmgross Jun 27 '14 at 05:55
  • 2
    AND it even works when your in Evil-mode! major super ultra win! – John Curry Oct 22 '14 at 01:52
  • It is also the only one that worked for me. unknown why C-M-\ doesn't work. – zhihuifan Feb 16 '16 at 00:21
  • warning "it will ident that region by 8 spaces" no by one tab, and tab isn't 8 spaces, however it may be represented instead of 8 spaces! – Error Oct 02 '16 at 13:21
  • On emacs in OSC C-M\ does not work. How do you select the block of code anyway (without the use of mouse) – Stryker Mar 04 '17 at 15:20
  • 1
    Very short and easy command :) works for me in MacOS `C-h C-a C-u 8 C-x Tab` – prayagupa Jul 03 '18 at 21:48
  • It is described at [GNU Emacs Indentation Commands](https://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation-Commands.html) (which is linked from the mentioned `Indent Multiple Lines` article). In `Emacs` I found it by `C-h i m i ` – Y. E. Aug 04 '21 at 09:48
36

If you want Emacs to correctly indent multiple lines, then the command is C-META \ also known as C-M-\. In other words, it's not Control - M, but Control - Meta - Backslash (Control + Alt + \ on most keyboards)

Cristian Ciupitu
  • 19,240
  • 7
  • 48
  • 73
Roy
  • 3,526
  • 1
  • 27
  • 38
  • 1
    In particular, this issues the `indent-region` command. It does nothing without the `column` parameter. To provide this, one must use the universal argument prefix `C-u` followed by the column number to indent to. For example, if you wish to indent to the third column, then the key sequence is `C-u 3 C-M-\ `. For me, while this does indent the region, it also destroys the current indentation scheme. It forces each line the region to the same indentation level. – Lorem Ipsum Apr 11 '17 at 15:57
22

[Warning: This approach might lead to errors as mentioned in one of the comments. Iniviting suggestions to fix it.]

Steps to do indentation interactively.

  1. Select the region to be indented.
  2. C-x TAB.
  3. Use arrows (← and →) to indent interactively.
  4. Press Esc three times when you are done with the required indentation.
vineeshvs
  • 391
  • 6
  • 28
4

None of the above method worked for me except Kev's- and that is too verbose for my slow fingers.

For me, I can highlight the region and press Alt-left arrow . Strangely, even though Alt registers as meta in emacs, Esc-left arrow doesn't work.

Daniel
  • 474
  • 5
  • 9
  • 1
    This always works for me on whatever emacs I use. I've tried it on many different emacs installs on various os's. Much quicker than all those multiple keystroke commands, too. – T.C. Proctor Jan 23 '20 at 18:35
  • 2
    The "C-u 4 C-x Tab" solution worked for me but the one you just provided is the best I found for quick reorganisation of my code. Thank you! – Naoy Jul 21 '21 at 10:26