Sublime Forum

Replace outer part

#1

Hello.

Is it possiible with sublime to replace for example

print “Hello”
print “World”
print “!!!”

to
print(“Hello”)
print(“Wolrd”)
print("!!!")

in one step? I can do replace print " with print(". But I could not replace the " at the end.

Best regards
Marten

0 Likes

#2

Sure. Open the replace panel (ctrl+h) enable regex search (alt+r) and search for print\s*(\"[^\"]*\") and replace it with print($1) .
You can have a look at regular expressions to do something like this on your own.

0 Likes

#3

If you’re not a regex ninja, a simple solution that often works is to use ctrl+d to select several “print” at once, insert a ( and use END key to go to the end of the line and append another )

If you want to do more complex stuff you should look into regex though

0 Likes

#4

with ctrl+d I can select all print statments. But how do you change them all at once?

0 Likes

#5

Thanks for the hint with print($1).

In my case to convert python 2.x code to python 3.x I modified the regex to print\s*(.*)\n and replace it with print($1)\n so
that ‘print “%s” % val’ also works

0 Likes