IntelliJ/PyCharm surround string with quotes

If you want to surround selected string or text in PyCharm or IntelliJ you have several different options:

  • Surround selection on typing quote or brace
  • Using a Live Template

Both of them are tested on Linux and Windows community editions 18.

PyCharm/IntelliJ Surround selection on typing quote or brace

This is my preferred way of quoting strings and variables with PyCharm/IntelliJ. It's general solution which apply every where with minimum settings.

  • File
  • Settings - CTRL + ALT + S
  • Editor
  • General
  • Smart keys - or just search for Surround
  • Check Surround selection on typing quote or brace
  • OK

After you setup the setting you can use it by:

  • Select text
  • Write single or double quote
  • For more than one quote repeat

On some Windows machines I notice that there is a problem related to quote auto-complete. In other words if you try to surround the text you need to type two times quotes which will result in:

''text'' instead of expected 'text'

This can be solved by disabling: Insert pair quote - on the same place.

PyCharm/IntelliJ Using a Live Template

The second way is much more customizable and allows you to add several different templates which can be used depending one your needs: file type and quotation marks:

  • File
  • Settings - CTRL + ALT + S
  • Editor
  • Smart keys - or just search for Live Tem
  • Find surround
  • Press the green plus to add new one
  • Enter abbreviation - i.e - Q
  • Enter descrtiption - Single Quote
  • Enter Template text - '$SELECTION$'
  • Enter Context - to which files types to apply
  • OK

Usage:

Once you are ready you can use it by:

  • select text
  • press CTRL + ALT + T
  • Select your live template