0

All I want to do is enable tern-mode, when I enter js-mode. I know that a hook called js-mode is run when I open a javascript file but for some reason tern-mode is not enabled. In my init.el file I have:

(add-hook 'js2-mode-hook (lambda () ('tern-mode)))

I also tried:

(add-hook 'js2-mode-hook (lambda () (tern-mode t)))

as well as both of those with js-mode-hook (This was mostly for fun though because it makes no difference, see the comments)

I have also tried:

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

And this does't work. However, tern-mode does not get enabled when I enter js2-mode. To enable it, each time I have to press M-x tern-mode, then it works.

Is there anything I am doing wrong here?

9
  • 1
    If you want to enable a minor mode named tern-mode when entering the major mode named js-mode, you can use (add-hook 'js-mode-hook 'tern-mode), you usually don't have to wrap tern-mode unless you have very old version of Emacs or some other special reason. js2-mode and js-mode is different thing, figure which you're using. Tern's doc already says how to use it, see ternjs.net/doc/manual.html#emacs
    – xuchunyang
    Commented Sep 30, 2015 at 6:54
  • Reading the documentation for js2-mode says: In addition to any hooks its parent mode js-mode' might have run, 15 this mode runs the hook js2-mode-hook', as the final step 16 during initialization. I have the newest (24.5) version of emacs and have tried your suggestions and it does not work.
    – Startec
    Commented Sep 30, 2015 at 7:03
  • Startec: (add-hook 'js-mode-hook 'tern-mode) works fine for me (aside from erroring out because I don't have tern installed; but the minor mode is enabled as expected).
    – phils
    Commented Sep 30, 2015 at 7:13
  • 1
    correction: What I get with C-h v js-mode-hook RET is Its value is (tern-mode skewer-mode ac-js2-mode ac-js2-setup-auto-complete-mode) . but it still does not immediately open.
    – Startec
    Commented Sep 30, 2015 at 8:06
  • 2
    @Startec: note that only recent versions of js2-mode derive from js-mode. So if the user has an earlier version it might not run js-mode-hook. In any case I suspect the *Messages* might contain some hint.
    – Stefan
    Commented Sep 30, 2015 at 12:52

1 Answer 1

1

The canonical answer here is:

(add-hook 'js2-mode-hook 'tern-mode)

This line of code is in my emacs config and works fine. When I open a .js file, tern-mode is enabled.

If that doesn't work, there are other things to debug, such as what package manager you're using, which files you're opening, whether tern has any prerequirements you're not fulfilling (like a server available in bin/tern ?), etc.

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.