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

后端 未结 13 940
情书的邮戳
情书的邮戳 2021-01-31 02:24

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

相关标签:
13条回答
  • 2021-01-31 02:46

    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.

    0 讨论(0)
  • 2021-01-31 02:46

    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()
    
    0 讨论(0)
  • 2021-01-31 02:47

    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.

    0 讨论(0)
  • 2021-01-31 02:48

    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)

    0 讨论(0)
  • 2021-01-31 02:48

    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.

    0 讨论(0)
  • 2021-01-31 02:49

    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
    
    0 讨论(0)
提交回复
热议问题