# Query replace and commenting out

I want to comment out all environments say myenv in a tex-file. I can find them with the regexp:

\\begin{myenv}$$.\|\n$$.*?\\end{myenv}

(instead of \n I had do type C-q C-j).

But what would be the correct regexp for replacing? How to reverse it, i.e. uncomment all environments myenv?

• Can be done in emacs. But I wonder whether it would be better to use the LaTeX-package "comment" for this purpose. With this package you can set \excludecomment{myenv} in the document preamble. Your regexp is interesting in the aspect that you only allow one newline behind \\begin{myenv} and the backslash for the or-operator is missing. I would expect \\begin{myenv}$$.\|n$$*\\end{myenv} as regexp. – Tobias Mar 22 '16 at 11:59
• I just wanted to add that you have to type C-q C-j for the newline character n in my last comment. You have already mentioned that in your question. – Tobias Mar 22 '16 at 12:09
• I think a better solution would be to write a loop to find all myenv environments and applying comment-region to each match. Or apply uncomment-region to undo it. You should be able to use the same regexp for both cases. (Sorry, don't have the time to flesh this out to a proper answer.) – Harald Hanche-Olsen Mar 22 '16 at 12:23

I think, for what you are doing, the comment by Tobias provides a superior solution. And this question gives quite a few options for commenting out chunks of code. That said, I'll assume this is an issue with using emacs, rather than latex.

Make your entire regex match a group $$regex$$, then the regex to replace it will be similar to \\iffalse \1 \\fi.

An easier solution would be to write a keyboard macro that finds the begin\end environment commands and inserts the comment code there.

M-x re-search-forward RET

MY_REGEXP RET

followed by M-x comment-dwim RET

;;;

comment-dwim is at M-;

After re-search-forward the region should be visible.

If not C-x C-x resp. M-xexchange-point-and-markRET