51

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
  • 5,630
  • 38
  • 140
  • 292

5 Answers5

123

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
  • 155,172
  • 47
  • 273
  • 272
  • 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
39

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
  • 20,270
  • 7
  • 50
  • 76
Roy
  • 3,574
  • 2
  • 29
  • 39
  • 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
28

[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
  • 479
  • 7
  • 32
  • 2
    This will have unintended results if [CUA](https://www.emacswiki.org/emacs/CuaMode) mode is used – scruss Aug 04 '20 at 12:23
  • Do you mean that tabs and spaces will get messed up? – vineeshvs Aug 05 '20 at 14:27
  • 1
    C-x TAB calls "indent-rigidly" function in my buffers, so if C-x TAB doesn't work you can call M-x: indent-rigidly. And instead of Esc, down arrow may end it too. – AlbusMPiroglu May 16 '21 at 05:09
  • C-x is text cut in CUA mode; indent-rigidly would need to be remapped to another key combination, a la @nephewtom's answer here: https://emacs.stackexchange.com/questions/31564/emacs-bulk-indent-for-python-while-cua-mode-is-enabled – Erik Knowles Jul 07 '22 at 00:08
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
  • 514
  • 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
0

Another method to indent several lines is just to use the commands to edit several lines: Edit multiple lines at once in Emacs.

Basically, select region and then C-x r t+Tab.