有没有办法让vim自动包装79个字符的python字符串? [英] Is there any way to get vim to auto wrap python strings at 79 chars?
问题描述
我发现了这个关于包装字符串的答案使用括号非常有用,但是 Vim 有没有办法让这件事自动发生?我想在一个字符串中,打字,然后让 Vim 在我的字符串周围放置括号并根据需要将其包裹起来.对我来说,这将是一个巨大的节省时间,因为我花了很多时间手动包装长字符串.提前致谢.
I found this answer about wrapping strings using parens extremely useful, but is there a way in Vim to make this happen automatically? I want to be within a string, typing away, and have Vim just put parens around my string and wrap it as necessary. For me, this would be a gigantic time saver as I spend so much time just wrapping long strings manually. Thanks in advance.
示例:
我输入以下文本:
I type the following text:
mylongervarname = "my really long string here so please wrap and quote
automatically"
当我使用字符串点击第 80 列时,Vim 会自动执行此操作:
Vim automatically does this when I hit column 80 with the string:
mylongervarname = ("my really long string here so please wrap and "
"quote automatically")
推荐答案
与其说是解决方案,不如说是方向.
More a direction than a solution.
使用 'formatexpr'
或 'formatprg'
.当一行超过 'textwidth'
并通过了由 'formatoptions'
设置的标准时,它们将被使用(如果设置)来断行.唯一真正的区别是 'formatexpr'
是一个 vimscript 表达式,而 'formatprg'
通过外部程序过滤该行.
Use 'formatexpr'
or 'formatprg'
. When a line exceeds 'textwidth'
and passes the criteria set by the 'formatoptions'
these are used (if set) to break the line. The only real difference is that 'formatexpr'
is a vimscript expression, while 'formatprg'
filters the line through an exterior program.
因此,如果您知道一种格式化程序可以对 Python 代码行进行这种转换,或者愿意编写一个格式化程序,这将为您提供一个钩子来执行它.而且由于 vim 支持 python(参见 :help python
),你甚至可以用 python 编写你的 python 格式化程序.
So if you know of a formatter that can do this transformation to lines of python code, or are willing to write one, this will give you a hook to have it executed. And since vim supports python (see :help python
) you can even write your python formatter in python.
这篇关于有没有办法让vim自动包装79个字符的python字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!