3

I am new to Emacs and I would like to know if it can auto-format code for a variety of languages (Java, C++, HTML, LaTeX) just like auto-format option in NetBeans IDE.

I would also like to know

  • Whether this feature is built-in
  • Can I download an el file with this option
  • How to customize the preferences of auto-format (tab-length, lines between functions, braces in new line or in the same line, etc) just like in NetBeans.

(Versions: Emacs-24 on Ubuntu-12.04 / OS-X-10.9)

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
  • 1
    Emacs is not an IDE per se. But I do believe [this post](http://stackoverflow.com/questions/63421/using-emacs-as-an-ide) might change your opinion. – Ébe Isaac Apr 29 '15 at 04:26

1 Answers1

2

You would find it by yourself if you called it indent. Indeed, there are many interactive functions that start with indent. We easily find them with M-x indent TAB. But we can also find documentation with C-h ?, so if we try with C-h d RET indent RET to search in functions' documentation, or with M-x apropos RET foo RET we would also find them.

The following are built in and work with different languages (just tried with elisp, python and javascript):

  • M-x indent-region formats the selected region,
  • indent-sexp formats the current expression

There is no global option to configure the indentation, we need to dive in each mode's options. There is often options like (setq html-tab-width 4).

edit: some hints: (setq tab-width 8), (setq c-set-style "K&R"), (setq c-basic-offset 8) and with emacs' built in c-mode or GNU Indent you can switch styles: https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Choosing-a-Style or https://www.gnu.org/software/indent/manual/indent.html#SEC4

Ehvince
  • 17,274
  • 7
  • 58
  • 79
  • Thanks to @Ehvince, indent-region works great. But how to customize how this indentation is performed. I see that in C, this indenting command indents the braces in a new line and also indents the braces itself slightly from the conditional statement too. I would like the braces to come along the same column as the start of its parent conditional (if/while/for/do/switch). How to edit this feature? – Ébe Isaac Apr 29 '15 at 04:44
  • some hints: `(setq tab-width 8)`, `(setq c-set-style "K&R")`, `(setq c-basic-offset 8)` and with GNU Indent you can switch styles https://www.gnu.org/software/indent/manual/indent.html#SEC4 – Ehvince Apr 29 '15 at 07:27
  • and see the styles of emacs's c mode here: https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Choosing-a-Style – Ehvince Apr 29 '15 at 07:30