Users of Emacs sometimes run into a performance issue which they didn't know about before, but is well known: Working with files which include long lines. If the lines are long enough - which can just be a couple thousand characters - Emacs will crawl to a halt whilst utilising 100% CPU. After reading this guide, you will be well prepared to tackle even these files in Emacs!
This issue is not new and due to the fact that Emacs scans every line of text multiple times for layout concerns. For example, it will check what the highest glyph in the line is and it will check for each paragraph if it should be rendered from left-to-right or from right-to-left. This enables some cool functionality like mixing Arabic languages with non-Arabic languages. However, it comes at a cost.
Having said that, do not worry - Emacs has you covered. Here are your options on how to handle files with long lines anyway:
Emacs supports bidirectional editing which means that scripts, such as Arabic, Farsi, and Hebrew, whose natural ordering of horizontal text for display is from right to left. However, digits and Latin text embedded in these scripts are still displayed left to right.
Whilst this is a great feature, it adds to the amount of line scans that Emacs has to do for rendering text. Too many line scans will cause Emacs to hang. If you normally do not work with right-to-left or left-to-right languages, then you can default to displaying all paragraphs in your preferred manner. For example, to enable left-to-right as a default, this is the configuration:
(setq-default bidi-paragraph-direction 'left-to-right)
There's another feature related to bidirectional editing: The Bidirectional Parentheses Algorithm. Disabling the BPA makes redisplay faster, but might produce incorrect display reordering of bidirectional text with embedded parentheses and other bracket characters whose 'paired-bracket' Unicode property is non-nil. Again, if you're usually not working with files that include left-to-right and right-to-left languages at the same time, disabling this gains some performance:
(if (version<= "27.1" emacs-version) (setq bidi-inhibit-bpa t))
This will already bring a bit of performance due to fewer line scans, but solves only part of the problem, because Emacs will still perform multiple line scans for other reasons.
Opening files with very long lines
When you open a file with very long lines, you have multiple options on how to handle the situation:
- If you need to open a file with very long lines and you already
have a hunch that Emacs will get stuck, instead of using the normal
find-file(Usually bound to
C-x C-f), you can use
find-file-literally. This will visit the opened file with no conversion of any kind and been available at or before Emacs version 20.1.
- If you're working in a file where the major mode is highly
relevant, maybe because you're using IDE-like features, then you
can manually disable minor modes that are contributing to the CPU
intensive scanning. Most prevalent in that is
font-lock-mode. If the load is still too high, here's a list of other minor modes that are known to have this effect (the list is taken from the code in so-long.el):
- ;; Known third-party modes-of-interest:
- If you're working with a file with very long lines, but the major
JSON or log files), then you can use
so-long mode. It will revert into a very rudimentary major mode and disable all potentially slow minor modes.
so-longhas been introduced in Emacs 27; if you're on an earlier version, you can install it through GNU Elpa. When done, you can revert into your old mode setup with
Last but not least: You can configure Emacs to be smart about opening files with long lines. If Emacs thinks that a file you're opening could trigger performance problems, it will automatically toggle automated performance mitigations.
When the lines in a file are so long that performance could suffer to an unacceptable degree, we say "so long" to the slow modes and options enabled in that buffer, and invoke something much more basic in their place.
(if (version<= "27.1" emacs-version) (global-so-long-mode 1))
That's it, now you're set up to view and edit any kind of file shape or form that the world might throw at you(;
These flags and more configuration can be seen in my Emacs configuration repository on Github which is documented in a literate programming.
Enjoy and happy text processing!
Update "Introduction to profiling in Emacs" blog post and video.: If you're interested in how to actually measure the time Emacs takes to do various tasks (like rendering text), we have added a
If you're into Emacs, chances are that you're also into Org mode. We're building a free and open source implementation of Org mode without the dependency of Emacs - built for mobile and desktop browsers: https://github.com/200ok-ch/organice/