Trying to make it easier to comment out unused parameters in a function.

Added the macro:

map C wbywcw/*<C-v><Esc>pa*/<C-v><Esc>/[,)]<C-v><C-m>nb

So on hitting Cap-C it will comment out the current parameter under the cursor and move to the next parameter. Nice to get feedback on that as well.

But what I really want to try and do is make it check to see if the current parameter is commented out. If it is then uncomment it otherwise comment it out.

Break down of the current command:

map C wbywcw/*<C-v><Esc>pa*/<C-v><Esc>/[,)]<C-v><C-m>nb

wb    go the beginning of the current word.
      Need to use "w" first becuase if we are currently at the
      beginning of the word a single "b" would go to the previous word.

yw    yank the parameter into the buffer.

cw    Change the parameter.
      Into /*

p     pull the yanked parameter so we now have

a     append more text

/[,)] Search for the closing ')' or the comma after this parameter

n     Move to the comma after the next param

b     Move back one word should be the next param
  • Did my answer work for you? Can you use it or need more assistance? – Philippos Mar 25 '17 at 7:42
  • @LokiAstari A few thoughts on the original mapping: 1) map should probably be nmap (or maybe even nnoremap), unless you really wanted to have the mapping apply to Visual, Select, and Operator-pending modes as well as Normal mode. 2) yw is redundant. c copies the deleted text. 3) wbcw can be replaced with caw if you want to include trailing spaces, or ciw if you just want the word itself. 4) <esc>pa can be replaced with <c-r>". – 8bittree Sep 19 '17 at 22:41
  • @8bittree That would be a great answer. – Martin York Sep 20 '17 at 15:54

Just the toggling of the comment (not yet turned into a macro and without proceeding to the next parameter):

viw:s_\(/\*\)\=\(\%V\w*\)\(\*/\)\=_\=strpart("/*", strlen(submatch(1))).submatch(2).strpart("*/", strlen(submatch(3)))_

Or, if you prefer more magic:

viw:s_\v(/\*)=(%V\w*)(\*/)=_\=strpart("/*", strlen(submatch(1))).submatch(2).strpart("*/", strlen(submatch(3)))_

Basically, the length of the submatches for /* and */ are used to cut characters from these strings (so their presence is inverted).

Or, if you trust in comment signs being pairwise, you could do it a little shorter:


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.