I'd need some guidance with adjusting my indentation settings for js-mode (JavaScript). It should basically look like C/C++'s Stroustrup mode. Most of it works as expected, however there are some cases that are not indented as expected:

  1. Indentation of multi-line initialiters { should start at column of declaration (var or const):

    function make_curve (id, color, fillColor)
    {                         // correct
        const en =            // correct
        {                     // desired
              {               // current, wrong 

    The indentation does match with const en = {but I very much prefer to put { in an extra line and align { with the matching }.

  2. Opening brace of function body should be aligned with function keyword. Example:
        = (window.requestAnimationFrame
           || window.webkitRequestAnimationFrame
           || ...
           || function (callback)
              {           // desired
           {              // current wrong

My current setup is:

(defun my-js-mode-hook ()
  (setq tab-width 4
        ;; this will make sure TABs are used instead of spaces
        indent-tabs-mode t)

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

1 Answer 1


Indentation in js2-mode is not as customizable as indentation in C mode. As such it's not possible to do this outside of changing the source code for js2-mode itself.

Like you, I like to have statements inside a function indented relative to the function keyword, so I added this code to the middle of the js2-bounce-indent function:

;; Fifth likely point:  indent from 'function' keyword on previous line.
(setq pos (save-excursion
            (forward-line -1)
            (goto-char (point-at-bol))
            (when (re-search-forward "\\s-+\\(function\\)\\s-+"
                                     (point-at-eol) t)
              (goto-char (match-beginning 1))
              (skip-chars-forward " \t\r\n")
(when pos
  (cl-incf pos js2-basic-offset)
  (push pos positions))

You can do something similar for variables. Note especially the "second likely point", which looks for " = " on the previous line to choose an indentation point; adjusting this will let you do what you want.

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.