user823738
user823738

Reputation: 17521

How to commentize a word in Vim?

I would like to commentize a word under the cursor and use it as a macro or map it as a key binding.

For example this:

void somefunc(MyType* pType);

Would become:

void somefunc(MyType* /*pType*/);

I know that I just need to prepend and append word with /* and */ but I dont know how to do this.

Upvotes: 1

Views: 275

Answers (2)

Daan Bakker
Daan Bakker

Reputation: 6332

A way more general way to create inline comments is to use the Tcomment plugin.

When you've installed it you can use the gc operator to comment something, for example if you have the following file (with ^ indicating the cursor):

void somefunc(MyType* ^pType);

Pressing gce will get you:

void somefunc(MyType* /*pType*/);

You can use this for any motion command, but of course a line wise operator (eg. gcj) will not comment inline but entire lines instead.

Upvotes: 1

Kent
Kent

Reputation: 195109

try either of this mapping, pick one you feel better.

nnoremap <leader>cw caw/*<c-o>P*/<esc>

or

nnoremap <leader>cw viw<esc>a*/<esc>hbi/*<esc>

type <leader>cw in normal mode.

Upvotes: 3

Related Questions