Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Editor to Auto-Comment

0 views
Skip to first unread message

rich...@cox.net

unread,
Apr 23, 2009, 9:51:55 PM4/23/09
to
I am looking for an editor (hopefully free or cheap) that will
let me to select a number of source code lines and easily prepend
the 2 comment chars "//" to the selected lines. Anybody? -- thanks

Phlip

unread,
Apr 23, 2009, 10:08:07 PM4/23/09
to
> I am looking for an editor (hopefully free or cheap) that will
> let me to select a number of source code lines and easily prepend
> the 2 comment chars "//" to the selected lines. Anybody? -- thanks

A> scite
B> what platform?
C> you will get the best answer on a newsgroup covering editors
for your platform. This newsgroup likes the really hard C++
questions, and we get bored if the question is about some dumb editor!


Jeff Schwab

unread,
Apr 23, 2009, 11:55:03 PM4/23/09
to

Try vim. Put the cursor on the first line you want to comment, press ^v
(that's control + v) to begin rectangular selection, move the cursor
down to the last line you want to comment, and press I//^[ (where ^[ is
the escape key). Or, if you really just want the // to be the first two
characters on each line in a range: Select the first line with V
(that's shift + v), move to the last line, and enter :s,^,// (but be
aware that the text vim shows you in the modeline will actually say
:'<,'>s,^,//).

Btw, comp.editors would be a much better forum for this kind of question.

SG

unread,
Apr 24, 2009, 2:04:46 AM4/24/09
to

This is not what you asked for but it might be useful, too:

#if 0
...
#endif

Cheers!
SG

James Kanze

unread,
Apr 24, 2009, 4:48:30 AM4/24/09
to
On Apr 24, 5:55 am, Jeff Schwab <j...@schwabcenter.com> wrote:

> richtm...@cox.net wrote:
> > I am looking for an editor (hopefully free or cheap) that
> > will let me to select a number of source code lines and
> > easily prepend the 2 comment chars "//" to the selected
> > lines. Anybody?
>
> Try vim. Put the cursor on the first line you want to
> comment, press ^v (that's control + v) to begin rectangular
> selection, move the cursor down to the last line you want to
> comment, and press I//^[ (where ^[ is the escape key). Or, if
> you really just want the // to be the first two characters on
> each line in a range: Select the first line with V (that's
> shift + v), move to the last line, and enter :s,^,// (but be
> aware that the text vim shows you in the modeline will
> actually say :'<,'>s,^,//).

Or you can pipe the marked zone through any sort of script or
program you want; I use a program I wrote myself (comment),
which handles formatting as well, for example. Emacs also
supports this, although I forget the commands. (I'm using vim
now.)

(Also, is there any particular reason for using the rectangular
selection? I always use V, to select lines.)

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Michael DOUBEZ

unread,
Apr 24, 2009, 5:02:39 AM4/24/09
to
James Kanze wrote:
> On Apr 24, 5:55 am, Jeff Schwab <j...@schwabcenter.com> wrote:
>> richtm...@cox.net wrote:
>>> I am looking for an editor (hopefully free or cheap) that
>>> will let me to select a number of source code lines and
>>> easily prepend the 2 comment chars "//" to the selected
>>> lines. Anybody?
>> Try vim. Put the cursor on the first line you want to
>> comment, press ^v (that's control + v) to begin rectangular
>> selection, move the cursor down to the last line you want to
>> comment, and press I//^[ (where ^[ is the escape key). [snip]

>
> Or you can pipe the marked zone through any sort of script or
> program you want; I use a program I wrote myself (comment),
> which handles formatting as well, for example. Emacs also
> supports this, although I forget the commands. (I'm using vim
> now.)

Myself, I use a vimtip:

au FileType vim let b:comment_leader = '" '
au FileType c,cpp,h,hpp let b:comment_leader = '// '
au FileType sh,make let b:comment_leader = '# '
noremap <silent> ,c :<C-B>sil
<C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:noh<CR>
noremap <silent> ,u :<C-B>sil
<C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:noh<CR>

And then, on a selection or on a line: ,c to comments the line and ,u
uncomment it.

> (Also, is there any particular reason for using the rectangular
> selection? I always use V, to select lines.)

IIRC because multiple insertion works only in block mode.

--
Michael

Phlip

unread,
Apr 24, 2009, 6:01:20 AM4/24/09
to
SG wrote:

> This is not what you asked for but it might be useful, too:
>
> #if 0
> ...
> #endif

Diiing!

Always remember, folks, if someone posts an off-topic question, replying
correctly and on-topic is in everyone's best interests!

Jeff Schwab

unread,
Apr 24, 2009, 11:19:27 AM4/24/09
to
James Kanze wrote:
> On Apr 24, 5:55 am, Jeff Schwab <j...@schwabcenter.com> wrote:
>> richtm...@cox.net wrote:
>>> I am looking for an editor (hopefully free or cheap) that
>>> will let me to select a number of source code lines and
>>> easily prepend the 2 comment chars "//" to the selected
>>> lines. Anybody?
>> Try vim. Put the cursor on the first line you want to
>> comment, press ^v (that's control + v) to begin rectangular
>> selection, move the cursor down to the last line you want to
>> comment, and press I//^[ (where ^[ is the escape key). Or, if
>> you really just want the // to be the first two characters on
>> each line in a range: Select the first line with V (that's
>> shift + v), move to the last line, and enter :s,^,// (but be
>> aware that the text vim shows you in the modeline will
>> actually say :'<,'>s,^,//).
>
> Or you can pipe the marked zone through any sort of script or
> program you want; I use a program I wrote myself (comment),
> which handles formatting as well, for example. Emacs also
> supports this, although I forget the commands. (I'm using vim
> now.)
>
> (Also, is there any particular reason for using the rectangular
> selection? I always use V, to select lines.)

There are a couple of reasons:

(1) ^v lets you insert the comment leaders at an arbitrary column, which
is tougher to do with regular expressions.

(2) The rectangular selection affects only lines that already have text
in the given column. (Of course, this may or may not be what you want
in a given situation.)

Here's a typical example of code commented with V:

// const_iterator begin() const {
// return m_words.begin();
// }
//
// const_iterator end() const {
// return m_words.end();
// }

And here it is with ^v:

// const_iterator begin() const {
// return m_words.begin();
// }

// const_iterator end() const {
// return m_words.end();
// }

Jeff Schwab

unread,
Apr 24, 2009, 11:30:46 AM4/24/09
to
Michael DOUBEZ wrote:
> James Kanze wrote:
>> On Apr 24, 5:55 am, Jeff Schwab <j...@schwabcenter.com> wrote:
>>> richtm...@cox.net wrote:
>>>> I am looking for an editor (hopefully free or cheap) that
>>>> will let me to select a number of source code lines and
>>>> easily prepend the 2 comment chars "//" to the selected
>>>> lines. Anybody?

> Myself, I use a vimtip:


>
> au FileType vim let b:comment_leader = '" '
> au FileType c,cpp,h,hpp let b:comment_leader = '// '
> au FileType sh,make let b:comment_leader = '# '
> noremap <silent> ,c :<C-B>sil
> <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:noh<CR>
> noremap <silent> ,u :<C-B>sil
> <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:noh<CR>

Fancy. :) Your vim foo clearly runs deeper than mine. What are <C-B>,
<C-E>, and <C-R> doing there? To me, those mean (by default) "scroll
back half a page," "scroll down one line," and "redo." I've also never
seen the =function() notation; any pointers?

blargg

unread,
Apr 24, 2009, 4:02:19 PM4/24/09
to

(I'm assuming you're being sarcastic above)

Often a person is trying to solve a problem with solution A, but unable to
fully apply the solution. Said person then asks for help in doing A, while
he would be just as satisfied receiving solution B which solves the
problem, and is easier to do. So when someone asks how to do A, and the
original problem seems evident or another solution B has virtually the
same effect as A, B will often be offered.

Phlip

unread,
Apr 24, 2009, 5:43:37 PM4/24/09
to
blargg wrote:

>> Always remember, folks, if someone posts an off-topic question, replying
>> correctly and on-topic is in everyone's best interests!
>
> (I'm assuming you're being sarcastic above)

Absolutely not. Many of the replies here "your question is off-topic go f---
yourself" should instead be opportunities to demonstrate ones C++ prowess
anyway.

> Often a person is trying to solve a problem with solution A, but unable to
> fully apply the solution. Said person then asks for help in doing A, while
> he would be just as satisfied receiving solution B which solves the
> problem, and is easier to do. So when someone asks how to do A, and the
> original problem seems evident or another solution B has virtually the
> same effect as A, B will often be offered.

Right.


James Kanze

unread,
Apr 25, 2009, 4:55:22 AM4/25/09
to

But what's wrong with line mode, and s:^:// : ?

Anyway, it occured to me after posting that I wasn't really sure
what the orginal poster was asking for. For commenting out a
block of code, I just use s:^:// : with a range or on a marked
block. I don't do it often enough to warrent special macros.
On the other hand, I write my comments in text mode, then pass
them through an external program (which even understands minimal
markup) to format them. A good editor should be able to handle
both jobs without any real difficulty---I use vim, but I know
that my collegues who use emacs do similar stuff with it (and in
the past, when I used emacs, so did I, but I've forgotten how
since then).

James Kanze

unread,
Apr 25, 2009, 4:59:48 AM4/25/09
to

I don't think I've ever needed that, but why not?

> (2) The rectangular selection affects only lines that already
> have text in the given column. (Of course, this may or may
> not be what you want in a given situation.)

That is cool. Less for commenting out blocks of code, than for
things like quoting in Usenet messages.

> Here's a typical example of code commented with V:

> // const_iterator begin() const {
> // return m_words.begin();
> // }
> //
> // const_iterator end() const {
> // return m_words.end();
> // }

> And here it is with ^v:

> // const_iterator begin() const {
> // return m_words.begin();
> // }

> // const_iterator end() const {
> // return m_words.end();
> // }

If I'm commenting out code, I prefer the former. If I'm dealing
with text that I want to convert into comments, the second may
be preferable, but I generally do this via an external program.
(Of course, history plays a role in these choices.)

Michael DOUBEZ

unread,
Apr 25, 2009, 6:14:04 PM4/25/09
to

Not in line mode: I am not a guru but IIRC <C-B>/<C-E> allows operating
on the range.

> I've also never seen the =function() notation; any pointers?

In line mode, <C-R> inserts the results of a command (in this case, the
escape of comment_leader).
For function 'escape':
http://vimdoc.sourceforge.net/htmldoc/eval.html#escape()

--
Michael

Frank Steinmetzger

unread,
May 1, 2009, 3:18:17 PM5/1/09
to
rich...@cox.net schrob:

> I am looking for an editor (hopefully free or cheap) that will
> let me to select a number of source code lines and easily prepend
> the 2 comment chars "//" to the selected lines. Anybody? -- thanks

All you need is a regexp-capable editor. Then select the lines you want to
change and replace "^" by "//" (without the ""). ^ means begin of a line.
--
Gruᅵ | Greetings | Qapla'
Der Mensch lebt nicht vom Brot allein, es muᅵ auch etwas Wurst bei sein.

Frank Steinmetzger

unread,
May 1, 2009, 3:20:57 PM5/1/09
to
rich...@cox.net schrob:

> I am looking for an editor (hopefully free or cheap) that will
> let me to select a number of source code lines and easily prepend
> the 2 comment chars "//" to the selected lines. Anybody? -- thanks

All you need is a regexp-capable editor. Then select the lines you want to


change and replace "^" by "//" (without the ""). ^ means begin of a line.

Since you didn't tell us what OS you're using, I can only suggest some
editors:
Windows: Notepad++
Linux: GEdit for gnome, KDE's editor component (found in kwrite, kate etc)

aceh...@gmail.com

unread,
May 1, 2009, 5:34:27 PM5/1/09
to
On Apr 24, 1:48 am, James Kanze <james.ka...@gmail.com> wrote:
> Emacs also supports this, although I forget the commands.  (I'm using vim
> now.)

For the sake of completeness: when in c-mode or c++-mode C-c C-c is
comment-region in Emacs. (Tsk! Tsk! ;) )

Ali

Bo Persson

unread,
May 2, 2009, 5:32:37 AM5/2/09
to
Frank Steinmetzger wrote:
> rich...@cox.net schrob:
>
>> I am looking for an editor (hopefully free or cheap) that will
>> let me to select a number of source code lines and easily prepend
>> the 2 comment chars "//" to the selected lines. Anybody? --
>> thanks
>
> All you need is a regexp-capable editor. Then select the lines you
> want to change and replace "^" by "//" (without the ""). ^ means
> begin of a line.
>
> Since you didn't tell us what OS you're using, I can only suggest
> some editors:
> Windows: Notepad++

An alternative for Windows: Visual Studio has this feature, even in
the Express edition.


> Linux: GEdit for gnome, KDE's editor component (found in kwrite,
> kate etc)


Bo Persson


JussiJ

unread,
May 8, 2009, 12:54:54 AM5/8/09
to

In the Zeus editor (on the Windows platform):

http://www.zeusedit.com

You do this by highlighting any number of lines and
then use the Macros, Add Line Comment menu to add the
line comments.

To remove the line comments you highlight the lines and
then run the Macros, Remove Line Comment menu.

NOTE: Zeus is shareware.

0 new messages