12

I am using emacs web-mode, and the tabbing in JS files seems to be acting strangely.

I have tab set to indent 2 spaces, but when the indent reaches 8 spaces, web-mode turns them into a tab, and then continues to tab 2 spaces until in reaches another 8, and converts that into another tab, etc.

Can I stop this from happening and tell emacs/web-mode to only ever indent spaces?

EDIT:

Here is a short nested function example:

(function() {
  function() {
    function() {
      function() {
    function() {
      // Indenting problem.
    }
      }
    }
  }
})()

It's obviously showing up fine in Emacs, but you can see the indentation problem here.

I will note here that this also occurs in javascript-mode.

6
  • 1
    Is this specific to web mode? Or does it happen anywhere?
    – Malabarba
    Oct 20, 2014 at 1:49
  • This is only happening on web-mode, everything else tabs fine with spaces indefinitely.
    – dieuwe
    Oct 20, 2014 at 2:16
  • Sorry, javascript mode does the same thing as well. But it's just those two.
    – dieuwe
    Oct 20, 2014 at 2:23
  • Which javascript-mode are you using, the builtin one? Also, does this happen when indenting html in web-mode?
    – dgtized
    Oct 20, 2014 at 4:22
  • Please post the content of your init file. Emacs uses a tab for 8 spaces by default, so there must be something in your init file to turn it off for most modes. Oct 20, 2014 at 6:56

2 Answers 2

13

In general, if you want indentation to use spaces only, I would recommend customizing:

(setq-default indent-tabs-mode nil)

This forces indentation to use spaces and no tabs. I'm not sure if web-mode uses the regular methods for indentation though, so it may not respect this.

Can you give an example file to demonstrate?

5
  • 3
    Have added an example to the question. Adding (setq indent-tabs-mode nil) doesn't seem to change anything in both web-mode and javascript-mode.
    – dieuwe
    Oct 20, 2014 at 2:29
  • 3
    I think you should use setq-default because indent-tabs-mode is buffer-local.
    – mike3996
    Oct 20, 2014 at 7:20
  • @progo Okay, that worked. Thank you so much.
    – dieuwe
    Oct 21, 2014 at 4:07
  • 1
    Is it possible to indent with tabs only ? I'm tried different settings, but it indents only with mixed tabs/spaces.
    – Dfr
    Nov 24, 2014 at 11:04
  • 1
    @Dfr You should add (setq-default indent-tabs-mode t) in your .emacs
    – fxbois
    Aug 31, 2015 at 14:14
5

Please try this code in your emacs config file to force web-mode indent. I referred it from the web-mode home-page.

(require 'web-mode)
(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)
1
  • Would this also work? (add-hook 'web-mode-hook (lambda () (setq web-mode-markup-indent-offset 2))) May 23, 2017 at 0:42

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.