1

So say I have a python v2.7 file with some code like this:

print 'asdf'
print 'hi mom!'

But I want to run it in python3, I'll need to add those parenthesis to them like so:

print('asdf')
print('hi mom!')

I was trying to use the following regex in vim to solve the problem, but it wasn't working:

:%s/print\ '.*'/print('\1')/gc

It just gave me print functions (with parenthesis) that had empty strings. Any help is appreciated; thanks.

1
  • 10
    For the love of all that is optimized and provable, please use 2to3 Commented Jul 26, 2013 at 0:42

1 Answer 1

3

This would work for your examples

:%s/print \('.*'\)/print(\1)/g
  1. You don't need to escape the space.
  2. You don't actually capture anything in parenthesis so the \1 is an empty string in your regex.

But I also recommend using 2to3

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that is the regex that I was looking for. I don't need something like '2to3', but thanks anyways.
@Benjamin if you are going to spend a lot of time using regexes in vim (if you use vim you will probably use them a lot) I would recommend reading or skimming a lot of :h pattern-searches

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.