13

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? Commented Oct 20, 2014 at 1:49
  • This is only happening on web-mode, everything else tabs fine with spaces indefinitely. Commented Oct 20, 2014 at 2:16
  • Sorry, javascript mode does the same thing as well. But it's just those two. Commented 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? Commented 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. Commented Oct 20, 2014 at 6:56

2 Answers 2

14

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?

4
  • 4
    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. Commented Oct 20, 2014 at 2:29
  • 4
    I think you should use setq-default because indent-tabs-mode is buffer-local. Commented Oct 20, 2014 at 7:20
  • 1
    Is it possible to indent with tabs only ? I'm tried different settings, but it indents only with mixed tabs/spaces. Commented Nov 24, 2014 at 11:04
  • 1
    @Dfr You should add (setq-default indent-tabs-mode t) in your .emacs Commented 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))) Commented May 23, 2017 at 0:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.