Search code examples
pycharm

PyCharm surround with "print"


Pycharm has the function to "surround with" pieces of code. There are some already setted up functions:

enter image description here

I would like to add print() (using python3) to it, how can I do?


Solution

  • AFAIU, you can define your own “Live” templates for this.

    define the live template

    Go to FileSettingsEditorLive Templates. In the right pane tree, click on Python then click the green “+” button on the right; select “Live Template”.

    Enter a name for Abbreviation (e.g. “printer”), a Description, and then in the Template text:

    print($SELECTION$)
    

    Afterwards click on the “Define” of the “No applicable contexts. Define” line at the bottom. Select “Python” in the tree. This step is important!

    use the live template

    Now your template will be usable by the CodeSurround with Live Template action (in my case, the shortcut is Ctrl+Alt+J).