Is there a way to get == to work in vim for reindent python code?

Let's say I have the following Python code:

if conditionOne(): if conditionTwo(): foo = bar foo += 1 bar -= 2 

If I delete conditionTwo later, I would highlight three lines of the block so that it looks compatible with all my other code. Usually I just reached =% (my main language is C ++), but this will not work here, so I tried 3== in the first line of the block. This led to the following:

 if conditionOne(): foo = bar foo += 1 bar -= 2 

This is not what I was looking for. I could go with 3<< and get a better result, but this is not the team that I usually use. I would rather not remember the special indentation commands for Python only. In the spirit of Do not Make Me Think , is there a way to make filters = work with Python code, as I expect?

+4
source share
2 answers

While in C or C ++ the indentation of a program does not affect its behavior, in Python this may be true, since indentation is part of flow control.

Therefore, in Python, a program with a different indentation will have a different behavior, and for the editor it is impossible to guess whether the developer wants to indent lines (in the inner area) or not.

Therefore, your editor’s indentation features are designed to work with C-like languages, not Python.

+3
source

If you are using the vim-indent-object plugin, you can do the following to delete a row and select a block:

  • With cursor on conditional: <iidd
  • With the cursor anywhere in the block: <aidd

With that in mind, perhaps you could :nmap =% <ii and :nmap == <ai and remove the conditional condition as you want. This is not an ideal solution, but it seems like a worthy alternative to me.

0
source

Source: https://habr.com/ru/post/1384736/


All Articles