resting
resting

Reputation: 17467

How can I set a tab width for JSON files?

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

Upvotes: 28

Views: 10818

Answers (3)

Jackson Ray Hamilton
Jackson Ray Hamilton

Reputation: 9466

(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.

Upvotes: 52

resting
resting

Reputation: 17467

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.

Upvotes: 6

Alex Ott
Alex Ott

Reputation: 87174

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)

Upvotes: 5

Related Questions