10

How do I change the style of comments for M-; (comment-dwim) when using c-mode?

I would like it to use comments preceeded by // instead of /* */ nesting.

Version:

GNU Emacs 23.2.1 (x86_64-pc-linux-gnu, GTK+ Version 2.20.1) of 2010-12-11 on brahms, modified by Debian
Noron
  • 103
  • 1
  • 4

2 Answers2

18

The relevant vars are comment-start and comment-end so you can use this:

(add-hook 'c-mode-hook (lambda () (setq comment-start "//"
                                        comment-end   "")))
Michael Markert
  • 3,986
  • 2
  • 19
  • 19
  • Works for PHP too :) I use this command to (un)comment the current line if nothing is selected : `(defun px-toggle-comments () "If region is set, [un]comments it. Otherwise [un]comments current line." (interactive) (if (eq mark-active nil) (progn (beginning-of-line 1) (set-mark (point)) (forward-line) (comment-dwim nil)) (comment-dwim nil)) (deactivate-mark)) ` – yPhil Sep 18 '13 at 12:18
  • In your cpp hook: (setq comment-start "/// ") (setq comment-end "") (setq comment-continue "/// ") – Ben Fitzgerald Jun 23 '14 at 16:05
  • The space should usually be in the var `comment-padding`. Might explain why @Mike gets one. – yerforkferchips Mar 23 '16 at 08:59
1

From http://www.cs.cmu.edu/cgi-bin/info2www?(emacs)C%20Mode:

C++ mode is like C mode, except that it understands C++ comment syntax and certain other differences between C and C++. It also has a command `M-x fill-c++-comment', which fills a paragraph made of C++ comment lines.

So you can just add this line to your .emacs:

(add-to-list 'auto-mode-alist '("\\.[ch]\\'" . c++-mode))
koddo
  • 3,328
  • 2
  • 26
  • 25