0

I'm trying to find a way to make bracket insertion work the same way in Pycharm as in a jupyter notebook, that is to say, when I have some code selected:

some_code

and I hit the '(' key, have it put into brackets, like this (some code) instead of having my whole selection erased and be left with ()

Any idea on how to make this possible ?

LoicM
  • 1,786
  • 16
  • 37

2 Answers2

2

I think the answer proposed by Arnoud in 'How to surround selected text in PyCharm like with Sublime Text' is exactly what you're looking for.

Namely using:

Settings | Editor | General | Smart Keys -> Surround selection on typing quote or brace

The advantage of this method is that it adds brakets without any extra shortcuts to remember.

Ivo Merchiers
  • 1,589
  • 13
  • 29
1

You may create your own Live template:

File | Settings... 

then in the left pane select

Editor | Live templates

Now click at the green + (the topmost icon in the right narrow pane), select 1. Live Template and write in the Template text: exactly this: ($SELECTION$) - see the following picture.

Then fill in other two fields with something appropriate, e. g.:

enter image description here

Finally, click in the bottom part on the blue Define and from the popup menu check applicable contexts, in your case at least Python. Accept with OK button.

From now you may select a text, press Ctrl + Alt + J

enter image description here

and you may use it (press Enter or the underlined character, i. e. P in our case):

enter image description here

MarianD
  • 13,096
  • 12
  • 42
  • 54