10

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

6
  • 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. Jan 15, 2016 at 14:18
  • What if the string has multiple words in it?
    – Eldamir
    Jan 15, 2016 at 14:19
  • 2
    check out :h surround (great tutorial and few of nice examples including answer to your question). Jan 15, 2016 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, 2016 at 16:26
  • Not if you have whitespace leading the string
    – Eldamir
    Jan 15, 2016 at 16:26

2 Answers 2

12

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')
3
  • I actually did try this, but I must have mistyped, 'cause it certainly works. Thanks mate
    – Eldamir
    Jan 19, 2016 at 14:47
  • just to clarify for people not using vim-surround: you can avoid leading white-space in your selection by making a remap like nnoremap va' v2i' and nnoremap va" v2i" Dec 30, 2020 at 13:32
  • For anyone using surround, you can map nmap ysa' ys2i', nmap ysa" ys2i", nmap ysa` ys2i`. From github.com/tpope/vim-surround/issues/276#issuecomment-515576951
    – Simba
    May 3, 2021 at 7:03
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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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