11

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?

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.

  • 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 5 '15 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.