Search code examples
pythonvimpretty-printautoformatting

A list of useful Python commands for Vim?


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


Solution

  • 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 :!