SlickEdit Product Discussion > SlickEdit User Macros

Remove c++ line comments from selection


This macro removes all line comments (//) from the selected text. If the line is only a comment, it removes the entire line.

On a side note, in my current implementation of this macro, it works when I manually select a block of text, but it doesn't seem to work when I use select all. Does anyone know why that might be?

Maybe the select all problem is because the cursor is at the end of the file.  Try adding a begin_select() call at the start to move the cursor to the start of the selection.

On my machine, a line containing only a comment doesn't actually get deleted - but the comment does get deleted, leaving a blank line.  I'm using Windows 11.

If the // is immediately preceded by a non whitespace character the comment doesn't get deleted  - I don't know why this is.  Also it doesn't seem to allow for // in a string.

I have a couple of suggestions.  If I record a macro to search for any character (search string is set to . and reg expression enabled) and select colour coding of "comment" it finds the start of the next comment - if it's not already in a comment.  If I record a macro doing this I get the following

_command last_recorded_macro() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
   if (find('.',"LIPCC,,")) stop();

The search function you're using can also search for comments.

There's also a macro you can call to tell if the cursor is in a comment
and it can distinguish between block comment and line comment.
I notice that Ctrl Z undo manages to undo all changes in one go with your current method.  If you change to some manually looping code, Ctrl Z might undo one by one - I don't know.


[0] Message Index

Go to full version