Editing xml files with long lines is really slow in vim. What can I do to fix this?

XmlVimPerformance

Xml Problem Overview


I edit a lot of xml files with vim. The problem is that, because of the long lines, navigation/editing in vim is extremely slow. Is there something I can do (besides turning off syntax highlighting/filetype plugins and filetype indentation) to be able to edit these files without all that lag?

It's really frustrating that a trivial thing such as syntax highlighting is being handled so poorly by vim. I don't remember this being an issue with any other editor. I really like using vim and I hope there is some way to fix this.

Xml Solutions


Solution 1 - Xml

The problem is that VIM syntax-highlighting is slow for long lines. An easy fix that only slightly degrades functionality is to limit syntax highlighting to the first x columns. Something like this in your .vimrc:

set synmaxcol=120

Solution 2 - Xml

It is 2014, I am using Vim version 7.4. the syntax highlighting and Long line combination still causes vim to behave unacceptably slow. As this is the first response returned from Google I wanted to drop my "current" solutions.

  • I have found that simply toggling your syntax on and off after loading the offending file allows vim to behave at an acceptable pace. For convenience you can bind it

    :syntax off :syntax on

    or bind it: nnoremap <leader>ts :syntax off<cr>:syntax on<cr>

  • I have also found that sourcing my .vimrc will produce the same result

    :source $MYVIMRC

    and obligatory map: nnoremap <leader>sv :source $MYVIMRC<cr>

EDIt ---- 07/31/14

Further exploration has lead me to limit the syntax with a maximum column. This has worked very well and I haven't had any problems since I've add the below to my vimrc.

set synmaxcol=250

this limits syntax generously to the first 250 columns.

Solution 3 - Xml

:set nocursorline

should help.

Solution 4 - Xml

Do you have line wrapping disabled? In my experience line wrapping can slow vim down a bit when working with very long lines.

set nowrap

Solution 5 - Xml

How about pretty-printing your XML file (if the line length is the real problem)? You could do that e.g. using xmllint which is part of the Gnome libxml2 package (and there is also a version available for Windows).

You can pretty-print in-place by executing

xmllint --format -o xmlFile.xml xmlFile.xml

Solution 6 - Xml

Nope. It's the syntax highlighting think, AFAIK. Regex approach Vim is using is not the optimal solution, really, for editing xml files.

(of course, you can always try writing your own syntax file for xml, in hope you'll do a better job)

Solution 7 - Xml

The simplest and most effective solution I have found is to simply disable syntax highlighting:
syntax off

This seems to be the culprit when dealing with long lines. Also, from my experience with vim and xml, the size of the file doesn't seem to matter - it's the long lines that cause these slowdowns.

Another work-around I found useful is to wrap areas with long lines in folds:

  <!--{{{ long lines -->
  <text>A reeealy long line</text>
  <!--}}}-->

Closing the folds will spare vim from parsing the syntax of those lines. Of course, this approach is not always practical, but it worked very well where I had only a few long lines, or they were in a specific area of the file.

Often, Vim is still noticeably slower, but in most cases the performance becomes acceptable.

Solution 8 - Xml

There is a plugin, LargeFile for the job. It disables some events, syntax highlighting and even undo. You did not mention about the size of the XML files, but the plugin is configurable. You can set the size of a "large file" in megabytes so that "files that are not large" can be treated normally.

Solution 9 - Xml

I often replace >< with >\r< -> :s/>\s*</>\r</g and then reindent the whole file with gg=G.

Solution 10 - Xml

Comment out the line

syn sync match xmlSyncDT grouphere  xmlDocType +\_.\(<!DOCTYPE\)\@=+

in your xml.vim file (with ").

This kind of issue can be debugged in a vim session by typing :syntime on, doing something that demonstrates the slowness of concern, and then :syntime report. In my case it reported xmlSyncDT taking over 10 seconds in my 6MB xml file with 4000-character lines just to display the last page of the file. Commenting out the line above has not affected syntax highlighting as far as I've noticed except that it now never takes more than a fraction of a second to display a screen.

Solution 11 - Xml

Add to vimrc file

nmap <leader>x <Esc>:set filetype=xml<CR>:%s/></>\r</g<CR><ESC>gg=G<Esc>:noh<CR>

pressing x will then automatically prettyprint the xml file.

Solution 12 - Xml

You can this function to your .vimrc to reformat your xml file and hopefully reduce line length.

function! DoPrettyXML()
  " save the filetype so we can restore it later
  let l:origft = &ft
  set ft=
  " delete the xml header if it exists. This will
  " permit us to surround the document with fake tags
  " without creating invalid xml.
  1s/<?xml .*?>//e
  " insert fake tags around the entire document.
  " This will permit us to pretty-format excerpts of
  " XML that may contain multiple top-level elements.
  0put ='<PrettyXML>'
  $put ='</PrettyXML>'
  silent %!xmllint --format -
  " xmllint will insert an <?xml?> header. it's easy enough to delete
  " if you don't want it.
  " delete the fake tags
  2d
  $d
  " restore the 'normal' indentation, which is one extra level
  " too deep due to the extra tags we wrapped around the document.
  silent %<
  " back to home
  1
  " restore the filetype
  exe "set ft=" . l:origft
endfunction

command! PrettyXML call DoPrettyXML()

Solution 13 - Xml

It's caused by long line parsing in vim. I finally found that if I remove following from my .vimrc, the problem solved:

filetype indent plugin on

Note that it doesn't work if you type in :filetype indent plugin off when editing a file with long line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrandomView Question on Stackoverflow
Solution 1 - XmlGabe MoothartView Answer on Stackoverflow
Solution 2 - XmlBenJaminView Answer on Stackoverflow
Solution 3 - XmldottomiView Answer on Stackoverflow
Solution 4 - XmlJohn CarterView Answer on Stackoverflow
Solution 5 - XmlDirk VollmarView Answer on Stackoverflow
Solution 6 - XmlRookView Answer on Stackoverflow
Solution 7 - XmlMihai RotaruView Answer on Stackoverflow
Solution 8 - XmlCaglar TokluView Answer on Stackoverflow
Solution 9 - XmlLuc HermitteView Answer on Stackoverflow
Solution 10 - XmlJames AshtonView Answer on Stackoverflow
Solution 11 - XmlcenterbackView Answer on Stackoverflow
Solution 12 - XmlautodidacticonView Answer on Stackoverflow
Solution 13 - XmlDeqingView Answer on Stackoverflow