Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting emacs M-; to produce // style comments

Tags:

emacs

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
like image 807
Noron Avatar asked Aug 02 '11 08:08

Noron


People also ask

How to comment emacs?

The command to create or align a comment is M-; ( comment-dwim ). The word “dwim” is an acronym for “Do What I Mean”; it indicates that this command can be used for many different jobs relating to comments, depending on the situation where you use it.

How do I comment out a region in Emacs?

As mentioned above, the CTRL +; key in Emacs comments out the entire line where the cursor or the region is located. If you want to comment out only the selected region, you can use the ALT +; key. For example, consider the following selected region. To comment out only the highlighted region, use the ALT +; key.

How do you uncomment a region?

To uncomment the region you use C-u - a plain prefix arg (no explicit number) to uncomment the region. C-h f comment-region says: comment-region is an interactive compiled Lisp function in newcomment.


2 Answers

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   "")))
like image 130
Michael Markert Avatar answered Sep 28 '22 03:09

Michael Markert


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))
like image 20
koddo Avatar answered Sep 28 '22 01:09

koddo