Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top