Question

Even when I open pure emacs -Q and a python file in it:

| - cursor

if smth:
|   print("asd")       # press TAB and cursor moves to "p" symbol, it's ok

if smth:
   |print("asd")       # press TAB and I get this:

if smth:
print("asd")           # press TAB and I get prev step

So TAB makes a cyclic change of indentation level. Which I absolutely don't want to.
If to use smart-tab problem can be solved. But yasnippet (yas-global-mode 1) brings it again.
And that's where I can't find why.

If you had this problem (python+yasnippet+correct indent) - please give me a tip. Or just a link to a working config.

Latest Emacs (24.3.50.1)

Was it helpful?

Solution

One way of controlling the cycling with TAB is to customize python-indent-trigger-commands. The docstring of the variable states

Commands that might trigger a `python-indent-line' call.

However it is not clear from the docstring that the variable can be used to control cycling (actually I am not even sure if setting this variable if the correct way of controlling indentation cycling). The docstring of python-indent-line explains the purpose of this variable better

When the variable last-command' is equal to one of the symbols inside python-indent-trigger-commands or FORCE-TOGGLE is non-nil it cycles levels indicated in the variable python-indent-levels by setting the current level in the variable `python-indent-current-level'.

So (setq python-indent-trigger-commands nil) (or you can just remove indent-for-tab-command from the list) can be used for disabling indentation cycling. There is a slight disadvantage of this approach that you cannot use TAB indent code like the following where else can either close for or if.

for ..:
    if ..:
        ...
        break
else:
    ...

You will have hit backspace before else to reindent it such that it closes the for (by default it will be indented such that it closes the if)

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