I am using web-mode 20161003.1327

At the moment when I comment a region in web-mode, it comments like this;

/* import chai from 'chai';
 * import 'babel-polyfill';
 * import configureMockStore from 'redux-mock-store';
 * import thunk from 'redux-thunk';
 * import * as ActionTypes from '../actions/ForecastActions';
 * const expect = chai.expect;
 * const middlewares = [ thunk ];
 * const mockStore = configureMockStore(middlewares);
 * /

This is not good for uncommenting only a section.

Can I change the commenting to use double forward slash?

I've tried using (setq web-mode-comment-style 2) but this appears to do nothing.

I have also tried web-mode-comment-formats but it also does not appear to work:

(require 'web-mode)

(add-to-list 'web-mode-comment-formats '("javascript" . "//"))

(defun my-web-mode-hook ()
  "web-mode settings"
  (setq web-mode-markup-indent-offset 2)
  (setq web-mode-css-indent-offset 2)
  (setq web-mode-code-indent-offset 2))

(add-hook 'web-mode-hook  'my-web-mode-hook)

(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.js?$" . web-mode))
  • Take a look at web-mode-comment-formats. I think this is the proper variable for what you want. – caisah Oct 10 '16 at 18:49
  • @caisah I've tried that also but it does not appear to do anything. I've updated the question – dagda1 Oct 10 '16 at 18:54

web-mode-comment-formats is a buffer local variable. Also, add-to-list may not work correctly, since there is already a mapping for javascript to /*.

You can try one of the following:

(setq-default web-mode-comment-formats (remove '("javascript" . "/*") web-mode-comment-formats))
(add-to-list 'web-mode-comment-formats '("javascript" . "//"))


(setq-default web-mode-comment-formats
              '(("java"       . "/*")
                ("javascript" . "//")
                ("php"        . "/*")))
  • Only the second solution works for me, because web-mode-comment-formats is undefined when my .emacs file loads. I wonder why web-mode-comment-formats is undefined, and where can I add this code to manipulate this variable, as you are doing in the first solution? – modulitos Jul 29 '17 at 6:51
  • It doesn't work for me. I have it set as: web-mode-comment-formats is a variable defined in ‘web-mode.el’. Its value is (("jsx" . "//") ("javascript" . "//")) – pors Mar 30 '18 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.