28

Emacs sometimes hangs when viewing large file. But it is fast with (global-font-lock-mode -1).

I'm using a fork of Prelude.

Emacs version: 24.3 cocoa System: OS X 10.8.4

Update: I found (setq jit-lock-defer-time 0.05) is a method to improve the scrolling speed.

6
  • 1
    You've anwered your own question. font-lock is slow. Also, find-file-literally is good since it puts you in fundamental mode.
    – abo-abo
    Aug 19, 2013 at 14:52
  • @abo-abo Then your recommendation is to disable font-lock-mode?
    – goofansu
    Aug 19, 2013 at 15:08
  • 1
    @abo-abo My large file is about 8000 lines. I think it is normal.
    – goofansu
    Aug 19, 2013 at 15:09
  • 4
    I usually use find-file-literally. It disables not only font-lock, but also the major-mode. I can call it from dired when I see that the file size is large.
    – abo-abo
    Aug 19, 2013 at 15:13
  • 1
    The speed can be influenced by so many things it's hard to tell. But 8000 lines is not "too large", so you can consider this problem as a bug and report it accordingly.
    – Stefan
    Aug 21, 2013 at 16:24

4 Answers 4

32

To help with large files, I've installed my own find-file-hook which turns on fundamental mode (avoids font-lock), turns off undo, and makes the buffer read-only just to avoid any accidental changes (making unnecessary backups of large files).

(defun my-find-file-check-make-large-file-read-only-hook ()
  "If a file is over a given size, make the buffer read only."
  (when (> (buffer-size) (* 1024 1024))
    (setq buffer-read-only t)
    (buffer-disable-undo)
    (fundamental-mode)))

(add-hook 'find-file-hook 'my-find-file-check-make-large-file-read-only-hook)

Obviously adjust the threshold value as you see fit.

4
  • 2
    Thank you. But I also want to edit this file. You can see my above description of a "large" file.
    – goofansu
    Aug 19, 2013 at 15:13
  • @goofansu Sure, just take out the setting of buffer-read-only... glad it works for you Aug 19, 2013 at 16:44
  • 3
    Just using (linum-mode -1) after the checking size is enough, if not, adding (font-lock-mode -1) would be.
    – CodyChan
    Oct 14, 2014 at 0:26
  • It will treat all images and pdfs larger than specified value to be opened in fundamental mode.
    – azzamsa
    Nov 6, 2018 at 7:38
18

If you need to work with really large files, you can use the View Large Files package which allows "viewing, editing and searching in large files in chunks." After requireing the package open large files with M-x vlfi.

0
4

I usually unroll long lines and indent by tags (like HTML, XML, JSON).

In order to make such operation possible I add:

(setq line-number-display-limit large-file-warning-threshold)
(setq line-number-display-limit-width 200)

(defun my--is-file-large ()
  "If buffer too large and my cause performance issue."
  (< large-file-warning-threshold (buffer-size)))

(define-derived-mode my-large-file-mode fundamental-mode "LargeFile"
  "Fixes performance issues in Emacs for large files."
  ;; (setq buffer-read-only t)
  (setq bidi-display-reordering nil)
  (jit-lock-mode nil)
  (buffer-disable-undo)
  (set (make-variable-buffer-local 'global-hl-line-mode) nil)
  (set (make-variable-buffer-local 'line-number-mode) nil)
  (set (make-variable-buffer-local 'column-number-mode) nil) )

(add-to-list 'magic-mode-alist (cons #'my--is-file-large #'my-large-file-mode))

Note that I don't use find-file-hooks as magic-mode-alist usually empty and have priority. If I add find-file-hooks it first validate XML file by nxml-mode and then switch to fundamental-mode.

I split line by regex, for XML it: C-M-% >< RET >NL< RET !.

After Emacs split long lines - it is possible to enable many *-modes and re-indent code.

2
  • 1
    this answer helped, but for me disabling bidi-display-reordering was enough
    – packet0
    Sep 25, 2016 at 5:29
  • @gavenkoa, Thanks for this config. I tried this config but it doesn't work well when I have (global-display-line-numbers-mode) in my init.el. I also tried changing that to (add-hook 'find-file-hook #'display-line-numbers-mode) so that it doesn't interfere with your config but still it doesn't work. Could you suggest a better approach? Mar 17, 2019 at 19:23
-1

it may be trick workaround
M-x shell
then
less your_large_file.txt
and you can proceed

2
  • Thanks, I'm using spacemacs now. When opening a large file, it will warn me in the minibuffer.
    – goofansu
    Feb 16, 2019 at 18:09
  • 1
    less wants a better terminal than the dumb terminal provided by M-x shell. M-x term would work.
    – phils
    Feb 17, 2019 at 9:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.