4

In my file containing LaTeX source code, I wish to comment out all the lines matching a pattern (specifically, the pattern that I am interested in is \draw, including the \ character).

I have magic mode enabled. I think a potential solution would be to use the global command i.e. something along the lines of

:g/\\draw/command

I am unsure of the command that achieves my task. I would like to seek your help through either of the methods below:

  1. I have Tim Pope's commentary plugin which provides the gc mapping, which works in normal mode. However, this does not work in ex mode.

  2. Maybe use the normal mode I command and use the comment character for LaTeX (the percent % character) on those lines matched by the global command.

Please let me know if a simpler alternative exists in addition to the ones mentioned above, I am of course glad to learn about it too!

2 Answers 2

3

You can use g/\\draw/s/^/%/ to insert a percent sign at the start of every line containing the text \draw. s/^/%/ means replace the empty string at the start of the line with a percent sign.

If you want to put the percent sign after any leading white space---which is more similar to how the normal command I works---, use g/\\draw/s/^\s*/&%/ instead. s/^\s*/&%/ means replace a possibly empty string of whitespace characters at the start of the line with the string itself, followed by a percent sign.

4

Try using :g/\\draw/norm gcc

From :help norm:

Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line. {commands} are executed like they are typed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.