136

I'd like to use 2 spaces for my indents instead of 4. I can change the default behavior of C mode using:

(setq c-basic-offset 2)

How do I change this in javascript mode?

mksuth
  • 1,999
  • 4
  • 17
  • 24

9 Answers9

223

js-indent-level can be used in the default javascript-mode, which is included by default starting in emacs 23.2.

(setq js-indent-level 2)

should do what you're looking for. If you're using an older version of emacs, you may be in java-mode. I think this mode responds to c-basic-offset, however I may be wrong.

Willi Ballenthin
  • 6,444
  • 6
  • 38
  • 52
  • 2
    (setq default-tab-width 2) would be the corresponding tab width setter for java-mode – Fergie Nov 14 '11 at 10:14
  • Using this mechanism the tab width remains at 8 and so tabs are only used if indent level is 4 (2x4=8) or more. How do you set tab width to 2 or 4 so that tabs instead of spaces are used? I've tried a bunch of settings, e.g. (setq tab-width 4) but to no avail. My goal: use tab width 4, and only use tab characters (not spaces as a substitute). – jdp Sep 10 '12 at 17:02
  • jdp: Try `(defun my-javascript-mode-hook () (setq indent-tabs-mode t tab-width 4 js-indent-level 4)) (add-hook 'javascript-mode-hook 'my-javascript-mode-hook)`. – phils Jan 09 '14 at 22:34
  • Use `js-mode-hook`, like so: `(defun my-js-mode-hook () (setq indent-tabs-mode t tab-width 4 js-indent-level 4)) (add-hook 'js-mode-hook 'my-js-mode-hook)` – skeltoac Nov 19 '14 at 17:30
  • Does the value for `js-indent-level` inherit from anywhere? Can I change a single variable like "`global-indent-level`" to change the behavior of all the languages I work with? – Vortico Dec 20 '14 at 06:24
  • 7
    Is this documented anywhere? Was there a way of finding this information without stackoverflow? I looked in `M-x h` from js-mode, but couldn't find anything there. – Charles Holbrow Jul 19 '15 at 23:28
  • Seems like Javascript mode does not honor `c-basic-offset`. When I put `// -*- js-indent-level: 2 -*-` at the top of my file, it works, but when I use `c-basic-offset` it does not, in Emacs 24.5.1. – amacleod Oct 05 '15 at 20:44
  • I'm on emacs26 and js2-mode, but that solution still works! – Adobe Oct 28 '16 at 13:52
  • This works also: `M-x customize-option` and at prompt `Customize variable:` type `js-indent-level`. – kwarnke Aug 07 '17 at 12:03
91

I wish someone had told me about Custom a lot sooner! Perhaps this will help another mere mortal like me ;)

Invoke Custom:

M-x customize

Then, choose "Programming," and then "Languages," and then select a language/mode to customize. Edit the options as you see fit. When done, choose either "Save for current session" or "Save for future sessions."

wprl
  • 24,489
  • 11
  • 55
  • 70
17

If you're using js2-mode (which is more powerful IMHO), then the command is:

(setq-default js2-basic-offset 2)

Source.

metakermit
  • 21,267
  • 15
  • 86
  • 95
11

You might also want to set emacs to use spaces instead of tabs

(setq-default indent-tabs-mode nil)
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Tyler
  • 512
  • 6
  • 8
8

You can also use

M-x customize-variable

and type

js-indent-level

(js- [TAB] shows a list of options). Then change the Js Indent Level as wanted and click [State:] and save.

kwarnke
  • 1,424
  • 1
  • 15
  • 10
2

In my javascript.el file (/usr/share/emacs/site-lisp) I found

javascript-indent-level 4

So if you are using the same lisp-mode you can change it by running

(setq javascript-indent-level 2)
Helge
  • 21
  • 1
  • 2
2

Using EditorConfig could be a good idea too. And of course, Emacs had a mode for it editorconfig-emacs.

It's also available in the package manager (M-x package-list-packages) through Melpa or Marmalade.

2

If you want to change it on a per-file basis, put this at the top of your file:

// -*- mode: js; js-indent-level: 2; -*-
MatthewD
  • 2,509
  • 2
  • 23
  • 27
0

None of these solutions worked for me after upgrading to Emacs 26 (I already had js-indent-level set to 2 but my tab width went back to 8), but what did work was setting the tab-width variable to 2, which seems to replace the old default-tab-width variable.

I found this in M-x customize by searching for tab width.

Malvineous
  • 25,144
  • 16
  • 116
  • 151