Open big files and work fast
From Vim Tips Wiki
[edit] Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
Tip 611 • Previous Tip • Next Tip
Created: December 4, 2003 Complexity: intermediate Author: William Minimum version: 5.7 Karma: 23/11 Imported from: Tip#611
When opening big files, having no swap file and no undo levels speeds up Vim dramatically. Here are lines to set that up automatically beyond a given file size threshold (BufSizeThreshold):
let g:SaveUndoLevels = &undolevels
let g:BufSizeThreshold = 1000000
if has("autocmd")
" Store preferred undo levels
au VimEnter * let g:SaveUndoLevels = &undolevels
" Don't use a swap file for big files
au BufReadPre * if getfsize(expand(<afile>)) >= g:BufSizeThreshold | setlocal noswapfile | endif
" Upon entering a buffer, set or restore the number of undo levels
au BufEnter * if getfsize(expand(<afile>)) < g:BufSizeThreshold | let &undolevels=g:SaveUndoLevels | hi Cursor term=reverse ctermbg=black guibg=black | else | set undolevels=-1 | hi Cursor term=underline ctermbg=red guibg=red | endif
endif
We could also disable syntax highlighting and restore it for small files.
[edit] Comments
You should place a " around <afile> or vim will be complaining too load and too much.
Thanks. I have it as expand("%") in my vimrc, but expand("<afile>") is more accurate.
Checking the file size each time one enters the buffer can be very expensive (time delays) when file is stored on shares etc. I use as an extention to tip #343; the following line that makes also auto command on the fly, only for the file that is considered to be large, and remove the auto commands when the file is closed (The variable g:LargeFileUL is the save-value of undo levels):
au BufReadPre * let f=expand("<afile>") | if getfsize(f) > g:LargeFile | set ei=FileType | setlocal noswf bh=unload bt=nowrite ro | let f=escape(substitute(f,'\','/','g'),' ') |exe "au LargeFile BufEnter ". f ." set ul=-1" | exe "au LargeFile BufLeave ". f ." set ul=". LargeFileUL | exe "au LargeFile BufUnload ". f ." au! LargeFile * ". f | exe "" | else | set ei= | endif
It's all one line. I'm not sure if this also works for version 5.x.
The amended script doesn't work for me. I get an error that au LargeFile BufEnter doesn't exist.
TO DO
Check if anything in this tip or VimTip343 is not covered in the LargeFile plugin script#1506. Probably should delete this tip (tip 611), and change tip 343 to cover the concepts, with a link to the LargeFile plugin.
Categories: Duplicate | Review | VimTip | Todo
