4

I have a string like 'foobar', and I want to surround it with parentheses: ('foobar').

With vim-surround, I can do ysi'), which produces '(foobar)'... Not what I want

I can also ysa'), but that will include whitespace: ( 'foobar')... Meh, close.

In vim, I can 2yi', which yanks exactly what I need 'foobar', but I cannot seem to make it work with vim-surround

  • 3
    ysiW) ->The W must be capitalized which forces it to surround around the '. Finally use ) instead of ( which surrounds it the way you were asking. Good luck. – Annis Monadjem Jan 15 '16 at 14:18
  • What if the string has multiple words in it? – Eldamir Jan 15 '16 at 14:19
  • 2
    check out :h surround (great tutorial and few of nice examples including answer to your question). – Annis Monadjem Jan 15 '16 at 14:37
  • Hmmm. ysa') seems to work correctly for me. If your cursor is on the opening ', then ysf') also works. – Rich Jan 15 '16 at 16:26
  • Not if you have whitespace leading the string – Eldamir Jan 15 '16 at 16:26
7

It's not specific to vim-surround - va' also selects the leading whitespace. From :h a':

a"                                                      v_aquote aquote
a'                                                      v_a' a'
a`                                                      v_a` a`
                        "a quoted string".  Selects the text from the previous
                        quote until the next quote.  The 'quoteescape' option
                        is used to skip escaped quotes.
                        Only works within one line.
                        When the cursor starts on a quote, Vim will figure out
                        which quote pairs form a string by searching from the
                        start of the line.
                        Any trailing white space is included, unless there is
                        none, then leading white space is included.

Reading on:

i"                                                      v_iquote iquote
i'                                                      v_i' i'
i`                                                      v_i` i`
                        Like a", a' and a`, but exclude the quotes and
                        repeating won't extend the Visual selection.
                        Special case: With a count of 2 the quotes are
                        included, but no extra white space as with a"/a'/a`.

So:

ys2i')

And with my tests, this worked as desired:

a   'aaa'

Changes to:

a   ('aaa')
  • I actually did try this, but I must have mistyped, 'cause it certainly works. Thanks mate – Eldamir Jan 19 '16 at 14:47
2

I always visually select string before using surround plus I have a mapping

xmap s S

It saves me a key stroke while surrounding selected string so for me the sequence is:

va's)

[obvious explanation: "select visually everything within and with single quotes and surround selection with parentheses without adding white space"]

Alternative version: va'sb

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.