8

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))
2
  • Take a look at web-mode-comment-formats. I think this is the proper variable for what you want.
    – caisah
    Oct 10, 2016 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, 2016 at 18:54

1 Answer 1

6

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" . "//"))

Or

(setq-default web-mode-comment-formats
              '(("java"       . "/*")
                ("javascript" . "//")
                ("php"        . "/*")))
2
  • 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, 2017 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, 2018 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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