Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reindenting Python code in Vim

Tags:

python

vim

vi

I need to enclose a block of code with a for loop. As this is Python I need to take care of indenting and increment the number of tabs by one. Any easy way to do this in Vim?

like image 340
Laz Avatar asked Jul 24 '12 11:07

Laz


People also ask

Can you code Python in Vim?

vimrc options. Vim as a Python IDE shows a slew of plugins and configuration options for coding with Python in Vim. This repository's folder with Vimrc files has example configurations that are well commented and easy to learn from.

How do I indent a Python file in Vim?

Vindect is a Python script to detect the indent options required for editing a Python program. In Vim, output from the :version command should include "+python". The following options are set based upon usage in the current file, and your preferences: shiftwidth , tabstop , softtabstop , smarttab , expandtab .

How do I create a Python script in vim?

Write Your Python Script To write in the vim editor, press i to switch to insert mode. Write the best python script in the world. Press esc to leave the editing mode. Write the command :wq to save and quite the vim editor ( w for write and q for quit ).

How do you indent an entire file in Python?

While in the editor, select the necessary code fragment and press Ctrl+Alt+I . If you need to adjust indentation settings, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | Code Style. On the appropriate language page, on the Tabs and Indents tab, specify the appropriate indents options and click OK.


1 Answers

You can manually adjust indentation with < and >, and == will auto-indent a block of code.

Also, Indenting Python with Vim might be of help for getting some more advanced auto-indentation.

Lastly, ]p is a handy way to insert a yanked block of code, indenting it to the proper level (try yanking your block, moving the cursor to the start of your for loop, then pressing ]p).

like image 164
David Cain Avatar answered Sep 30 '22 13:09

David Cain