How to uncomment blocks code in emacs - 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?

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

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.

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.

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.

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.

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/

Related

Binding a key to a command and automatically using the (interactive) default values for its interactive arguments

I would like F5 to switch to the most recently used buffer. This functionality is accomplished by running M-x icicle-buffer and then hitting enter to not specify the buffer I want to switch to -- (the default behavior of icicle is to switch to the most recent buffer.)
I have tried editing my .emacs thus:
(defun most-recent-buffer-please ()
(interactive)
(icicle-buffer " "))
(global-set-key [(f5)] 'most-recent-buffer-please)
but when I evaluate this lisp, and then hit F5, I get an error that starts with Wrong number of arguments followed by a lot of gibberish characters. What am I doing wrong?
A function can have mandatory and/or optional arguments, or no arguments at all. When writing elisp, it is usually a good idea to find out what arguments are available for certain functions by typing M-x describe-function RET [name of the function] RET. Drew (the author of Icicles) has indicated in his comment underneath the original question that the function icicle-buffer is not designed to be used in conjunction with any arguments -- therefore, adding " " causes the error message that the original poster experienced.
To switch to the previous buffer, Emacs already has a built-in function called previous-buffer. Since the original poster has indicated a preference for the f5 key, the following is an example of how to configure that keyboard shortcut so that it triggers previous-buffer -- brackets are sufficient and the parentheses used by the original poster around f5 can be omitted:
(global-set-key [f5] 'previous-buffer)

Redefine M-z from zap-to-char

Clicking Ctrl-h k Alt-z gives this:
M-z runs the command zap-to-char, which is an interactive compiled
Lisp function in `simple.el'.
It is bound to M-z.
(zap-to-char ARG CHAR)
Kill up to and including ARGth occurrence of CHAR. Case is ignored if
`case-fold-search' is non-nil in the current buffer. Goes backward if
ARG is negative; error if CHAR not found.
I never use this function, but the keys are located in a very good position. So I wanted to redefine this sequence. But I couldn't. Even when I execute this command in .emacs:
(global-set-key (kbd "\M-z") 'backward-delete-word) M-z is still bound to zap-to-char.
Approach, suggested here, didn't help:
Emacs can't reset Ctrl-d key behaviour
You don't need a \ in 'kbd'. Also, I think you're looking for backward-kill-word:
(global-set-key (kbd "M-z") 'backward-kill-word)
EDIT: I didn't notice that the page you linked to defines backward-delete-word. If you have that function defined, you can use it instead of backward-kill-word here.

Is there a way to comment out a large chunk of code?

I'm trying to comment out a large piece of code in a program but it's too difficult to do it manually. Is there a program to do it for me? (making such program doesn't seem hard anyway)
Also, I'm using emacs. Does emacs have such a function? (or an emacs Lisp file at least?)
In emacs, you can use M-x comment-region and M-x uncomment-region.
The comment-region method bound to key M-;. You can select region using marker and then use M-; to comment/uncomment.
You can mark at the beginning of the region that you want to comment out, move to the end of the region, then do C-c C-c or M-x comment-region.
I bind comment-region to C-M-;. And remember that C-u uncomments the commented region.
E.g. C-u 2 C-M-; comments the region with ;; (in Lisp). Then C-u C-M-; uncomments the region. (The region must be active in both cases.)

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.

Emacs C-h c doesn't seem to work for chords 3 combinations long?

I'm trying to use C-h c in emacs to figure out what a key combination is bound to. The combination is C-u C-c C-q, which realigns tags in org-mode. However, Emacs just tries to look up C-u C-c and then fails. What am I doing wrong? I realize I could easily look at the orgmode source or something to figure this out, but for future reference what would I do to figure out what function something like this is bound to?
Edit: OK, so it's actually C-u followed by C-c C-q, and according to emacs this is what that combination is bound to:
(org-set-tags-command &optional arg just-align)
Call the set-tags command for the current entry.
So what exactly does it mean to give this command the argument 4?
Oh, just to give an explanation: I'm trying to start learning emacs-lisp and customization and one of the things I wanted to do was to have this command added to the before-save-hook so that when I save an org file, the tags get automatically aligned.
Final edit: I figured out why this command behaves as it does; given the prefix argument it changes its behavior. How can I set the prefix argument when calling the function in elisp?
It's not a general problem with combinations that are three keys long: For example, C-h c ESC ESC ESC (keyboard-escape-quit) or C-h c C-x r t (string-rectangle) both work fine.
When I try C-h c C-u C-c C-q in org-mode, the command interrupts after C-u and shows:
C-u runs the command universal-argument
in the minibuffer, which is correct. So, in fact, "C-u C-c C-q" is not a command, it's the command "C-c C-q" (org-table-wrap-region) started with an additional argument (4 -- see C-h k C-u for an explanation).