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


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

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

b) Maybe use the normal mode I command and use the comment character for latex, i.e. the % character on those lines matched by the global command

If a simpler alternative(s) exist in addition to the ones mentioned above, I am of course glad to learn about it too!


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.


Try using :g/\\draw/norm gc

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, privacy policy and cookie policy

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