34
votes
How to pretty-format code (auto-insert newlines, indent, etc)?
json.el package provides json-pretty-print-buffer. This is what it produces for your example:
{
"menu": {
"popup": {
"menuitem": [
{
"onclick": "CreateNewDoc()",
...
24
votes
Accepted
Completely disable all auto-indentation
Emacs ≤24.3
Pressing RET doesn't indent by default (except in some programming modes, for which you need to check mode-specific configuration, e.g. “electric” settings).
Unix terminals normally ...
22
votes
How to set indent to 4 spaces in nxml-mode?
I finally found it in this issue.
(setq nxml-child-indent 4 nxml-attribute-indent 4)
21
votes
Accepted
How prettify (newlines and indentation) an optimized HTML/JS file?
You can use the built-in sgml-mode for this.
M-x sgml-mode - Enable the sgml-mode major mode so that sgml-pretty-print command auto-loads.
C-x h - Select the whole buffer.
M-x sgml-pretty-print
To ...
17
votes
Accepted
Correct indentation for wrapped lines
After M-x package-install RET adaptive-wrap RET:
(setq-default adaptive-wrap-extra-indent 2)
(add-hook 'visual-line-mode-hook #'adaptive-wrap-prefix-mode)
(global-visual-line-mode +1)
16
votes
Accepted
How to pretty-format code (auto-insert newlines, indent, etc)?
It looks like you need the fill-region function more than the indentation.
Select that line and do M-x fill-region.
Select the same, now split across multiple lines, and do M-x indent-region.
...
16
votes
Accepted
Modifying the indentation of some Emacs Lisp forms
Answer:
Symbols for functions and macros have a lisp-indent-function property which you can set with declare or in your case, since if is already defined, you can just use put.
You can read about ...
15
votes
Accepted
Org-mode : disable indentation when promoting and demoting trees + subtrees
Set the variable org-adapt-indentation to nil. The default value is t, which means:
When this variable is set, Org assumes that you write outlines by
indenting text in each node to align with the ...
14
votes
Accepted
How to make emacs align and indent a bibtex entry?
When the cursor is somewhere in the entry, run the command bibtex-fill-entry (bound to C-c C-q), which will
align the fields. You may also want to set variable bibtex-align-at-equal-sign to a non nil ...

Dan♦
- 32.3k
11
votes
Accepted
Set indentation for shell script function
Setting sh-basic-offset to the required indentation is what is needed - but what I didn't realise is, it's also necessary to close and reopen all shell script files before the customisation takes ...
10
votes
Accepted
How to indent without the two extra spaces at the beginning of code blocks in org mode?
Try to customize org-edit-src-content-indentation. I think by default it is set to 2. I think this is what controls that behavior.
However, if you put (setq org-src-preserve-indentation t) in your ...
10
votes
Accepted
no html/jsx indentation in jsx-mode
This is a known issue and as of now, it has not been fixed. I've decided to forego jsx-mode for this reason and just use web-mode. It has support for JSX and is better suited for mixed content files.
...
10
votes
Accepted
How to detect tabs or spaces in Emacs
By default I want all indentation (when I hit the tab key) to be 2 spaces.
This can be achieved for many modes with settings such as
(setq-default indent-tabs-mode nil
tab-stop-list ...
9
votes
Accepted
Commented lines shoot off to column 32 in octave-mode
I achieved what I wanted by overriding some definitions from the original major mode. In /lisp/progmodes/octave.el (the file was zipped, I had to unzip it to view the source), I redefined octave-...
9
votes
Word wrap that preserves indent from previous line
I stumbled across an answer to this... on StackOverflow...
You can use the package Adaptive-Wrap.
It's pretty nice, there's an "extra indent" option you can use for the wrapped line, though no max ...
9
votes
no html/jsx indentation in jsx-mode
FYI for anyone else who comes across this issue, this is because this jsx-mode project is not made to edit Facebook's React JSX templates but rather this other compiled javascript language also called ...
9
votes
Accepted
formatting constexpr if in c++-mode
UPDATE: That pretest version that I put down there in the original question (emacs-26.0.90.tar.gz) had a bug that cause certain infinity loops while parsing the buffer, specifically when using the <...
9
votes
Accepted
Why does org-mode not indent content under headings when I press tab?
Org's indentation behaviour is controlled by the org-adapt-indentation variable. It can be set to three states:
t: Adapt indentation for all lines
headline-data: Adapt indentation for headline data ...
8
votes
Is there a function that returns the position of the first and last non-whitespace characters in a line?
The function current-indentation will return the column number of the first non whitespace character on the current line.
(current-indentation)
Return the indentation of the current line. This ...
8
votes
How can I write [0,1) in LaTeX mode without breaking indentation?
Not an Emacs solution, but my LaTeX code writes such intervals as \halfopen{0}{1}, where
\newcommand\halfopen[2]{\ensuremath{[#1,#2)}}
This convention pushes the balancing issue to a place where it'...
8
votes
Indentation in square brackets
Setting LaTeX-begin-regexp and LaTeX-end-regexp does not help here because inside LaTeX-indent-level-count it searches a backslash before checking the regexp.
I got it working by modifying TeX-brace-...
7
votes
How to pretty-format code (auto-insert newlines, indent, etc)?
According to the tip here, you can use external command to format a region in your Json file:
(defun json-format ()
(interactive)
(save-excursion
(shell-command-on-region (region-beginning)
...
7
votes
Accepted
Indent each yasnippet line according to major mode
Is there an option in yasnippets to automatically indent each line according to the mode's default indentation of the line?
This is what setting yas-indent-line to 'auto is for, except that you also ...
7
votes
Commented lines shoot off to column 32 in octave-mode
Try using more than one % or # in a row: %% or %%% etc.
By default, the number of consecutive comment chars determines the kind of comment and its behavior (e.g., with respect to indentation).
See ...
7
votes
Accepted
Trouble adjusting tab-width
The first line when enabling python-mode is (set (make-local-variable 'tab-width) 8). Because tab-width is a buffer-local variable, a new value would need to be set subsequent to the code above. The ...
7
votes
Accepted
Change the behaviour of RET (with electric indent) to only indent the new line, not the previous one
I think you'll get what you want with
(setq-default electric-indent-inhibit t)
This was meant as a variable for major-modes (rather than for users), but it should take care of your use-case as well.
7
votes
Accepted
Why does Emacs indent my Lisp LOOP construct weirdly?
You can use indentation provided by a SLIME contribution:
(require 'slime-cl-indent)
You may want to add that to the list slime-contribs- see Loading Contribs in the SLIME manual.
One can add it to ...
7
votes
Accepted
Emacs: indentation takes 4 spaces instead of 2
See this answer to a similar question. That answer doesn't answer that question, because it is about Ruby mode not respecting such code. But it likely does answer your similar question.
This is that ...
7
votes
Confusion about space and tab characters in Emacs
Like most things in emacs, the tabs policy is configurable. It sounds to me that you'd like to follow the most common apporach and use the tab key as an indentation tool and not have tab characters in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
indentation × 522org-mode × 48
python × 45
tabs × 38
c++ × 31
cc-mode × 24
web-mode × 21
spacemacs × 19
auctex × 19
whitespace × 19
c × 19
javascript × 19
latex × 18
major-mode × 15
formatting × 15
key-bindings × 14
comment × 14
evil × 13
electric-indent × 12
init-file × 10
php-mode × 9
smie × 9
org-babel × 8
align × 8
newlines × 8