Editor to Auto-Comment

R

richtmyer

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
 
S

SG

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

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

#if 0
...
#endif

Cheers!
SG
 
J

James Kanze

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.)
 
M

Michael DOUBEZ

James said:
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.
 
P

Phlip

SG said:
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!
 
P

Phlip

blargg said:
(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.
 
J

James Kanze

James said:
(e-mail address removed) 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.
IIRC because multiple insertion works only in block mode.

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).
 
J

James Kanze

James said:
(e-mail address removed) 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.

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.)
 
M

Michael DOUBEZ

Jeff said:
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?

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()
 
F

Frank Steinmetzger

(e-mail address removed) 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.
 
F

Frank Steinmetzger

(e-mail address removed) 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)
 
A

acehreli

 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
 
B

Bo Persson

Frank said:
(e-mail address removed) schrob:


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
 
J

JussiJ

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top