Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created November 8, 2017 03:36
Show Gist options
  • Save theY4Kman/c2009062d06ed9bc59586e0e985b73c5 to your computer and use it in GitHub Desktop.
Save theY4Kman/c2009062d06ed9bc59586e0e985b73c5 to your computer and use it in GitHub Desktop.
A little write-up on some of the PyCharm Live Templates I use on a daily basis

Convenient PyCharm Live Templates

I find the real beauty of PyCharm to be its code insight. vim seems to be winning on the scripting side of things, but it looks like the JetBrains ecosystem made it economical to build deeply insightful and performant language tooling, enabling ways to navigate, produce, and refactor code – regardless of who wrote it – without waiting for an executable to start, or a plugin to be written, or a maintainer to patch the eighteen bugs affecting only others... in his pro bono, personal-use tool. And one day when JetBrains realizes the selling power of (or I get off my ass and embed) a scripting language for customization, shit's gon' explode.

Till then, PyCharm has a few semi-customizable features which make dev life easier. One of them is Live Templates, which are text templates which can either be expanded by typing an abbreviation and pressing tab; or by selecting some text, pressing Option+Command+T, autocompleting the abbreviation, and pressing Enter. They allow variables which can be inputted by the user (and ordered however you wish), and offer ways to autocomplete text from, e.g., a list of local variables, all the Python classes in scope, the current filename, etc. You can then make that text camelCase, snake_case, and a few other variations your heart hopefully desires (because if what you want ain't there, you're up some creek we pump our septic tank into).

Anyway, here are some of the Live Templates I've picked up from others over the years, or felt pain enough to create. PyCharm is pretty good with settings. They allow importing/exporting, as well as syncing to a VCS repo, and, in benefit of our case, copying and pasting from/to the settings UI. So, I'll give you my Live Template XML settings, and if you wanna try them out, you can open your settings (Command+, [comma]), go to Live Templates under Editor, create a new Live Template group (the plus button opens a menu), right click it, and Paste.

Here are my PyCharm Live Templates.

I made some screencasts of the ones I use the most, so you can see them in action.

Screencasts

dv

This creates a dictionary key-value pair, where the value is a name in scope, and the key is the same name as the value. The benefit is the autocomplete.

2017-11-07 16 00 40

If you want to add many dictionary entries, hit Enter twice after narrowing down your selection in autocomplete, run the Live Template again, rinse and repeat.

kw

This passes a keyword argument to a function, where the argument name is the same as the name being passed as value. Autocomplete is winrar again.

2017-11-07 22 01 35

A comma and space are added to the end, enabling one to chain these suckers up. PyCharm, by default, will remove whitespace at the end of modified lines, so if you want to (or have to) wrap, just press Enter and continue. Upon saving, the whitespace after the comma at the end of the line will be removed. A thing of beauty.

paren

PyCharm has some settings where if you select text and press "(" or "[" or "{" or any quote or other such opening characters with matching closers, PyCharm will automatically insert both the beginning and end.

Enclosing is very nice, but there's one special but notable case where the cursor is left in an inconvenient position. And that's when I want to wrap a value in a function call. With normal enclosing, the cursor is placed after the ending parenthesis, but I want it at the beginning, so I can write the function name. This live template simply leaves the cursor before the opening parenthesis:

2017-11-07 22 16 18

This one is performed by selecting text, pressing Option+Command+T, typing "p" to autocomplete "paren", and pressing Enter. Then I extend my selection (which might be Command+UpArrow, but I have it bound to Command+W, because I'm used to non-OSX PyCharm) twice, rinse, repeat.

se

Sets an instance variable (self.<variable> = <variable>) using the same name as the value, so autocomplete reduces the number of steps involved (and provides spelling feedback, so ya don't have to think about it – just perceive).

2017-11-07 22 21 47

xxx

No, this isn't your favourite Vin Diesel movie. Everyone knows that's Pitch Black, anyway.

This expands to a monstrous comment filled with octothorpes, something which looks obviously out of place. In PyCharm, I add a TODO template (regex "\bxxx\b.*"), so these burn neon into my corneas. In most consoles not taking up the entire monitor, these lines wrap in a git diff, which looks wonky. They just look so ugly they don't belong in any code released to the public. And therein lies their benefit.

When I'm debugging and add a print() line to check something, I add it to the end of the line. If it'd be particularly dangerous (or embarrassing) if I ever accidentally committed it, I pad the top and bottom with five or ten of these. Same goes for large chunks of debugging-only code. When I skim through code before committing (a cheap habit which saves a lot of self-facepalming), these stick out like a sore thumb, even if I'm just paging through.

2017-11-07 22 31 10 2017-11-07 22 33 55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment