I can use vi{
and va{
to select C++ code blocks. It helps me a lot when I need to yank/delete them.
But Python uses indentation to indicate code blocks. I cannot find any better way. Any ideas?
Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.
To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.
I have not had much of an occasion to use it yet, but Michael Smith’s vim-indent-object sounds like it may be close to what you want.
Example usage (line numbers shown as with set number
active):
1 This is
2 some text
3 with multiple
4 levels of
5 indentation.
6 There is also
7 another stanza.
With the cursor on the line 2, 4 or 5, type Vii
to select lines 2 through 5.
With the cursor on line 3, type Vai
to select lines 2 through 3.
With the cursor on line 3, type Vaiii
(or Viiii
), to select lines 2 through 5 (in visual mode multiple uses of indentation objects extend the selection to the next level up/out).
The text objects it defines are:
ai
: the current indentation level plus the line aboveif
and its “then” block)ii
: the current indentation levelaI
: the current indentation level plus the one line above and one line belowif
/endif
).iI
: the current indentation level (effectively, ii
, and iI
are the same)The objects can also be used with other commands that expect text objects (i.e. cii
to delete the current indentation level and start inserting its replacement).
I simply hit v
for visual mode then (
or )
. This will take you to the next blank line. That should correspond to your code blocks if you are formatting nicely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With