Spacebars indentation in Emacs

Is there a way to get Emacs to auto-indent Spacebars blocks? Right now, Emacs automatically does this:

{{#each player}}
{{name}}
{{/each}}

instead of this:

{{#each player}}
    {{name}}
{{/each}}

Failing that, can I stop Emacs from deleting my indentation when I hit return? If I manually add indentation so that the code looks like the second example above, then hit return after {{name}}, it deletes the indentation so it looks like the first example. It’s very annoying.
Thanks in advance for any help!

Which emacs version are you using? Which distribution? Vanillia? Spacemacs? Doom? Which major mode is ono for html files?

Right now I’m using Emacs 25.2.2. I just use the basic GNU Emacs that I installed in Ubuntu 18.04. The major mode when I open html files is HTML.
Thanks!

Try using web-mode!

Note: I haven’t used vanilla emacs in a long time.

Do you know how to set up web-mode for meteor? I installed it, but it doesn’t do spacebars indentation out of the box.
I’m a bit confused by the instructions on http://web-mode.org/. In a table of engine families, it says that Meteor and Blaze are cousins of ctemplate. Above the table, it says, “Never forget to update the auto-mode-alist”, but it doesn’t give any instructions of the syntax to use.

Elsewhere it says, “Using [web-mode-engines-alist] is required as soon as the file extension is unknown (by web-mode) or is too general (e.g. *.html). In summary, you may have to set both auto-mode-alist and web-mode-engines-alist.” So I tried adding this to .emacs:

(setq web-mode-engines-alist
      '(("ctemplate" . "\\.html\\'"))
      )

But it didn’t solve the indentation problem.

No, sry…

That is one of the reasons I stopped using vanilla emacs. In emacs doom you just have to uncomment one line to get this working. But AFAIK it is tailored for vim keybindings. But spacemacs lets you choose between emacs and vim keybindings on installation, and settings up web-mode should be as easy as emacs doom.

You might want to ask the original question in an emacs forum, as this is not specific to meteor.

Good idea about posting in an emacs forum.
For anyone reading, I posted my question here.

I figured out how to get web-mode to work!

The most obvious thing, in retrospect, is that to switch from HTML mode to web-mode, you have to open an html file and press M-x web-mode. It looks like all html files open with web-mode after that. The Emacs Manual has an explanation of modes.

It turns out that I did guess the correct syntax to set up web-mode for meteor before. Here is everything I put in my .emacs file, after downloading web-mode to .emacs.d/web-mode/:

(add-to-list 'load-path "~/.emacs.d/web-mode/")
(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
(setq web-mode-engines-alist
      '(("ctemplate" . "\\.html\\'"))
      )

The Install section on web-mode.org says to add-to-list several other file extensions, but they seem unnecessary for this purpose.
.
.
.
The only weird thing with this procedure is that when I press M-x html-mode, the syntax highlighting doesn’t go back to the previous colours. But once I get web-mode set up the way I want it, I won’t feel the need to go back and forth.
Thanks @ni-ko-o-kin for your suggestion. I’m glad to have finally found a solution.

1 Like