Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the indentation width in emacs javascript mode

People also ask

How do you set a tab to 4 spaces in emacs?

If the previous nonblank line has no indent points beyond the column point starts at, `tab-to-tab-stop' is done instead. Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.


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.


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."


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

(setq-default js2-basic-offset 2)

Source.


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

(setq-default indent-tabs-mode nil)

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.