Karl Yngve Lervåg
Karl Yngve Lervåg

Reputation: 1715

To comment out matches in Vim - independent on comment leader?

I want to comment out lines in some code I have. I have different kinds of codes, and they use different comment leaders. E.g. in latex: '%', in Fortran 90: '!' and in python: '#'. I want to do a substitute command that looks something like this:

:g/<search-string>/s/^/<add-comment-leader-here>/

If this is possible, I could also make a command in Vim that automatically commented out the selected text. Something like this:

vmap <z> :'<,'>s/^/<add-comment-leader-here>/

Any ideas are welcome! :)

Upvotes: 1

Views: 314

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 993085

If you haven't seen it already, you may be interested in the NERD Commenter Vim plugin.

Upvotes: 5

DrAl
DrAl

Reputation: 72626

Check out Enhanced Commentify: I think this does what you want: it determines the comment leader based on the file type.

If you want to do it yourself, the easiest way would be to define a mapping that uses exec to build a command and include a variable that is set in your ~/.vim/after/ftplugin/c.vim and other ftplugin files. Alternatively, just add the same mapping (with a different leader) to each ftplugin file.

Upvotes: 4

Related Questions