In this tutorial, we’ll learn how to open and edit large files in Vim in a performant way. First, we’ll tackle the built-in options of Vim without the use of any plugins. Then, we’ll use the LargeFile plugin to increase the performance of editing large files.
2. Opening Large Files in Vim
Usually, Vim can handle large files very efficiently. We can edit huge files that are around 10 MB or more without any issues. However, as the file becomes larger, Vim will become clunky and a bit unresponsive — depending on the file size and the CPU.
There is no dedicated way to work around this issue. However, there are steps that we can take to somehow speed things up in both Vim and NeoVim. We’ll discuss these in the next sections.
2.1. Turning Off Plugins
We can launch Vim with no plugins. Not only does this decrease the startup time of Vim, but it also opens buffers and files in an efficient way.
Let’s edit a file that’s larger than 10 MB. For that reason, we’ll dump the system log into a plain text file:
$ journalctl --no-tail > /tmp/log.txt
Now, let’s check its file size:
$ du -h /tmp/log.txt
Let’s open this in Vim without the plugins:
$ vim -u NONE /tmp/log.txt
The -u option is used to specify the path to the config file. In this case, we’ll load Vim without the default configurations. This way, the startup time will decrease because there are no plugins and additional configurations to load.
Moreover, the -u option will also drop the compatibility layer for the classic vi editor and run Vim in the “VI iMproved” mode.
2.2. Turning Off Visual Enhancements
Vim takes up resources to parse a file and enhance the visuals. Surely, this slows down editing large files. Therefore, we should disable them inside Vim or put the options in the ~/.vimrc file.
By default, Vim will open the supported files with syntax highlighting enabled. We can turn off the syntax highlighting for huge files with the set syntax command:
: set syntax=off
Additionally, we should also turn off the filetype option:
: set ft=
If we don’t require line wrapping, then we can turn it off as well:
: set nowrap
For large source code files, we can disable the code folding:
: set nofoldenable
2.3. Separate Config File for Large Files
Typing these options by hand is a bit of work. Of course, we can put this in the .vimrc file. But, then again, we’ll have to change it once we no longer need these options. Therefore, we can create a separate Vim config file and put these options there:
" Disable line wrapping
" Disable code folding
" Disable filetype detection
" Disable syntax highlighting
" Fast scrolling
" Disable text highlighting on search
Now, we can save this file anywhere on our machine and load Vim with this config file when we need it:
$ vim -u ~/.optimized.vimrc [FILE PATH]
3. The LargeFile Plugin
The above workaround should work with NeoVim as well. However, there is also the LargeFile plugin written solely for Vim that can help us edit large files. We can simply download the latest version of the archive and decompress it:
$ gunzip LargeFile.vba.gz
Now, let’s open the Vimball in Vim:
$ vim LargeFile.vba
Once we open the file, Vim will recognize it as a Vimball that we can source:
: source %
After sourcing the plugin, Vim will move the appropriate file to the plugin’s directory. Therefore, we don’t need to source it again when we use it. After the installation, we’re now ready to edit huge files with good performance.
4. Extra: Using more and less for Reading Large Files
It’s not necessary to use Vim for reading huge files because searching through the file and search highlights can be resource intensive. For that reason, we should simply use a pager utility like less or more.
We can simply load the file and read through it without any issues. Pagers are usually more performant as compared to using Vim for read-only purposes:
$ less /tmp/log.txt
In this article, we saw how to open large files in Vim and NeoVim. First, we learned how to disable the built-in options to gain performance. Then, we looked at the LargeFile plugin that works only in Vim.
Finally, we briefly discussed how using more and less is more performant when reading large files.