Autoindent Tool For Python In Mac

  • Python Runner is a handy tool for learning Python and running Python script for daily tasks. Simply type Python code and press “⌘R” to run it, that is all! You don’t event need to save the file first!
  • It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package. You can also find it on the web. This script attempts to convert any python script to conform with the 4-space standard.
Active1 year, 11 months ago

I really like the Emacs editor for Python because of it's smart tabbing for instance if I have something like this

and I press tab on the cursor (which is on the b of beep), it will not insert a new tab causing a syntax error but it would toggle through the possible levels that beep can be on. Is there anyway of getting this effect on Vim?

Mateusz Piotrowski

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation. Change Python (.py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files.

4,3816 gold badges35 silver badges57 bronze badges
DoboyDoboy
4,9449 gold badges29 silver badges46 bronze badges

8 Answers

In general, vim is a very powerful regular language editor (macros extend this but we'll ignore that for now). This is because vim's a thin layer on top of ed, and ed isn't much more than a line editor that speaks regex. Emacs has the advantage of being built on top of ELisp; lending it the ability to easily parse complex grammars and perform indentation tricks like the one you shared above.

To be honest, I've never been able to dive into the depths of emacs because it is simply delightful meditating within my vim cave. With that said, let's jump in.

Getting Started

Janus

For beginners, I highly recommend installing the readymade Janus plugin (fwiw, the name hails from a Star Trek episode featuring Janus Vim). If you want a quick shortcut to a vim IDE it's your best bang for your buck.

I've never used it much, but I've seen others use it happily and my current setup is borrowed heavily from an old Janus build.

Vim Pathogen

Otherwise, do some exploring on your own! I'd highly recommend installing vim pathogen if you want to see the universe of vim plugins.

It's a package manager of sorts. Once you install it, you can git clone packages to your ~/.vim/bundle directory and they're auto-installed. No more plugin installation, maintenance, or uninstall headaches!

You can run the following script from the GitHub page to install pathogen:

Helpful Links

Here are some links on extending vim I've found and enjoyed:

  • OS X And Python (osx specific)
  • Learn Vimscript The Hard Way (great book if you want to learn vimscript)
mvanveenmvanveen
4,8706 gold badges26 silver badges38 bronze badges

For those arriving around summer 2013, I believe some of this thread is outdated.

I followed this howto which recommends Vundle over Pathogen. After one days use I found installing plugins trivial.

The klen/python-mode plugin deserves special mention. It provides pyflakes and pylint amongst other features.

I have just started using Valloric/YouCompleteMe and I love it. It has C-lang auto-complete and python also works great thanks to jedi integration. It may well replace jedi-vim as per this discussion /davidhalter/jedi-vim/issues/119

Finally browsing the /carlhuda/janus plugins supplied is a good guide to useful scripts you might not know you are looking for such as NerdTree, vim-fugitive, syntastic, powerline, ack.vim, snipmate...

All the above '{}/{}' are found on github you can find them easily with Google.

GregGreg
Mac

Put the following in your .vimrc

See also the detailed instructions

I personally use JetBrain's PyCharm with the IdeaVIM plugin when doing anything complex, for simple editing the additions to .vimrc seem sufficient.

KimvaisKimvais
26.8k11 gold badges90 silver badges127 bronze badges

There is a bundled collection of Vim plugins for Python development:http://www.vim.org/scripts/script.php?script_id=3770

orlukeorluke

Under Linux, What worked for me was John Anderson's (sontek) guide, which you can find at this link. However, I cheated and just used his easy configuration setup from his Git repostiory:

His configuration is fairly up to date as of today.

Scott Emmons
1,5752 gold badges9 silver badges9 bronze badges
Will SamsWill Sams

Re: the dead 'Turning Vim Into A Modern Python IDE' link, back in 2013 I saved a copy, that I converted to a HTML page as well as a PDF copy:

Edit (Sep 08, 2017) updated URLs.

Victoria StuartVictoria Stuart
1,6641 gold badge17 silver badges17 bronze badges

Some time ago I installed Valloric/YouCompleteMe and I find it really awesome. It provides you completion for file paths, function names, methods, variable names... Together with davidhalter/jedi-vim it makes vim great for python programming (the only thing missing now is a linter).

SummerBreezeSummerBreezeIndent
3,8632 gold badges19 silver badges20 bronze badges
Indent

A very good plugin management system to use. The included vimrc file is good enough for python programming and can be easily configured to your needs. See http://spf13.com/project/spf13-vim/

OseackOseack

Not the answer you're looking for? Browse other questions tagged pythonvimconfigurationeditorindentation or ask your own question.

Active9 months ago

I have some existing code which isn't formatted consistently -- sometimes two spaces are used for indent, sometimes four, and so on. The code itself is correct and well-tested, but the formatting is awful.

Is there a place online where I can simply paste a snippet of Python code and have it be indented/formatted automatically for me? Alternatively, is there an X such that I can do something like X --input=*.py and have it overwrite each file with a formatted version?

John FeminellaJohn Feminella
251k37 gold badges310 silver badges332 bronze badges

5 Answers

Edit: Nowadays, I would recommend autopep8, since it not only corrects indentation problems but also (at your discretion) makes code conform to many other PEP8 guidelines.

Use reindent.py. It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package.

You can also find it on the web.

This script attempts to convert any python script to conform with the 4-space standard.

unutbuunutbu
591k112 gold badges1286 silver badges1327 bronze badges

autopep8

autopep8 would auto-format your python script. not only the code indentation, but also other coding spacing styles. It makes your python script to conform PEP8 Style Guide.

Update:

Many editors have pep8 plugins that automatically reformat your code right after you save the file. py-autopep8 in emacs

Python Indent Spaces

yapf

yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.

For more information, like formatting configurations, please read the README.rst on yapf github

Update 2:

Black

Black is much better than yapf. It's smarter and fits most complex formatting cases.

d2207197d2207197

Use black. It has deliberately only one option (line length) to ensure consistency across many projects. It enforces PEP8.

nbedounbedou

Found an offline solution with PyCharm

In PyCharm do:

1. Select all the Code:

Python Indent Function

2. Format all the Code

mrkmrk
2,8921 gold badge22 silver badges41 bronze badges

Some editors have an auto-format feature that does this for you. Eclipse is one example (though you would probably have to install a python plug-in).

Have you checked whichever editor you use for such a feature?

Notepad++ Auto Indent Python

ChrisChris
2,5691 gold badge15 silver badges32 bronze badges

Python Indent Checker

Not the answer you're looking for? Browse other questions tagged pythonformatting or ask your own question.