Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know this is gonna be a stupid question, but I've tried here, google and the official documentation.

I've got a file with a whole heap of words of the form 'foo' and i want to change them to Literal('foo'). The following :s command :%s/'.*'/Literal('.*')/gci finds the things i want to change but then it replaces them all with Literal('.*') rather than Literal('foo')

Examples:
I get:
'_' -> Literal('.*') 
'dog' -> Literal('.*') 
I want: 
'_' -> Literal('_') 
'dog' -> Literal('dog')
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You almost have it

:%s/\('.*'\)/Literal(\1)/gci

You need to save the match and reference it in the replace. You were inserting a literal (no pun) .* in the replace.

share|improve this answer
    
Wonderful. Thanks so much. –  user191304 Dec 28 '13 at 4:59

An alternative to casey's answer with explicit capturing is to restrict the match with \zs (start) and \ze (end), to assert that the single quotes are there, but don't include them. Then, you can just refer to the match in the replacement part with &:

:%s/'\zs.*\ze'/Literal(&)/gci
share|improve this answer

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.