Skip to main content
 首页 » 编程设计

python之在复杂字符串后插入换行符时 Vim 巨大的延迟

2025年04月02日23luoye11

当 Vim 在一个被认为很复杂的特定代码块的末尾插入一个换行符(正常模式下为 o 或插入模式下为 return 键)时,Vim 会花费很长时间来插入一个令人恼火的换行符。

我将如何确定原因并解决问题?

案例特定信息:

就我而言,下面是一个有问题的 Python 代码块,它在双引号字符串中包含多个单引号:

  for item in tree.xpath("//li"): 
    a = item.xpath(".//div[contains(concat(' ', normalize-space(@class), ' '), ' alpha ')]/text()")[0] 
    b = item.xpath(".//div[contains(concat(' ', normalize-space(@class), ' '), ' betahaus ')]/text()")[0] 
    c = item.xpath(".//div[contains(concat(' ', normalize-space(@class), ' '), ' capitalism ')]/text()")[0] 
    d =    item.xpath(".//div[contains(concat(' ', normalize-space(@class), ' '), ' doughnuts-of-the-realm ')]/a")[0].attrib['href'] 
    g = item.xpath(".//span[contains(concat(' ', normalize-space(@type), ' '), ' dontcare ')]/text()")[0] 
    h = item.xpath(".//span[contains(concat(' ', normalize-space(@type), ' '), ' foo ')]/text()") 

延迟不到一秒但很明显。

该机器是 AMD Phenom(tm) 9550 2.2GHz,64 位四核处理器,这是在移动了 .vimrc 的 Arch Linux 上(因此使用 Arch 的 vim 默认值)。 vim 和 gvim 都会受到影响。

如果我将定义变量的行复制粘贴 5 次,得到大约 48 行,则延迟为 3 秒。增加到超过 400 行会导致相同的延迟,这让我假设已经达到超时。

显示问题的视频:https://youtu.be/rCSfSASrZjQ

请您参考如下方法:

可能与语法高亮有关;在 :syntax off 之后检查延迟是否消失。

如果您的 Vim 版本(具有“巨大”功能的最新版本)支持 :syntime 命令,您可以深入研究; cp。 :帮助:同步时间。

这可能会导致运行缓慢;然后您将联系语法插件作者(您可以在脚本的标题中找到问题跟踪器的地址/链接:$VIMRUNTIME/syntax/python.vim。