I just installed web-mode. It works, but the indentation seems to be set to 4 spaces by default. How does one properly set it to 2 spaces?


According to the web-mode documentation, you can do that with the following:

(defun my-web-mode-hook ()
  "Hooks for Web mode."
  (setq web-mode-markup-indent-offset 2)
(add-hook 'web-mode-hook  'my-web-mode-hook)

You can also set values for web-mode-css-indent-offset for CSS, and web-mode-code-indent-offset Javascript, Java, PHP, etc.

  • 3
    It might be better to customize-group web-mode and set this. Dec 26 '17 at 23:23
  • @ChristianStewart or even better, do it with use-package
    – DomQ
    May 7 '20 at 15:49

With use-package:

(use-package web-mode
  (web-mode-markup-indent-offset 2)
  (web-mode-css-indent-offset 2)
  (web-mode-code-indent-offset 2))

This is what worked for me:

(setq indent-tabs-mode nil
      js-indent-level 2)
  • 1
    I'm pretty sure these variables don't affect web-mode indentation at all.
    – user12563
    Jan 19 '19 at 13:17
  • the provided solution above did not work for me. What I shared here is what solved this issue for me., actually (js-indent-level 2) will do the job also Jan 19 '19 at 13:19

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.