简体   繁体   English

Vim 的有用 Python 命令列表?

[英]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前几天我正在寻找一种在 Vim 中自动格式化/漂亮打印 JSON 的快速方法,并在 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.这让我开始搜索其他 Python 工具的列表,以漂亮地打印常见的 web 文件,但我找不到太多。 Is there a good resource/list of Python tools that they find particularly useful for cleaning up poorly formatted web stuff inside Vim (eg HTML, XML, JavaScript, etc.)? Is there a good resource/list of Python tools that they find particularly useful for cleaning up poorly formatted web stuff inside Vim (eg HTML, XML, JavaScript, etc.)?

For XHTML and XML files you can use tidy.对于 XHTML 和 XML 文件,您可以使用 tidy。

:%!tidy -i -asxhtml -utf8

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

The last one works on visual selections.最后一个适用于视觉选择。

Python Python

Are you just looking for a resource for Python one-liners?您是否只是在寻找 Python 单线的资源? You could browse through the Python standard library documentation to find more inspiration.您可以浏览Python 标准库文档以找到更多灵感。

Or simply google "python one-liners json.tool" to find additional resources.或者简单地谷歌“python one-liners json.tool”来查找其他资源。 For example, this Reddit post: Suggestion for a Python blogger: figure out what what all the stdlib main functionality is, and document it例如,这个 Reddit 帖子: Python 博主的建议:弄清楚 stdlib 的所有主要功能是什么,并记录下来

Command line命令行

Vim supports more than just Python (eg HTML Tidy as Keith suggested). Vim 支持的不仅仅是 Python(例如,Keith 建议的HTML Tidy )。 Any tool that can accept pipe/standard input will integrate well with Vim.任何可以接受管道/标准输入的工具都可以与 Vim 很好地集成。

The % command just picks a range that contains the entire file, and ! %命令只选择一个包含整个文件的范围,并且! filters that range through an external program.过滤范围通过外部程序。

See :help:% and :help:!请参阅:help:%:help:!

Vim has a command to do it, = (equal), like in ggvG= will reindent the whole file. Vim 有一个命令, = (等于),就像在ggvG=中一样,将重新缩进整个文件。 Try :help = for more info about how to use functions and external programs with = .尝试:help =以获取有关如何使用=的函数和外部程序的更多信息。 The default configuration uses internal indenting rules which works for most file types.默认配置使用适用于大多数文件类型的内部缩进规则。

!autopep8 -i % seems to work fine in vim . !autopep8 -i %似乎在vim中工作正常。 The -i switch is to over-write the existing file in place. -i开关用于覆盖现有文件。 Use more @ autopep8 --help .使用more @ autopep8 --help

There is a vim plugin for this if you really are thinking of being a power user.如果您真的想成为高级用户,那么有一个vim插件。 Outside of vim you can test it with autopep8 -diff {filename} or autopep8 {filename} .vim之外,您可以使用autopep8 -diff {filename} or autopep8 {filename}进行测试。

There are loads of good tools that are can convert text between the two formats:有很多很好的工具可以在两种格式之间转换文本:

  1. par: for hard line wrapping. par:用于硬换行。
  2. pandoc: for HTML, LaTeX, rst, and Markdown pandoc:适用于 HTML、LaTeX、rst 和 Markdown
  3. autopep8: for parsing Python code into an AST and spitting it out as pep8 compliant. autopep8:用于将 Python 代码解析为AST并将其吐出为符合 pep8 标准。

    ... ...

Vim is designed to make use of such utilities by the powerful formatprg settings. Vim 旨在通过强大的formatprg设置利用此类实用程序。 That by default is mapped to the gq operator.默认情况下,它映射到 gq 运算符。 It works well with Vim motions, Vim text objects, selections, etc.它适用于 Vim 动作、Vim 文本对象、选择等。

For instance, I use the setting below for on my Python files例如,我对我的 Python 文件使用以下设置

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. John MacFarlne 有一篇关于使用 pandoc 创建专门脚本的好文章,您可以将其粘贴在 vimrc 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM