2

i want to substitute the text 'which apt' to $(which apt).

:s/'\&.*'/$(\0) 

does substitute to $('which apt'). How can i leave those single quotes out on the substitution, so that just $(..) appears?


EDIT

:s/'\zs\&.*\ze'/$(\0)

results in $('which apt)'. Although the regex should match any character between the two ', as i marked them as outside of the selection. i don't know if i did sth wrong?

  • Use \zs and \ze. See :help \zs. – EvergreenTree Feb 6 '16 at 20:32
  • I'm certain that \& doesn't do what you think it does...although I don't know exactly what you think it does. :) – Wildcard Feb 9 '16 at 5:46
8
:s/'\(.\{-}\)'/$(\1)/g

Search part:

'            single quote
 \(.\{-}\)   any character — . —, as few as possible — \{-} —,
             captured for reuse in the replacement — \(...\) —
          '  single quote

Replacement part:

$(           replace the first quote
  \1         reuse the capture group
    )        replace the second quote
  • Huh, I thought when using {curly braces} you have to backslash escape both of them, e.g. /\(cat\)\{3\}/ to match catcatcat. Is \{-} a typo? – Wildcard Feb 9 '16 at 5:39
  • Also: Thanks, I didn't know about the "minimal match" quantifier; I usually would just use :s/'\([^']*\)'/$(\1)/g – Wildcard Feb 9 '16 at 5:40
  • 1
    No, only the first { needs to be escaped. – romainl Feb 9 '16 at 6:12

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.