How to automatically format code in Emacs just like in Netbeans IDE
I'm new to Emacs and I would like to know if it can automatically format code for different languages โโ(Java, C ++, HTML, LaTeX), just like the autoformat option in NetBeans IDE.
I would also like to know
- Is this function built in
- Is it possible to load the el file using this option.
- How to set up auto-formatting options (tab length, lines between functions, curly braces on a new line or on a single line, etc.) like in NetBeans.
(Versions: Emacs-24 on Ubuntu-12.04 / OS-X-10.9)
source to share
You will find it yourself if you named it indent
. Indeed, there are many interactive features starting with indent
. We easily find them with M-x indent TAB
. But we can also find documentation with C-h ?
, so if we try to search C-h d RET indent RET
for a search in the documentation for functions or with M-x apropos RET foo RET
, we will find them as well.
Embedded and working with different languages โโ(only with elisp, python and javascript):
-
M-x indent-region
formats the selected area, -
indent-sexp
formats the current expression
The global option to set the indentation is not required, we need to dive in each mode. There are often options like (setq html-tab-width 4)
.
edit: some tips: (setq tab-width 8)
, (setq c-set-style "K&R")
, (setq c-basic-offset 8)
and using 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
source to share