From: Drew Adams <drew.adams@oracle.com>
To: Emanuel Berg <embe8573@student.uu.se>, help-gnu-emacs@gnu.org
Subject: RE: one key-press to comment out lines of code?
Date: Wed, 30 Apr 2014 13:54:45 -0700 (PDT) [thread overview]
Message-ID: <9477b732-7c9e-4a16-b11c-6b09e359cf8e@default> (raw)
In-Reply-To: <874n1ak5b9.fsf@nl106-137-194.student.uu.se>
> > I prefer to use `comment-region', which I bind to `C-x ;'.
Sorry, I mistyped the key I use. I meant `C-x C-;' - hold down
Control and hit `x ;' (or `u x ;', to uncomment).
> Why not use M-; for that as well? C-SPC, move point, M-; last.
Read what I wrote. I already said why I use `comment-region'.
You are free to use anything you like. And I started out by
saying that someone else would no doubt laud `M-;'.
`comment-region' lets you uncomment or comment (any number of
comment chars), whatever is selected (whether some or all of
the selected text is already commented or not).
I do also use `M-;', but only for what it has always done,
even when it was bound to `indent-for-comment': add/indent
inline comments. I really have no use for the rest of what
it tries to do. YMMV.
> > To uncomment, provide a plain prefix arg (`C-u'):
> > `C-u C-x ;'. To comment using N comment chars, use a
> > numeric prefix arg: `C-3 C-x ;'. (And you can use
> > that to nest commented text.)
>
> Yeah, but again, I think it is much simpler to C-SPC,
> move point, and M-; (if the region is commented) to
> uncomment.
You need to select the text in any case. I don't consider
that `M-;' is "much simpler" than `C-x C-;'. But you can
of course bind `comment-region' to whatever key you like.
Or not.
But no; sorry. `comment-dwim' simply does NOT do what
`comment-region' does.
(define-key isearch-mode-map (kbd "C-y C-a") 'foo)
;;; (define-key isearch-mode-map "\C-a" nil)
Select both lines as the region. Each of `M-;' and `C-x C-;'
produce this result:
;; (define-key isearch-mode-map (kbd "C-y C-a") 'foo)
;; ;;; (define-key isearch-mode-map "\C-a" nil)
But `C-u C-x C-;' produces this:
(define-key isearch-mode-map (kbd "C-y C-a") 'foo)
(define-key isearch-mode-map "\C-a" nil)
It uncomments one level. It always uncomments.
`M-;' cannot uncomment the commented lines in the region
when there are also uncommented lines.
Now select only the second line, the one which is commented:
;;; (define-key isearch-mode-map "\C-a" nil)
Now `M-;' uncomments it. `C-u C-x C-;' uncomments it too
(it does so always). `C-2 M-;' removes two of the semicolons.
`C-x C-;' still comments here, nesting the existing
block comment, just as in the case where the selected
text contained an uncommented line:
;; ;;; (define-key isearch-mode-map "\C-a" nil)
And it uncomments just one level of nesting, giving this:
;;; (define-key isearch-mode-map "\C-a" nil)
from this:
;; ;;; (define-key isearch-mode-map "\C-a" nil)
`comment-region' does not try to outsmart you: a plain
`C-u' uncomments one level. No prefix arg comments one level.
A positive prefix arg comments using that many `;'. A negative
prefix arg uncomments that many `;'.
It is especially the behavior wrt nesting and unnesting of
block comments that I appreciate. I often comment out a
block of text and then comment out some more, surrounding
text. Nesting of the commented lines means I can quickly
unnest one level, to return to the previous state.
(And yes, like the other Emacs comment functions,
`comment-region' is not limited to Lisp code. `M-x' is not
special in this regard.)
> So for all three cases: M-;.
No - see above.
Most Emacs users already know about `M-;'. Far fewer know
about `comment-region', which is not bound to a key by default.
Which is why I mentioned it - in case it helps someone.
I assumed that there are users like you who take a quick
look (or no look) at `comment-region' and figure that
`comment-dwim' does everything it does - everything they need.
A DWIM command is often limited this way; it is a compromise.
Whether `comment-dwim' is your cup of tea is up to you.
next prev parent reply other threads:[~2014-04-30 20:54 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-30 16:14 one key-press to comment out lines of code? Sharon Kimble
2014-04-30 16:22 ` Drew Adams
2014-04-30 17:09 ` Michael Heerdegen
2014-04-30 17:18 ` Michael Heerdegen
2014-04-30 16:27 ` Dale Snell
2014-04-30 17:38 ` Andreas Röhler
2014-04-30 19:02 ` Stefan Monnier
[not found] ` <mailman.391.1398874951.1147.help-gnu-emacs@gnu.org>
2014-04-30 19:03 ` Emanuel Berg
2014-04-30 20:54 ` Drew Adams [this message]
2014-04-30 21:06 ` Drew Adams
[not found] ` <mailman.414.1398891311.1147.help-gnu-emacs@gnu.org>
2014-05-01 13:54 ` Emanuel Berg
2014-05-01 14:39 ` Drew Adams
[not found] ` <mailman.441.1398955214.1147.help-gnu-emacs@gnu.org>
2014-05-01 15:03 ` Emanuel Berg
[not found] ` <mailman.392.1398875301.1147.help-gnu-emacs@gnu.org>
2014-04-30 19:10 ` Emanuel Berg
2014-04-30 20:09 ` Joost Kremers
2014-05-01 14:15 ` Emanuel Berg
2014-05-01 14:31 ` Joost Kremers
2014-04-30 21:12 ` Dale Snell
[not found] ` <mailman.417.1398892388.1147.help-gnu-emacs@gnu.org>
2014-05-01 14:05 ` Emanuel Berg
2014-05-01 22:54 ` Dale Snell
[not found] ` <mailman.469.1398984898.1147.help-gnu-emacs@gnu.org>
2014-05-01 23:42 ` Emanuel Berg
2014-05-02 0:11 ` Joost Kremers
2014-05-02 1:23 ` Emanuel Berg
2014-05-03 2:01 ` Emanuel Berg
2014-05-03 2:43 ` Emanuel Berg
2014-05-02 17:42 ` Dale Snell
[not found] ` <mailman.520.1399052563.1147.help-gnu-emacs@gnu.org>
2014-05-02 19:59 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9477b732-7c9e-4a16-b11c-6b09e359cf8e@default \
--to=drew.adams@oracle.com \
--cc=embe8573@student.uu.se \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.