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.

dgtized
  • 4,169
  • 20
  • 41
dieuwe
  • 123
  • 1
  • 8
  • 1
    Is this specific to web mode? Or does it happen anywhere? – Malabarba Oct 20 '14 at 01:49
  • This is only happening on web-mode, everything else tabs fine with spaces indefinitely. – dieuwe Oct 20 '14 at 02:16
  • Sorry, javascript mode does the same thing as well. But it's just those two. – dieuwe Oct 20 '14 at 02:23
  • Which `javascript-mode` are you using, the builtin one? Also, does this happen when indenting html in `web-mode`? – dgtized Oct 20 '14 at 04: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. – Gilles 'SO- stop being evil' Oct 20 '14 at 06:56
  • @Gilles Someone below posted something that works right now, and I think most of the other modes I was using normally actually turn tabbing off themselves. There wasn't anything in my `.emacs` file to stop the default tabbing/indentation from happening. – dieuwe Oct 21 '14 at 04:17

2 Answers2

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?

dgtized
  • 4,169
  • 20
  • 41
  • 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 '14 at 02:29
  • 3
    I think you should use `setq-default` because `indent-tabs-mode` is buffer-local. – mike3996 Oct 20 '14 at 07:20
  • @progo Okay, that worked. Thank you so much. – dieuwe Oct 21 '14 at 04: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 '14 at 11:04
  • 1
    @Dfr You should add ```(setq-default indent-tabs-mode t)``` in your ```.emacs``` – fxbois Aug 31 '15 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)
YulongNiu
  • 51
  • 1
  • 2