qubodup qubodup - 5 months ago 26
Python Question

Vim huge delay when inserting newline after complex strings

Vim takes an annoyingly noticeable long time to insert a newline (

in normal mode or return key in insert mode) when inserted at the end of a specific code block that could be considered complex.

How would I go about identifying the cause and fixing the problem?

Case-specific information:

I my case, a problematic Python code block is the following, which contains multiple single quotes in double-quoted strings:

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()")

The delay is less than a second but noticeable.

The machine is a AMD Phenom(tm) 9550 2.2GHz, 64bit Quad-Core Processor and this is on Arch Linux with .vimrc moved (so Arch's vim defaults are used). Both vim and gvim are affected.

If I copy paste the lines defining variables 5 times, resulting in about 48 lines, the delay is 3 seconds long. Increasing to over 400 lines causes the same delay which makes me assume there's a timeout that is being reached.

A video showing the issue: https://youtu.be/rCSfSASrZjQ


It's likely related to syntax highlighting; check whether the delay is gone after :syntax off.

If your Vim version (recent ones with "huge" features) supports the :syntime command, you can dive deeper; cp. :help :syntime.

This may turn up a pattern that is responsible for the slowness; you'd then contact the syntax plugin author (whose address / link to issue tracker you'll find in the script's header: $VIMRUNTIME/syntax/python.vim.