13

Emacs generally comments regions well, but sometimes I wish I could change the string it prepends to lines. For example, in LaTeX, I would like to have a commented region only use a single % per line instead of two, i.e.,

% First line of commented region
% Second line of commented region
% Third line of commented region

Instead of

%% First line of commented region
%% Second line of commented region
%% Third line of commented region

I assume one could just set some variable for latex-mode in .emacs.el, but I could not find the variable name. Any ideas?

Drew
  • 75,699
  • 9
  • 109
  • 225
karlo
  • 231
  • 2
  • 4

1 Answers1

12

comment-add:

comment-add is a variable defined in newcomment.el. Its value is 1 Local in buffer .emacs; global value is 0

Documentation: How many more comment chars should be inserted by comment-region. This determines the default value of the numeric argument of comment-region. The plain comment style doubles this value.

This should generally stay 0, except for a few modes like Lisp where it is 1 so that regions are commented with two or three semi-colons.

Use:

(add-hook 'latex-mode-hook (lambda () (setq-local comment-add 0)))

to get the behavior you want.

I found this variable with helm-apropos. You can also search with the built-in command M-x apropos-variable.

nanny
  • 5,704
  • 18
  • 38
  • 4
    A bit more context: the string inserted to start a comment is defined by the variable `comment-start`, which for latex mode is `%`. As @nanny points out the variable `command-add` is used in modes like lisp and latex where the default behavior is to insert the `comment-start` string multiple times. – glucas Feb 05 '15 at 18:02