Feed on
Posts
Comments

one of the reasons why i use emacs, as opposed to just a vanilla text editor, is that it auto-indents your code. all you’ve got to do is ask it to do so nicely.

just highlight the region of code you want indented (control-space at the beginning of the text block, then move the blinking cursor to the end of the text block) and hit:

>> C-M-\

or, “control-meta-backslash.”


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

10 Responses to “how to auto-indent in emacs”

  1. on 23 Sep 2007 at 9:23 am x

    Not always working, maybe i’m dull on this …
    Ctrl+M just gives a newline, before hitting \

  2. on 27 Sep 2007 at 6:44 pm y

    It’s not control + M (as in shift+m) it’s control + meta, usually alt or escape. So, depending on your settings it might be control+alt+\

  3. on 20 Jan 2008 at 8:05 am noshir

    thank you

    and thank you emacs so nice this auto indent

  4. on 16 Feb 2009 at 4:12 pm me

    You’re confusing auto-indent with auto code formatting. auto-indent means that it inserts the same space before a new line as it did on the previous line.

    Automatic code formatter is a different thing entirely. Ctrl+Alt+\ is an auto code formatter. Your article caused me hours of confusion before I found out what the VI equivalent of :set ai was on emacs….

  5. on 12 Mar 2009 at 6:49 am john doe

    And could you write what is the VI equivalent od :set ai in emacs?

  6. on 17 Jun 2009 at 5:55 am Amit

    Thanks a lot !
    I used to hit tabs per line before. This is awesome. Is there a way to remove multiple blank lines and keep only one blank line ?

  7. on 13 Jul 2009 at 11:09 am octoberdan

    Thank you for the tip! Quite useful. If indent-tabs-mode is nil, will tabs be converted to spaces?

  8. on 03 Aug 2009 at 5:27 pm verma

    Works great! thanks for the tip.

  9. on 29 Nov 2010 at 8:45 am Rob Stewart

    @john doe

    There is no Emacs equivalent of vi’s :set autoindent. Instead, Emacs references a variable, indent-line-function, to determine the function to call to handle indentation. Setting indent-line-function to indent-relative or indent-relative-maybe, for example, will replicate the previous line’s indentation, according to the function’s algorithm. Normally, you don’t set indent-line-function but rather leave that to a buffer’s mode.

    For example, in an Elisp buffer, the mode is Emacs-Lisp and indent-line-function is set to list-indent-line. In a C++ file’s buffer, the mode is C++ and indent-line-function is set to c-indent-line. In a Fundamental mode buffer, indent-line-function is set to indent-line.

  10. on 29 Nov 2010 at 8:47 am Rob Stewart

    @octoberdan

    Setting indent-tabs-mode to nil will insert spaces to fill the columns a tab would have filled, but it will not convert tabs to spaces. For that you need M-x untabify.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS