yPhil
yPhil

Reputation: 8357

Emacs: one-line comments every time, everywhere

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?

Upvotes: 6

Views: 382

Answers (1)

phils
phils

Reputation: 73246

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.

Upvotes: 4

Related Questions