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.)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
johnmdonahue
  • 653
  • 7
  • 16
  • very cool, and I hope to see more of these - excellent way to discover features. – sa125 Aug 11 '11 at 04:07
  • 1
    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](http://stackoverflow.com/faq#dontask). Might be okay w/ community wiki flag checked. – Merlyn Morgan-Graham Aug 11 '11 at 09:01
  • @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 "http://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. – johnmdonahue Aug 11 '11 at 17:51
  • If you're coming from textmate then you might be interesting in the [snipmate plugin](http://www.vim.org/scripts/script.php?script_id=2540). – Keith Aug 11 '11 at 21:30
  • Thanks @Keith. I've started playing with that a bit as well as [Janus](https://github.com/carlhuda/janus) which is awesome! – johnmdonahue Aug 11 '11 at 22:05

5 Answers5

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.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • This looks promising! Are there other flags available for the tidy command? e.g. -javascript, -php, -ruby etc? – johnmdonahue Aug 11 '11 at 17:55
  • @johnmdonahue: It is called HTML tidy (tidy for short). It is for HTML and now XML documents. http://tidy.sourceforge.net/#docs – Merlyn Morgan-Graham Aug 11 '11 at 19:57
  • @Keith 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. – johnmdonahue Aug 11 '11 at 20:10
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 :!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 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. – johnmdonahue Aug 11 '11 at 22:10
2

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.

exhuma
  • 20,071
  • 12
  • 90
  • 123
Pablo Castellazzi
  • 4,164
  • 23
  • 20
  • =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 . – Nishant Jun 09 '14 at 11:50
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}.

Nishant
  • 20,354
  • 18
  • 69
  • 101
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.

Community
  • 1
  • 1
Meitham
  • 9,178
  • 5
  • 34
  • 45