10

I'm working with the vim-surround plugin and this HTML (where the * is my cursor):

<li class="sample" style="border-color: #005462;">*#005462</li>

I'd like to surround the #005462 with <code> so it looks like this, <code>#005462</code>. I can do this with visual mode but would like to do something I can repeat with the dot operator. Any advice?

Nick Knowlson
  • 7,185
  • 6
  • 47
  • 63
Mark B
  • 2,870
  • 3
  • 20
  • 18

4 Answers4

9

You want repeat.vim which adds . support to several other plugins, including surround.

Austin Taylor
  • 5,437
  • 1
  • 23
  • 29
  • 1
    surround.vim and repeat.vim in tandem is definitely the best general-purpose solution to this problem. – sleepynate Apr 26 '11 at 18:12
  • Since he has to use visual mode to select the text, repeat.vim unfortunately won't help in this specific case. See [my answer](http://stackoverflow.com/questions/5794182/how-do-i-surround-two-words-with-code-tag-in-vim-such-that-i-can-repeat-the-op/5795807#5795807) for the rest of the details. – Nick Knowlson Apr 26 '11 at 20:06
7

From normal mode try to record a macro. Then:

qai<code><esc>ea</code><esc>q
  1. This command say start recording (q) in a.
  2. Start insertion mode (i).
  3. Type <code>.
  4. Return to normal mode (<esc>).
  5. Move to the end of the word (e).
  6. Then enter insert mode again (a).
  7. Type </code>.
  8. Return to normal mode (<esc>).
  9. Then stop recording (q).

After you can repeat this command using @a or @@ for repeat last used command. Dont forget to be positionned at the rigth place when you invoke a or you will not get the expected result.

Lynch
  • 9,174
  • 2
  • 23
  • 34
1

A couple other people have run into problems repeating things with surround.vim:

In the first link, there is a quote from the surround.vim docs that implies visual mode surrounding doesn't work:

The "." command will work with ds, cs, and yss if you install repeat.vim

And given the text elements surrounding it, I don't think there's a way to surround just the #005462 without using visual mode.

So for this particular problem, I think a quick, repeatable search and replace is your best bet.

:s/: \(#......\);/: <code>\1<\/code>;/g

Go to the right line, and type or paste this in command-line mode and press enter.

Move to the next line and press & to repeat it.

If you know you want to replace ALL of them in the file, you can add % before the s/ command, like so:

:%s/: \(#......\);/: <code>\1<\/code>;/g

Hope this helps!

Community
  • 1
  • 1
Nick Knowlson
  • 7,185
  • 6
  • 47
  • 63
0

Check out this Vimcasts.org episode on converting haml to erb. You can use the same technique to accomplish your task.

nathan
  • 5,513
  • 4
  • 35
  • 47