It's really sad not to write well formatted angularJs code inside emacs, the image below shows what I am saying.

What is happening inside the photo is that the code is already well formatted as it is, but when I create a new line it become like the photo shows.

If any one knows any solution to this problem, it would be so helpful to me. (note i've used angularJs mode but in vain)

update: what i'm expecting is auto indentation is working as the file suggesting which means if the file is indented with 4-spaces so the indent-level would be 4 spaces, if it's 2-spaces the indent-level would be 2-spaces and so on.

what is set now at my .emacs file is (setq global-tab-width 2) but that doesn't work.

note: i've done what is suggested in Jordon Biondo answer but that forces me to apply this change for Js files only where i need it to be globally applied

enter image description here

  • 1
    You don't say what behavior you expected, or what is wrong with the indentation shown. The question risks being closed as unclear. (But a guess is that you might want to turn off electric-indent-mode - just a guess.)
    – Drew
    Feb 12, 2016 at 15:02
  • Indeed. Instead of a screenshot, please post the source you get, the source you want, how they differ, and any other requirements to reproduce this. Only then can we help you.
    – PythonNut
    Feb 12, 2016 at 16:52

1 Answer 1


The provided javascript mode,js-mode, indents with four spaces by default, and you are trying to edit code that was indented with two spaces. You need to to change your indent settings to use two spaces instead like this:

(defun my-js-mode-hook ()
  (setq-local js-indent-level 2))

(add-hook 'js-mode-hook 'my-js-mode-hook)

Will fix your problem.

You can also use M-xcustomize-variable to customize js-indent-level if you prefer the customize interface.

  • that worked well thank you, i used global-tab-width 2. is there any way to make global indent level = 2. or to adapt emacs so that if the file indent level = 2, the js-indent-level =2 and so on.
    – Islam Taha
    Feb 12, 2016 at 14:54
  • Not that I am aware of, any programming mode can implement indentation using what ever variables or methods they want, there is no real global setting. Feb 12, 2016 at 15:42
  • @IslamTaha In recent Emacs version you can set standard-indent, but I'm not sure whether JS Mode respects it. Generally I recommend that you use directory variables to change indentation settings per project.
    – user227
    Feb 13, 2016 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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