Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I ran into the following vimscript snippet in a book this afternoon and it seems like it'd be really useful. Unfortunately I haven't quite gotten it to work and I'm hoping somebody can tell me what I'm doing wrong.

 vmap <silent> ;h :s?^\(\s*\)+'\([^']\+\)',*\s*$?\1\2?g<CR>

When I highlight some markup in visual mode and hit ;h I get the following error:

Pattern not found: ^\(\s*\)+'\([^']\+\)',*\s*$

The vimscript regexp dialect is a little odd and vimscript itself seems a little alien. For all I know there's a typo. Everything after the first '+' is a bit of a mystery. My understanding is that this should convert a selection in visual mode to a quoted version:

 <div>
      <div class="header">stuff</div>
 </div>

to

 + '<div>'
 + '    <div class="header">stuff</div>'
 + '</div>'

In sublime I can do a find/replace with the following expression:

 /^(.*)$/+'\1'/

which makes the vimscript version seem a little verbose. Even so I'd like to be able to do it in vim as well.

[Edit: It turns out the above snippet works fine, it just wasn't doing what I thought it was. The text I was looking at listed a pair of these and I was looking at the wrong one. See below:]

 vmap <silent> ;h :s?^\(\s*\)+'\([^']\+\)',*\s*$?\1\2?g<CR>
 vmap <silent> ;q :s?^\(\s*\)\(.*\)\s*$?\1+'\2'?<CR>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

The equivalent to /^(.*)$/+'\1'/ in vim is

:%s/.*/+'&'/

It looks like

 vmap <silent> ;h :s?^\(\s*\)+'\([^']\+\)',*\s*$?\1\2?g<CR>

is trying to undo it for the current line.

So it converts

 + '<div>'
 + '    <div class="header">stuff</div>'
 + '</div>'

into

 <div>
      <div class="header">stuff</div>
 </div>

Not the other way around

share|improve this answer
    
You're right. The text I was looking at listed a pair of these: one to quote and one to remove quotes. I'm not too familiar with vimscript at all so I'd assumed that the first one listed was intended to quote a selection but that wasn't the case. Thank you for clearing that up. –  nerraga Sep 12 '13 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.