适用于 Vim 的有用 Python 命令列表?
前几天,我正在寻找一种在 Vim 中自动格式化/漂亮打印 JSON 的快速方法,并在 StackOverflow 上发现了这个很棒的小命令: :%!python -m json.tool
这让我开始搜索其他 Python 工具的列表,以漂亮地打印常见的 Web 文件,但我找不到太多。他们发现是否有一个很好的资源/Python 工具列表,对于清理 Vim 内格式不良的 Web 内容(例如 HTML、XML、JavaScript 等)特别有用?
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.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python
您是否正在寻找有关 Python 俏皮话的资源?您可以浏览Python 标准库文档来寻找更多灵感。
或者简单地搜索“python one-liners json.tool”来查找其他资源。例如,这篇 Reddit 帖子:给 Python 博主的建议:figure找出所有 stdlib 主要功能是什么,并记录它
命令行
Vim 支持超过只是Python(例如HTML Tidy,正如Keith建议的那样)。任何可以接受管道/标准输入的工具都可以与 Vim 很好地集成。
%
命令只是选择包含整个文件的范围,并且!
通过外部程序过滤该范围。请参阅
:help :%
和:help :!
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 :!
对于 XHTML 和 XML 文件,您可以使用 tidy。
最后一个涉及视觉选择。
For XHTML and XML files you can use tidy.
The last one works on visual selections.
Vim 有一个命令可以做到这一点,
=
(等于),就像ggvG=
中一样,将重新缩进整个文件。尝试:help =
了解有关如何通过=
使用函数和外部程序的更多信息。默认配置使用适用于大多数文件类型的内部缩进规则。Vim has a command to do it,
=
(equal), like inggvG=
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.有很多很好的工具可以在两种格式之间转换文本:
par:用于硬换行。
pandoc:适用于 HTML、LaTeX、rst 和 Markdown
autopep8:用于将 Python 代码解析为 AST< /a> 并将其吐出为符合 pep8 标准。
...
Vim 旨在通过强大的
formatprg
设置来使用此类实用程序。默认情况下映射到 gq 运算符。它适用于 Vim 动作、Vim 文本对象、选择等。例如,我在我的 Python 文件上使用下面的设置
John MacFarlne has 一篇关于使用 pandoc 创建专用脚本的好文章,您可以将其粘贴到 vimrc 中。
There are loads of good tools that are can convert text between the two formats:
par: for hard line wrapping.
pandoc: for HTML, LaTeX, rst, and Markdown
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
John MacFarlne has a good article about creating a specialised script using pandoc which you could stick in your vimrc.
!autopep8 -i %
似乎在vim
中工作正常。-i
开关用于覆盖现有文件。使用more @ autopep8 --help
。如果您确实想成为高级用户,可以使用
vim
插件来实现此目的。在vim
之外,您可以使用autopep8 -diff {filename} 或 autopep8 {filename}
进行测试。!autopep8 -i %
seems to work fine invim
. The-i
switch is to over-write the existing file in place. Usemore @ autopep8 --help
.There is a
vim
plugin for this if you really are thinking of being a power user. Outside ofvim
you can test it withautopep8 -diff {filename} or autopep8 {filename}
.