A list of useful Python commands for Vim?
Asked Answered
S

5

20

I was looking for a quick way to autoformat/pretty-print JSON in Vim the other day and found this great little command on Stack Overflow: :%!python -m json.tool

That sent me on a search for a list of other Python tools to pretty-print common web files, but I couldn't find much. Is there a good resource/list of Python tools that they find particularly useful for cleaning up poorly formatted web stuff inside Vim (e.g. HTML, XML, JavaScript, etc.)?

Starnes answered 11/8, 2011 at 0:56 Comment(5)
very cool, and I hope to see more of these - excellent way to discover features.Nasturtium
This is soliciting extended discussion, and of the form "I use this, what do you use?". See the FAQ for why this question is off topic. Might be okay w/ community wiki flag checked.Annora
@Merlyn I see what you mean, but my intent actually was not to start a list of post with individual commands but to just find a link to a resource or wiki that had one already: for example "docs.python.org/cool-pretty-print-goodness.html". I'm new to Vim, switching over from TextMate, and when I found the JSON Python tool I was itching for more but couldn't find a proper resource with more.Starnes
If you're coming from textmate then you might be interesting in the snipmate plugin.Seaborne
Thanks @Keith. I've started playing with that a bit as well as Janus which is awesome!Starnes
A
5

Python

Are you just looking for a resource for Python one-liners? You could browse through the Python standard library documentation to find more inspiration.

Or simply google "python one-liners json.tool" to find additional resources. For example, this Reddit post: Suggestion for a Python blogger: figure out what what all the stdlib main functionality is, and document it

Command line

Vim supports more than just Python (e.g. HTML Tidy as Keith suggested). Any tool that can accept pipe/standard input will integrate well with Vim.

The % command just picks a range that contains the entire file, and ! filters that range through an external program.

See :help :% and :help :!

Annora answered 11/8, 2011 at 20:38 Comment(1)
Perfect. Thanks. I'm new to Vim and haven't played with Python at all so I'm still trying get my bearings but it looks like I just need to play around with different utilities - Python or otherwise - and just find the ones that fit best into my workflow. Thanks for the links, that's exactly what I need - just a jumpstart to some helpful tools.Starnes
S
5

For XHTML and XML files you can use tidy.

:%!tidy -i -asxhtml -utf8

:`<,`>!tidy -i -xml -utf8

The last one works on visual selections.

Seaborne answered 11/8, 2011 at 8:0 Comment(3)
This looks promising! Are there other flags available for the tidy command? e.g. -javascript, -php, -ruby etc?Starnes
@johnmdonahue: It is called HTML tidy (tidy for short). It is for HTML and now XML documents. tidy.sourceforge.net/#docsAnnora
@Seaborne Right! Gotcha. I wasn't sure if there was an extended tidy/pretty-print for other langs as well. Looks like I'll just need to assemble a bit of a tool set for whichever language I need to work with. Still getting the lay of the Vim land here. Thanks for the help.Starnes
A
5

Python

Are you just looking for a resource for Python one-liners? You could browse through the Python standard library documentation to find more inspiration.

Or simply google "python one-liners json.tool" to find additional resources. For example, this Reddit post: Suggestion for a Python blogger: figure out what what all the stdlib main functionality is, and document it

Command line

Vim supports more than just Python (e.g. HTML Tidy as Keith suggested). Any tool that can accept pipe/standard input will integrate well with Vim.

The % command just picks a range that contains the entire file, and ! filters that range through an external program.

See :help :% and :help :!

Annora answered 11/8, 2011 at 20:38 Comment(1)
Perfect. Thanks. I'm new to Vim and haven't played with Python at all so I'm still trying get my bearings but it looks like I just need to play around with different utilities - Python or otherwise - and just find the ones that fit best into my workflow. Thanks for the links, that's exactly what I need - just a jumpstart to some helpful tools.Starnes
M
1

Vim has a command to do it, = (equal), like in ggvG= will reindent the whole file. Try :help = for more info about how to use functions and external programs with =. The default configuration uses internal indenting rules which works for most file types.

Marcosmarcotte answered 11/8, 2011 at 7:32 Comment(1)
=G worked fine for HTML but for Python I could see its breaking the logic by combining if block with rest of the function etc . I prefer the autopep8 route for this .Carbine
C
0

!autopep8 -i % seems to work fine in vim . The -i switch is to over-write the existing file in place. Use more @ autopep8 --help.

There is a vim plugin for this if you really are thinking of being a power user. Outside of vim you can test it with autopep8 -diff {filename} or autopep8 {filename}.

Carbine answered 9/6, 2014 at 11:52 Comment(0)
S
0

There are loads of good tools that are can convert text between the two formats:

  1. par: for hard line wrapping.

  2. pandoc: for HTML, LaTeX, rst, and Markdown

  3. autopep8: for parsing Python code into an AST and spitting it out as pep8 compliant.

    ...

Vim is designed to make use of such utilities by the powerful formatprg settings. That by default is mapped to the gq operator. It works well with Vim motions, Vim text objects, selections, etc.

For instance, I use the setting below for on my Python files

au FileType python setlocal formatprg=autopep8\ --indent-size\ 0\ -

John MacFarlne has a good article about creating a specialised script using pandoc which you could stick in your vimrc.

Schultz answered 28/6, 2014 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.