0

I'd like to learn how to search and replace in VIM - keeping the search string intact and adding to it.

Take this as a simple example. See image below. I copied a simple python script written for python2 and the goal is to make it compatible for python3. The print lines needs to be encapsulated with ().

I've entered :%s/".*" to regex all lines within two ". Now I'd like to add () on top of that string -> (STRING). So I have to somehow reference that search string and add to it.

Something like this

:%s/".*"/(SEARCHSTRING)

enter image description here

2
  • To convert script from Python 2 to Python I strongly recommend 2to3.py: C:\Python27\Tools\Scripts\2to3.py -f print -f has_key -f tuple_params -f ne -f dict -f map -f filter -f except -f raise -f numliterals -w "myscript.py" Feb 22 at 10:38
  • Thanks, didn't know about that. But I just used this script as a simple example.
    – tre332sju
    Feb 23 at 8:23

1 Answer 1

1

You can use & or \0:

:%s/".*"/(&)/

2
  • 2
    Teach a man how to fish: :h sub-replace-special
    – statox
    Feb 22 at 11:26
  • Thank you! I knew there was an answer. Didn't find it when searching online. Much appreciated.
    – tre332sju
    Feb 23 at 8:24

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.