27

I have the following in my .emacs file. But it doesn't change the tab width in .json files.

(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(setq standard-indent 2)

I'm using emacs 24.3 on OS X 10.8.4

0

3 Answers 3

51
(add-hook 'json-mode-hook
          (lambda ()
            (make-local-variable 'js-indent-level)
            (setq tab-width 2)
            (setq js-indent-level 2)))

Make the variable buffer local so that it does not conflict with js-mode for JavaScript files.

1
  • It works well with js2-mode. Js2Mode
    – xb.
    Jun 13, 2020 at 11:35
6

I used M-x customize as mentioned here: How to change the indentation width in emacs javascript mode

It inserted '(js-indent-level 2) into my .emacs file.

But thanks for the response anyway.

5

it's better to set tab width in the corresponding hook - find the name of major mode (for example, by using the M-: major-mode, and then define the hook where to set the value:

(defun my/json-mode-hook ()
   (setq tab-width 4))
(add-hook 'json-mode-hook 'my/js2-mode-hook)

Your Answer

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

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