34

I have this at the end of my .emacs:

(setq-default indent-tabs-mode nil)

(setq tab-stop-list (number-sequence 2 120 2))

It works fine in other modes – whenever I tab, it adds two spaces. But in js2-mode, it adds 4.

Loren
  • 13,903
  • 8
  • 48
  • 79

3 Answers3

43

You can set js2-basic-offset to 2.

  1. M-x customize-variable
  2. Enter js2-basic-offset
  3. Set the value to 2

Then Apply and Save button in the customize option buffer. This will add an item to set the variable into .emacs or init.el file.

ntalbs
  • 28,700
  • 8
  • 66
  • 83
15

Little late here, but if you're like me and keep configurations for major modes in separate files, you can add the following line and it works as well.

(add-hook 'js2-mode-hook (lambda () (setq js2-basic-offset 2)))
Rigotti
  • 2,766
  • 23
  • 28
13

For me js2-basic-offset is an alias to js-indent-level, so I had to change the latter to make it work.

Thomas Fankhauser
  • 5,039
  • 1
  • 33
  • 32
  • 2
    I haven't tried changing `js2-basic-offset` as a user customized variable as the accepted answer suggest, but to change it as a simple `setq` in a `.emacs`, yours should be the answer, because setting `js2-basic-offset` didn't work to me either. Using `setq-default` for `js2-basic-offset` works as well. – ABu Feb 28 '19 at 17:43
  • 1
    Everything else that was recommended seemed to fail. `js2-indent-level` works fine! – Mapsy Jul 07 '19 at 17:30
  • @Mapsy, if I don't mistake it should be `js-indent-level`, not `js2-indent-level`. – Andrii Tykhonov Aug 24 '21 at 20:24