Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: one-line comments every time, everywhere

Tags:

emacs

elisp

I would like an interactive function that would comment-or-uncomment region using only the one-line comment syntax of the mode.

Currently, in PHP, when I comment out (using either comment-or-uncomment-region or comment-dwim)

This
Block of
Code

I get this:

/* 
 * This
 * Block of
 * Code
 */

But what I need is this:

// This
// Block of
// Code

I tried to (no, let me rephrase this: I spent nights trying every possible combination) use M-x customize-group RET comment, specifically the variables comment-multi-line and comment-style but to no avail.

Note that when I edit Javascript, js-mode does exactly that. How can I get this behaviour in all modes?

like image 809
yPhil Avatar asked Dec 20 '13 12:12

yPhil


1 Answers

Try this:

(add-hook 'php-mode-hook 'my-php-mode-hook)
(defun my-php-mode-hook ()
  (set (make-local-variable 'comment-start) "//")
  (set (make-local-variable 'comment-padding) " ")
  (set (make-local-variable 'comment-end) "")
  (set (make-local-variable 'comment-style) 'indent))

In Emacs 24.3 you can use the form (setq-local comment-start "//") instead.

like image 199
phils Avatar answered Sep 30 '22 17:09

phils