Stephen
Stephen

Reputation: 147

How to uncomment blocks code in emacs

I am using emacs with the major mode "Java/L Abbrev" activated. When I type M-x comment-region or M-x uncomment-region the desired effects happen in the editor. But I am getting tired of typing this out every time.

I have found that I can type C-c C-c and comment a region. I want to find a similiar way to uncomment a region. I go to the emacs docs:

https://www.gnu.org/software/emacs/manual/html_node/ccmode/Comment-Commands.html

And it says to give the C-c C-c command a negative argument to uncomment lines. How do I do this? or is there a better way?

Upvotes: 13

Views: 7898

Answers (6)

Kind Stranger
Kind Stranger

Reputation: 1761

comment-dwim (DWIM stands for "Do What I Mean") is bound to M-; by default and works differently depending on whether or not the region is active (and sometimes what mode you're in)

From the emacs help page for comment-dwim:

comment-dwim is an interactive compiled Lisp function in
‘newcomment.el’.

It is bound to M-;.

(comment-dwim ARG)

Call the comment command you want (Do What I Mean).
If the region is active and ‘transient-mark-mode’ is on, call
‘comment-region’ (unless it only consists of comments, in which
case it calls ‘uncomment-region’).
Else, if the current line is empty, call ‘comment-insert-comment-function’
if it is defined, otherwise insert a comment and indent it.
Else if a prefix ARG is specified, call ‘comment-kill’.
Else, call ‘comment-indent’.
You can configure ‘comment-style’ to change the way regions are commented.

Upvotes: 4

vineeshvs
vineeshvs

Reputation: 565

  • First, select the region

  • To comment, use

    ALT + x comment-region

  • To un-comment, use

    ALT + x uncomment-region

Credits: https://www.reddit.com/r/emacs/comments/1kklgl/command_to_uncomment_entire_comment_block/

Upvotes: -1

JackRed
JackRed

Reputation: 1204

Why not mapping the uncomment-region command to a key? It's not really what you were asking for (previous answer are better for this) but it's a way to stop typing M-x uncomment-region every time

like this (with the key binding you want)

(global-set-key (kbd "C-c C-u") 'uncomment-region)

Documentation about key binding can be found here:
Commands for Binding Keys
Customizing Key Bindings

Here is a map of command:
Map of command

You can use C-h k (M-x describe-key) to show what command is bind to a particular key (so you're sure to not erase it) and C-h f (M-x describe-function) will show you a description of the function + its binding.

Upvotes: 1

Drew
Drew

Reputation: 30701

Your question is how to use C-c C-c to uncomment the region.

@AaronHarris answered your question about using a negative prefix arg.

But I think you misread the doc of comment-region (which CC mode binds to C-c C-). It does not uncomment the region. It deletes a certain number of comment characters.

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.el.

It is bound to menu-bar edit region comment-region.

(comment-region BEG END &optional ARG)

Comment or uncomment each line in the region.

With just C-u prefix arg, uncomment each line in region BEG .. END.

Numeric prefix ARG means use ARG comment characters.

If ARG is negative, delete that many comment characters instead.

The strings used as comment starts are built from comment-start and comment-padding; the strings used as comment ends are built from comment-end and comment-padding.

By default, the comment-start markers are inserted at the current indentation of the region, and comments are terminated on each line (even for syntaxes in which newline does not end the comment and blank lines do not get comments). This can be changed with comment-style.

So the answer is to use C-u C-c C-c.

And FWIW, comment-region is much better than M-; (comment-dwim) for commenting and uncommenting the region. It lets you nest and unnest comment blocks any number of levels.

Upvotes: 4

varro
varro

Reputation: 2482

Please try M-;, which is bound by default to comment-dwim. I think this should do what you want.

Upvotes: 14

Aaron Harris
Aaron Harris

Reputation: 916

TLDR: Use C-- C-c C-c; i.e., prefix your command with "control-hyphen"

To give a negative argument to a command, you need to call either the negative-argument command or the universal-argument command, supplying a negative argument. (Try C-h f for more information on these.)

The negative-argument command is bound to keys C--, M--, and C-M--, so all of these will work as prefixes; generally, you'll use the one that's most convenient to type for any given command.

The universal-argument command is bound to C-u and accepts its argument immediately after that, so you can also do C-u -, optionally followed by zero or more digits (e.g., C-u - 5 3 9); that one is overkill here, but good to know about.

Finally, here is the section of the Emacs manual that discusses this topic.

Upvotes: 2

Related Questions