Vime regex for python2 print to python3

So say that 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 will need to add those brackets to them:

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

I tried to use the following regex in vim to solve the problem, but it did not work:

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

He just gave me print functions (with parentheses) that had blank lines. Any help is appreciated; thanks.

+1
source share
1 answer

This will work for your examples.

 :%s/print \('.*'\)/print(\1)/g 
  • You do not need to hide the space.
  • Nothing is written in parentheses, so \1 is an empty string in your regular expression.

But I also recommend using 2to3

+3
source

All Articles