How do I use spaces instead of tabs everywhere? I noticed that web-mode was using tabs and could not figure out how to fix it. (Also, you may notice I was trying to set 2 tab spaces globally everywhere. It may be related to the problem.)

Currently, my only solution is untabify, but I would rather this be something automatic.

my settings below:

(global-set-key (kbd "TAB") 'self-insert-command)
(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)
(setq indent-line-function 'insert-tab)

(use-package web-mode
  :ensure t
  (add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.api\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("/some/react/path/.*\\.js[x]?\\'" . web-mode))
    (setq web-mode-content-types-alist
  '(("json" . "/some/path/.*\\.api\\'")
    ("xml"  . "/other/path/.*\\.api\\'")
    ("jsx"  . "/some/react/path/.*\\.js[x]?\\'")))
  (setq web-mode-engines-alist
        '(("php"    . "\\.phtml\\'")
          ("blade"  . "\\.blade\\.")))
  (setq web-mode-markup-indent-offset 2)
  (setq web-mode-code-indent-offset 2)
  (setq web-mode-css-indent-offset 2))
  • I like to set tabs to just one space width. I created a simple function that I bind to my favorite keyboard shortcut that inserts 2 spaces -- I like to use M-SPC for that. I use tabs for triggering my favorite yasnippet snippets where the tab is the first in a series of one or more additional keys. I insert literal tabs with C-q tab – lawlist May 11 '17 at 3:36
  • 2
    Why are you binding TAB to self-insert-command? That will cause a TAB character to be inserted, which is apparently what you're trying to avoid? I wouldn't recommend binding TAB at all, or changing the indentation functions. Set indent-tabs-mode to nil, tab-width to 2, and configure any mode offset/indentation variables on a case by case basis. – phils May 11 '17 at 3:54
  • @phils I researched beforehand and got this idea from another stack overflow post see: stackoverflow.com/questions/69934/…. I however can not get a working solution. You will see in my response below to sds that I removed this and it still does not work. Thanks for the reply. – codingdraculasbrain May 11 '17 at 4:14
  • No, you only said you'd removed (setq indent-line-function 'insert-tab). I can't replicate the problem (in web-mode, setting indent-tabs-mode, tab-width, and the web-mode-markup-*-offset variables). Can you provide a minimal example of something which doesn't work? – phils May 11 '17 at 4:24
  • basically anything in web mode won't use 2 spaces instead of tabs. It uses a tab character instead of spaces. <hit tab key><h1> hello world</h1>. Instead of 2 spaces. Tab key is a tab character. This specifically happens when files are using web mode. – codingdraculasbrain May 11 '17 at 4:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.