days
-2
-6
hours
0
-3
minutes
-3
-5
seconds
-5
-7
Shed your skin

Automatically fold Python code in Vim with Coiled Snake

Sarah Schlothauer
python
© Shutterstock / Stefano Buttafoco

Have you tried automatically folding Python code in Vim? While manually folding code works well, there’s always room for improvement. Coiled Snake is a Vim plug-in for automatically folding code. Say good-bye to clutter and hello to cleaner, more compact code.

Calling all Python programmers: have you seen this Vim plugin? Coiled Snake is a Vim plugin for automatically folding Python code. It makes compact folds without a fuss and allows for flexible, custom configuration.

There are a few other ways to fold Python code out there (such as SimplyFold – a no-BS Python code folding for Vim), but let’s give Coiled Snake a few minutes to bask in the spotlight. Do you manually fold your code with foldmethod indent? Is a plug-in your method of choice?

Plugin-in features

Some of the key features include:

  • Classes, functions, docstrings, imports, and large data structures are all automatically recognized and folded.
  • The folds seek to hide as much unnecessary clutter as possible, e.g. blank lines between methods or classes, docstring quotes, decorator lines, etc.
  • Each fold is labeled clearly and without visual clutter. The labels can also present context-specific information, like whether a class or function has been documented.
  • The algorithms for automatically creating the folds and summarizing the folds are independent from each other, so if you only like one you don’t have to use the other.

SEE ALSO: JavaScript is Python developers’ best friend, report shows

Less whitespace, less code. Coiled Snake folds decorators and whitespace, giving you cleaner, concise code that’s easier to read.

With Coiled Snake, you can disable folds on a case-by-case basis. Simply put a # at the end of the line. How do you feel about this feature since it requires modifying the source code?

Sadly, the demo recording has been archived. (Just a reminder: if you use asciinema.org to record terminal sessions, make sure to claim your recordings. Otherwise they automatically enter archival mode after 7 days. Plenty of helpful recordings are no longer active because the creator did not claim them.)

Get folding

It is compatible with  vim and neovim, and is easy to install with whichever plug-in management tool of your choosing.

SEE ALSO: Microservices are in, monoliths are out & everyone should switch to automated testing for CI/CD deployment

The repo’s creator does suggest using the FastFold plugin, however. FastFold helps speed up Vim by updating folds only when absolutely necessary. This a great plug-in that deserves attention even if you decide to forgo Coiled Snake.

Coiled Snake uses all the standard code folding commands. Use :help fold-commands to see all of them.

Several configuration options exist, however none are necessary for running Coiled Snake.

Contributions are more than welcome, so don’t be shy about bug reports and pull requests. On GitHub, it states that, “I’m especially interested to hear about cases where the folding algorithm produces goofy results, since it’s hard to encounter every corner case.” Go on, help reproduce those goofy results!

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat.

Leave a Reply

Your email address will not be published.