17

I am using PyCharm to write some python code and notice that I run into the following problem quite often:

I write a line of code like this

for item in myList:

Later, I realize that I would like the index of item as well, so I try to turn that line into this:

for i,item in enumerate(myList):

In order to turn the first line into the second, I put the cursor to the left of item and type i,. Then, I put the cursor to the left of myList and type enu; by this time, the code-completer suggests that I might want to type enumerate, which is exactly the behavior that I'm after. When I hit tab to implement the suggested enumerate, I notice that my line turns into

for i,item in enumerate:

The myList has been overwritten!
The behavior that I expect is this:

for i,item in enumerate(myList):

with the cursor immediately to the right of either the myList or the :.

Is there any way that I could make Pycharm behave according to my expectations?

Just in case it matters, my dev environment is Mac OSX 10.7.5 (Lion)

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • Possible duplicate of [IntelliJ autocompletion replacing the subsequent word](https://stackoverflow.com/questions/35424303/intellij-autocompletion-replacing-the-subsequent-word) – ash Sep 05 '18 at 11:56

2 Answers2

18

This behavior is by design when you complete using Tab. Please use Enter instead of Tab to insert the completion variant instead of overwriting.

Code completion settings dialog also has an option to insert variant by typing dot, space, etc.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
2

This is default behaviour in PyCharm, if you press TAB while being connected to another word like so en|myList, then myList will get deleted.

What you can do is this, double-click myList, press CRTL+ALT+T, press ENTER, and then press <-. Then just type in enumerate.

If you do this regularly, then you can just make a live template that surrounds.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • The idea of `a live template that surrounds` is very attractive to me. How might I go about making such a live template to override the default [TAB] behavior? Also, I am on a mac (question updated), and `CTRL+ALT+T` does not work (nor does `CMD+ALT+T`). What would the analogous Mac keyboard shortcut be? – inspectorG4dget Oct 19 '13 at 14:44
  • @inspectorG4dget https://www.jetbrains.com/pycharm/webhelp/creating-code-constructs-using-surround-templates.html – Games Brainiac Oct 19 '13 at 15:01
  • @inspectorG4dget If you face any problems, invite me to a chat-room, or something, and we can sort it out. – Games Brainiac Oct 19 '13 at 15:05
  • I just created a [chat room](http://chat.stackoverflow.com/rooms/39563/pycharm-codecompletion) and would appreciate your inputs – inspectorG4dget Oct 19 '13 at 15:11