views:

132

answers:

2

Hi!

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! :)

+4  A: 

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.

Al
If you are using EnhancedCommentify, you should be able to do a generic:g/<pattern>/exe "normal \<Plug>Comment" -- whatever the current filetype is.
Luc Hermitte
+4  A: 

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

Greg Hewgill
Do you have any comments about the differences between NERD Commenter and Enhanced Commentify? It seems both do something like what I want to do ... (and thanks for the tip, by the way!)
Karl Yngve Lervåg
EnhancedCommentify is much more older and has always fulfilled my needs. In the end, I never felt the need to test the challenger.
Luc Hermitte