-- Autocommands for handling large files and switching between large file buffers and others local largefile_group = vim.api.nvim_create_augroup('largefile', { clear = true }) local max_filesize = 4 * (1024 * 1024) -- 4 MB local old_eventignore = false -- Disables syntax, treesitter and folding on larger files vim.api.nvim_create_autocmd({ 'BufReadPre' }, { group = largefile_group, callback = function(ev) if ev.file then local status, size = pcall(function() return vim.loop.fs_stat(ev.file).size end) if status and size > max_filesize then old_eventignore = vim.o.eventignore vim.o.eventignore = 'FileType' -- Reduce the calls for filetype specific plugins or syntax highlighting vim.b[ev.buf].is_large_file = true vim.bo['bufhidden'] = 'unload' vim.bo['buftype'] = 'nowrite' vim.bo['swapfile'] = false vim.bo['undofile'] = false vim.bo['undolevels'] = -1 end end end, }) vim.api.nvim_create_autocmd({ 'BufWinEnter' }, { group = largefile_group, callback = function(ev) if old_eventignore ~= false then vim.o.eventignore = old_eventignore -- Restore the old setting old_eventignore = false end if vim.b[ev.buf].is_large_file then vim.wo.wrap = false else vim.wo.wrap = vim.o.wrap -- Restore to default setting end end }) -- End large file handling