126

Is there a way to configure PyCharm to be able to surround selected code with parenthesis by just typing on the parenthesis key, like when we use SublimText 2?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alexis Benoist
  • 2,400
  • 2
  • 17
  • 23

3 Answers3

229

I think you want something like

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

Arnaud P
  • 12,022
  • 7
  • 56
  • 67
  • 7
    On Windows, this is `File | Settings` instead of `Preferences` – Nic May 22 '16 at 22:37
  • This results in a behavior similar to that in Jupyter Notebook, which makes the switch between the two much smoother. Thanks! – Shovalt Apr 10 '17 at 07:25
  • 2
    on mac is `Preferences` instead of `Settings` – framled Jun 29 '17 at 13:14
  • Just to clarify, once this is enabled, go to your code editor window and select part of the text you want wrapped in quotes and then just type `SHIFT` + `'` for example. – ruslaniv May 14 '21 at 11:08
20

PyCharm 4.0 has the option to Surround With..., by selecting your code snippet and pressing

ctrl + alt + T

or on Mac: + + T

Option 1 should provide you with the functionality you are looking for:

PyCharm Ctrl+Alt+T

Lee He
  • 181
  • 12
Andy
  • 49,085
  • 60
  • 166
  • 233
  • 1
    I tried this, but it also adds a line break. If I have, say, "print doc", select "doc" and apply this, "doc" turns into "(doc)", as expected, but it goes to another line. – Douglas Henrique Aug 12 '16 at 22:22
  • @DouglasHenrique that behavior should be able to be changed using the $END$ live template variable. – j_walker_dev Feb 04 '17 at 21:06
1

Windows: open pycharm and select file, settings, Editor, Smart Keys, in the list you will check "Surround selection on typing quote or brace", then apply. enter image description here

Image of pycharm location of smart keys

Adam H
  • 11
  • 2