views:

358

answers:

4

I'm not sure this is possible, but I'm interesting in making this happen.
Ideally, I would like to map this feature to shift+control+3.

I'm looking for a way to have Vim enter a comment (single line) which corresponds to the syntax of the file I'm editing. If there are multiple single-line comment styles, Vim/I can pick one. If the single-line comment has two parts (/*, */), then pressing shift+control+3 the first time will start the comment, and the second time will close the comment.

Examples:
Python: #
JavaScript: //
C, C++: /* */ or //

I know there are scripts which will insert comments for you, but I haven't seen any that will do this based on the syntax of the file.

Thanks!

+3  A: 

Sort of! I don't believe vim will do this out of the box, but you can install plugins that will do fairly intelligent commenting (using movement keys, visual line highlighting, etc) that are specific to the filetype being edited. You can get these plugins off of vim.org, and you should be able to make your own key mappings in your .vimrc file if you don't like the ones they come with.

tComment is pretty well regarded, and has worked for me.

I've heard that EnhCommentify might be better, but I haven't used it myself.

John Hyland
Thanks! Both look pretty good. I will try them out sometime today.
Nick Presta
+3  A: 

Seems like a similar question to this: http://stackoverflow.com/questions/1022799/how-to-comment-in-vim-while-respecting-the-indent/

Use the nerd commenter plugin: http://www.vim.org/scripts/script.php?script_id=1218

Sam
Thanks. I will add this to my list of plugins to try.
Nick Presta
+1  A: 

See: this script which provides a function to commented a highlighted area in visual mode.

You want to start a comment in insert mode so your function would look more like:

fun CommentLines()
  exe ":s@^@".g:Comment."@g"
endfun
Callum
+2  A: 

I highly recommend NERD Commenter.

Jeremy Cantrell