Is there any way to get vim to auto wrap python strings at 79 chars?

房东的猫 提交于 2019-12-17 15:23:18

问题


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.

Example:

  1. I type the following text:

    mylongervarname = "my really long string here so please wrap and quote
    automatically"
    
  2. Vim automatically does this when I hit column 80 with the string:

    mylongervarname = ("my really long string here so please wrap and "
                       "quote automatically")
    

回答1:


More a direction than a solution.

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.

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.



来源:https://stackoverflow.com/questions/1314174/is-there-any-way-to-get-vim-to-auto-wrap-python-strings-at-79-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!