sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #343: Faster loading of large files

 tip karma   Rating 53/31, Viewed by 5241 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 12, 2002 11:32      complexity:   intermediate
author:   Erik Remmelzwaal      as of Vim:   6.0

In the past I experienced long loading times for large files ( size > 10MB )
These files are normally generated by SQL tracing, XML message based
protocols tracing etc.
One of the causes of long loading times was syntax parsing, creating swap file etc.
Normally one want to view these files and remove not relevant details by
deleting lines, but do not want to have undo capabilities and auto recalculation of
syntax highlighting.

The code below, I put in my _vimrc to switch off a number of defaults for
large files.

One can modify the g:LargeFile variable and reload a file to test:
:let g:LargeFile=10
:e

It would be interesting to know if others have more or better suggestions.

" Protect large files from sourcing and other overhead.
" Files become read only
if !exists("my_auto_commands_loaded")
        let my_auto_commands_loaded = 1
        " Large files are > 10M
        " Set options:
        "     eventignore+=FileType (no syntax highlighting etc
        "            assumes FileType always on)
        "       noswapfile (save copy of file)
        "       bufhidden=unload (save memory when other file is viewed)
        "       buftype=nowritefile (is read-only)
        "       undolevels=-1 (no undo possible)
        let g:LargeFile = 1024 * 1024 * 10
        augroup LargeFile
                autocmd BufReadPre * let f=expand("<afile>") | if getfsize(f) > g:LargeFile | set eventignore+=FileType | setlocal noswapfile bufhidden=unload buftype=nowrite undolevels=-1 | else | set eventignore-=FileType | endif
        augroup END
endif

 rate this tip  Life Changing Helpful Unfulfilling 

<< Remap <ESC> | Cut / Copy / Delete / Paste Lines without knowing the number of lines >>

Additional Notes

[email protected], October 13, 2002 18:50

Can we just set scratch buffer for such purpose?

help special-buffers

Yakov, July 22, 2003 6:15
File will load faster if you open it read-only: vim -R HugeFile.txt
BigUser, December 15, 2003 17:24

Try to open a 2 Gig text file. It takes to long and uses all my systems memory.

Looking for a solution.....
Anonymous, June 18, 2004 3:30
I have a 3Ghz machine with 1Gb memory. Its takes 15 minutes to open a 70mb xml file. I had to kill vim after a couple of hours running a :%s/^.*foo.*\n//g script

With visual studio the file opens in 3 seconds (albeit, no syntax hilight) and replaces with regexp take a minute or two.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo