8

Is there way to define snippets in PyCharm, built-in or via plug-in? For example I want paste self. by hotkey. Now I'm doing it by 'se' + CTRL+ENTER, but want to find more efficient way for this frequent operation.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Gill Bates
  • 14,330
  • 23
  • 70
  • 138

2 Answers2

9

What you are probably looking for is PyCharm's live templates - although for something as short as self. - they probably are no quicker than typing.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • Yeah, I already discover it. Seems like their purpose to do more complex and less frequent job, like surround with `try:...except:`. And they cannot be bounded with a hotkey. – Gill Bates Jan 02 '13 at 16:10
  • I'm not sure it makes sense to try and use a hotkey to insert something so short - it's not going to be quicker than typing it. – Gareth Latty Jan 02 '13 at 16:13
  • @GillBates, @Lattyware: Are you saying that pressing 2 keys instead of 5 is still "not so good solution"? Live template with `s` as abbreviation and `Tab` (my own preference) as expand key: and now `s[TAB]` magically transforms into `self.` – LazyOne Jan 03 '13 at 10:47
  • @LazyOne I'm not saying it's not shorter - I just don't really see it's that valuable - there are a few characters different, and hotkeys tend to break your flow in typing more than just typing it out. – Gareth Latty Jan 03 '13 at 12:20
  • 1
    @Lattyware. Hot key -- yes, 100% agree on that. Using Live Template for this (as per your answer) is a very valid choice here (because of the way how it works -- you naturally type/press 2 characters/keys one by one instead of pressing combination of keys (2 and more) at the same time). In short: I agree with your answer on using Live Templates over Hot Keys here even if result text (`self.`) is quite short especially if such text/construction is used a lot. – LazyOne Jan 03 '13 at 13:24
  • @LazyOne Well, it is matter of taste. I can counter with that, `sTAB ` takes longer time, than `ALT+s`, or `SHIFT+s`, because you need to hit two keys sequentially, and combo takes as much time as hitting one key takes. And also - `TAB` lies on the edge of keyboard, and `ALT`/`SHIFT` is almost always under fingers, which makes them more easy to access. – Gill Bates Jan 03 '13 at 16:40
  • i agree, live templates work pretty well. eg i use them to type "ipdb" and it completes it to "import ipdb; ipdb.set_trace()" for quick drop into debug shell anywhere... upboat. – Florian Jul 15 '13 at 09:59
6

You can use macros for this purpose. Edit | Macros | Start Macro Recording begins the recording, then Settings | Keymap allows you to assign keyboard shortcuts to macros.

yole
  • 92,896
  • 20
  • 260
  • 197