# Block commenting-out

**URL:** <https://www.ruby-forum.com/t/block-commenting-out/64857>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857 "2006-08-15T14:19:54Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/1 "2006-08-15T14:19:54Z")

</div>

hi,

can anyone tell me how to bulk comment-out lines of code rather than  
#'ing each line?

thanks

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/2 "2006-08-15T14:19:54Z")

</div>

On 14.08.2006 15:51, newyorkdolluk wrote:

> hi,
> 
> can anyone tell me how to bulk comment-out lines of code rather than  
> #'ing each line?
> 
> thanks

15:56:48 [~]: ruby \<\<XXX

> puts 1  
> =begin  
> foo  
> adha sdjas dha dad +asdp+adp+ad+  
> =end  
> puts 2  
> XXX  
> 1  
> 2

In short, use “=begin” and “=end”.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/3 "2006-08-15T14:19:54Z")

</div>

On 8/14/06, newyorkdolluk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi,
> 
> can anyone tell me how to bulk comment-out lines of code rather than  
> #'ing each line?
> 
> thanks

=begin

your code

=end

I have to know, how does a New York Doll end up in the UK?

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/4 "2006-08-15T14:20:56Z")

</div>

newyorkdolluk wrote:

> hi,
> 
> can anyone tell me how to bulk comment-out lines of code rather than  
> #'ing each line?
> 
> thanks

Komodo can do block commenting out, prepending “#”. Scite prepends  
“##” so you know that is was done in bulk fashion. Your editor must  
hvae something of the sort to block comment and uncomment.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/5 "2006-08-15T14:19:54Z")

</div>

On 14/08/06, newyorkdolluk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi,
> 
> can anyone tell me how to bulk comment-out lines of code rather than  
> #'ing each line?
> 
> thanks

=begin  
This is a comment  
on multiple lines  
=end

Farrel

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/6 "2006-08-15T14:20:58Z")

</div>

On Tue, Aug 15, 2006 at 03:42:20PM +0900, Gene T. wrote:

> “##” so you know that is was done in bulk fashion. Your editor must  
> hvae something of the sort to block comment and uncomment.

Vim could, of course, do it via a substitution iterating over a number  
of lines indicated by a prepended number. I’m sure EMACS (Esc Meta Alt  
Ctrl Shift) could do something similar, but with four times as many keys  
involved.

What, me biased? Never.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/7 "2006-08-15T14:20:59Z")

</div>

If you highlight the lines you want commented out in Textmate, then  
press Command + /, it will prepend # to every line. It works quite  
well. Plus, if you highlight and Command + / again, it’ll remove the  
#s.

😉

M.T.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/8 "2006-08-15T14:21:01Z")

</div>

With Ruby In Steel you just mark a block and click a button to comment  
or  
uncomment 😉

best wishes  
Huw C.

[http://www.sapphiresteel.com](http://www.sapphiresteel.com)  
Ruby P.ming In Visual Studio 2005

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/9 "2006-08-15T14:21:01Z")

</div>

Huw C. wrote:

> With Ruby In Steel you just mark a block and click a button to comment or  
> uncomment 😉

But… but… that requires taking your hand off the keyboard!

Heresy!

🙂

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/10 "2006-08-15T14:21:01Z")

</div>

On 15-aug-2006, at 11:35, Huw C. wrote:

> With Ruby In Steel you just mark a block and click a button to  
> comment or  
> uncomment 😉

Click a button? Isn’t that what most people try to avoid when editing  
files? 🙂

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/11 "2006-08-15T14:21:02Z")

</div>

Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > Komodo can do block commenting out, prepending “#”. Scite prepends  
> > “##” so you know that is was done in bulk fashion. Your editor must  
> > hvae something of the sort to block comment and uncomment.
> 
> Vim could, of course, do it via a substitution iterating over a number  
> of lines indicated by a prepended number. I’m sure EMACS (Esc Meta Alt  
> Ctrl Shift) could do something similar, but with four times as many keys  
> involved.
> 
> What, me biased? Never.

M-x comment-region

Bind to every key you like (I don’t use it often).

C-u M-x comment-region (or M-x uncomment-region) will uncomment.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/12 "2006-08-15T14:21:02Z")

</div>

On Aug 15, 2006, at 6:42 AM, Christian N. wrote:

> > What, me biased? Never.
> 
> M-x comment-region
> 
> Bind to every key you like (I don’t use it often).
> 
> C-u M-x comment-region (or M-x uncomment-region) will uncomment.

Try M-; (comment-dwim).

– Daniel

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/13 "2006-08-15T17:53:57Z")

</div>

Daniel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > > involved.
> > > 
> > > What, me biased? Never.
> > 
> > M-x comment-region
> > 
> > Bind to every key you like (I don’t use it often).
> > 
> > C-u M-x comment-region (or M-x uncomment-region) will uncomment.
> 
> Try M-; (comment-dwim).

I knew there was an easier way, but M-; requires using transient-mark  
mode (shudder). Well, not too bad in Emacs 22, C-SPC, C-SPC.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/14 "2006-08-15T14:21:04Z")

</div>

“Alex Y.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> Huw C. wrote:
> 
> > With Ruby In Steel you just mark a block and click a button to comment or  
> > uncomment 😉
> 
> But… but… that requires taking your hand off the keyboard!
> 
> Heresy!

Sorry, my mind must have been warped by doing too much Smalltalk  
programming  
lately. You can, of course, use the keyboard: CTRL+E,C to comment,  
CTRL+E, U  
to uncomment, or you can define your own favourite keystrokes. 🙂

best wishes  
Huw C.

[http://www.sapphiresteel.com](http://www.sapphiresteel.com)  
Ruby P.ming In Visual Studio 2005

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/15 "2006-08-15T20:00:16Z")

</div>

On Wed, Aug 16, 2006 at 12:53:24AM +0900, Christian N. wrote:

> > > > Ctrl Shift) could do something similar, but with four times as
> > 
> > Try M-; (comment-dwim).
> 
> I knew there was an easier way, but M-; requires using transient-mark  
> mode (shudder). Well, not too bad in Emacs 22, C-SPC, C-SPC.

What do “comment-region” and “comment-dwim” mean in practical terms in  
this case?

How many modes are there to emacs?

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/16 "2006-08-15T20:03:16Z")

</div>

On Tue, Aug 15, 2006 at 08:35:10PM +0900, Huw C. wrote:

> Sorry, my mind must have been warped by doing too much Smalltalk programming  
> lately. You can, of course, use the keyboard: CTRL+E,C to comment, CTRL+E, U  
> to uncomment, or you can define your own favourite keystrokes. 🙂

. . . and to “mark a block” in preparation for this, I gather you have  
to use arrow keys and the Shift key, or take a hand off the keyboard.  
Yes?

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/17 "2006-08-15T20:27:21Z")

</div>

Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> What do “comment-region” and “comment-dwim” mean in practical terms in  
> this case?

comment-region:

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.  
By default, comments start at the left margin, are terminated on each  
line,  
even for syntax in which newline does not end the comment and blank  
lines  
do not get comments. This can be changed with `comment-style’.

comment-dwim:

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

> How many modes are there to emacs?

492 matches for “-mode$” in buffer: _Apropos_

🙂

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 15, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/18 "2006-08-15T20:51:32Z")

</div>

On Wed, Aug 16, 2006 at 03:25:49AM +0900, Christian N. wrote:

> If arg is negative, delete that many comment characters instead.  
> Else, if the current line is empty, 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.

Thanks. That satisfies my curiosity.

> > How many modes are there to emacs?
> 
> 492 matches for “-mode$” in buffer: _Apropos_

Holy crap.

---

<div class="post-metadata">

**Author:** ![newyorkdolluk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@newyorkdolluk](https://www.ruby-forum.com/u/newyorkdolluk)\
**Post date:** [August 16, 2006, 8:52am UTC](https://www.ruby-forum.com/t/block-commenting-out/64857/19 "2006-08-16T08:52:42Z")

</div>

In vim you could put this in your .vimrc file:  
map ,# :s/^/#/

mark the block you want to comment and then hit ,# (or whatever else you  
want to map it to), and its done.

Cheers.
