4

In my init.el file I have the following line (setq js2-basic-offset 2)

The last line in my .init.el file is

(add-hook 'after-init-hook
         (lambda ()
           (message "Welcome home %s" (user-login-name))))

When I start emacs and go to the *Messages* buffer, I see:

Loading c:/Users/user/.emacs.d/.mc-lists.el (source)...done
[yas] Prepared just-in-time loading of snippets successfully.
Welcome home user

However, when I do C-h v js2-basic-offset I get

js2-basic-offset is a variable defined in ‘js2-mode.el’.
Its value is 4

  This variable is an alias for ‘js-indent-level’.

Documentation:
Number of spaces for each indentation step in ‘js-mode’.

You can customize this variable

If I go to my init.el file and re-load it, the variable gets set to it's correct value of 2.

Does anybody have any solution for this issue?

  • 1
    You probably require the js-mode after setting js2-basic-offset, thus that it overwrites your value. Put a (require 'js-mode) before setting the value and see whether that resolves the issue. Note that this always requires the mode, wheter you you it or not, causing probably longer start time. – theldoria Jun 2 '17 at 8:57
  • Looking at the source of js2-mode I found that js2-basic-offset is alias for js-indent-level. So setting the latter value may also help. – theldoria Jun 2 '17 at 9:02
  • Check that you don't have a customized value being evaluated after your setq form. – phils Jun 2 '17 at 11:24
7
  This variable is an alias for ‘js-indent-level’.

That's the clue. If you're running Emacs ≥25 then js2-mode defines js2-basic-offset as an alias of the variable js-indent-level. At the point where the variable is made an alias, any value that it had is forgotten. Only the value of the base variable (js-indent-level) matters.

If you set it later then the assignment dereferences the alias: once the alias is in place, (setq js2-basic-offset 2) is equivalent to (setq js-indent-level 2).

So set js-indent-level in addition to js2-basic-offset, or instead if you don't care about versions of Emacs older than 25.

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.