I've set my tabs to 4 and haven't changed any JS indent settings. All the indents I found in the "JavaScript group" have values either 0 or 4, yet the following code

describe('test Info', () => {
    it("should respond with Info", done =>
       doGet("Info", r => {

gets indented to columns 4, 7 and 11, while I'd strongly prefer 4, 8 and 12. I guess, Emacs indents doGet just below "should...", but I can't see any reason for it. Strangely, it doesn't do this on the next line...

How can I change it?


GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9)

Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption Blink-Cursor File-Name-Shadow Font-Lock Global-Font-Lock Line-Number Menu-Bar Mouse-Wheel Show-Paren Tooltip Transient-Mark

Javascript mode defined in `js.el':

  • 1
    Line 4 is not strange, because line 3 is not a deeper level of indentation. Instead it is merely aligning function arguments at the same level. – dcorking Jun 2 '18 at 22:13
  • Which js mode are you using? – dcorking Jun 2 '18 at 22:14
  • @dcorking OK, it's aligning the doGet line, but not the line above it nor the line below it. I guess, the difference is the missing opening brace. Anyway, it's not what I want as I never align arguments (maybe it's common in JS, but I mainly use Java). It's actually deeper as it's inside of the it( opening parenthesis. +++ I've added some details to the question. – maaartinus Jun 2 '18 at 22:26
  • Do you want Emacs to indent with tab characters or spaces? – dcorking Jun 3 '18 at 5:43
  • @dcorking Tabs only. Every indentation should be a single tab. Currently, tabs get used where possible. – maaartinus Jun 3 '18 at 10:37

I don't have a pure Emacs fix for you. For a lot of my projects, I allow Emacs to call an external formatter, prettier, that with one setting, does what you want. (Prettier is a free npm package you can install with npm or yarn.)

I installed the prettier-js package from ELPA and initialized it with:

(require-package 'prettier-js)
(require 'prettier-js)
(dolist (hook '(js2-mode-hook js-mode-hook json-mode-hook))
  (add-hook hook 'prettier-js-mode))

Then I customized prettier to use tabs by setting prettier-js-args to '("--use-tabs"). prettier-js formats on save, so on saving your code, I got:

describe("test Info", () => {
    it("should respond with Info", done =>
        doGet("Info", r => {

Prettier is quite opinionated, so while it agrees with your opinions here, it may force other formatting changes on your code that you don't like.

  • 1
    As you wrote, it's quite opinionated. And so am I, unfortunately. +++ The formatted code from your snippet looks fine according to what I wrote in the question. In the meantime, I've got one more requirement: The closing })) on the last but one line close the it, so it should be just below it. IOW it closes two things, so it should decrease the indentation by two. +++ I'm afraid, this is not something, Emacs could be configured to. – maaartinus Jun 6 '18 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.