Monday, May 03, 2010

Disable syntax highlighting for a large file

Got to edit today a 150MB XML file. That was painful. Tried to disable the syntax highlighting - improved the performance a lot. So I added the trick to my vimrc:

au BufReadPost * if getfsize(bufname("%")) > 512*1024 |
\ set syntax= |
\ endif


That tells VIM to disable syntax (:set syntax=) for any file whose size (while opening, :h BufReadPost) is greater than a half meg (if getfsize(bufname("%")) > 512*1024).

Edit1 A more generic solution is to use the LargeFile script from vim.sf.net, as found out by reader David in the comments below. Starting from version 5 (at the moment of writing available only on author's, Charles Campbell, personal web page.), the script handles properly(*) the buffers with content populated using external commands. The script's main purpose in life is, when opening a large file, to set buffer options to make editing of large files more palatable experience: disable swap file, disable syntax highlighting, discard buffer with large file as soon as it is closed, disable folding, disable undo and so on.

(*) Event used is the BufReadPost, and the size of buffer is evaluated with line2byte(line("$")+1).

9 comments:

David said...

This is a really nice tip.
However it seems it does not work when you open a file via scp.

Ihar Filipau said...

@David, check ':h scp' inside the VIM. It appears that the way network plug-in works is not compatible with standard events. My reading is that the file size on BufReadPost event is initially zero, since the file at the moment not yet transferred (completely) and the buffer is still empty. Check the ':h autocommand-events' for the full list of events. You can try other events, e.g. BufWinEnter.

David said...

Thanks Ihar.
I tried some of the events the help file mentioned, but so far I didn't succeed.
Do you know if there is a way to let vim print out the events it creates except explicitly mentioning every event in my vimrc.

Ihar Filipau said...

Unfortunately I'm not aware of any tracing facility like that in VIM.

I know only about the 'verbose' option (`:h 'verbose'`) and the debug mode (`:h :debug`). But none of them AFAIK trace events.

OTOH if your issue with large files is that severe, you can try to go with the reverse logic: disable the syntax highlighting globally; with auto command enable it only for small/etc files.

David said...

It is not that severe. I was just curious if it is possible :-).
Maybe I'll post on the vim mailing list to see if they have an idea.

Ihar Filipau said...

If you would find something interesting, please drop a word here ;)

David said...

I talked to developer of netrw.
It doesn't seem to be possible in this way.

Ihar Filipau said...

@David. Probably it should be brought up to VIM developers: provide a way for plugins to trigger the events for a buffer (or queue events to be triggered at a point of time when the VIM could do it). Then netrw/etc (that btw also affects reading files from stdin) would be able to simulate (partially) the behavior as if opening a local file.

David said...

Dr Chip has updated his LargeFile Plugin

It now works on remote files, too.
Here is the discussion I had with him about this.