When I grep inside emacs, it begins to be completelly laggy when lines are too long (just try to grep in your home emacs directory and you will see what I mean). How to set limit to line lengths in grep output buffer?

The effect can be reproduced with the following emacs lisp form. One can run the code in the scratch buffer.

(let ((file (make-temp-file "greptest")))
  (with-temp-file file
    (dotimes (i 100)
      (dotimes (j 100)
        (dotimes (k 100)
          (insert (string (+ 32 (random (- 127 32))))))
        (insert "test"))
      (insert "\n")))
  (grep (concat "grep --color -nH --text -e test " file))

It is clear that the effect depends on the performance of the computer. So it might be that you need to play around with the numbers to see the lagging.

  • 1
    Here is a link that I found with Googling: grep truncate. "How to truncate long matching lines returned by grep or ack": stackoverflow.com/questions/2034799/… It's not an Emacs solution, but perhaps it would help.
    – lawlist
    Commented Dec 2, 2017 at 23:51
  • @lawlist I think your comment makes a good answer. I tested it. The only small problem with the option -oE is that it highlights also the surrounding characters. That might be acceptable.
    – Tobias
    Commented Dec 3, 2017 at 5:15

1 Answer 1


I too had this recurring issue with grep/projectile-grep. When a result contained extremely long lines, the *grep* buffer become bogged down and largely unresponsive while Emacs read the find/grep output from the single-line 10MB file. Commonly software project directories contain extremely large files that aren't neatly broken into consumable lines (ex: minified source, source maps, or other textual asset files).

I'm aware of a few strategies for avoiding this circumstance on the command line (ex: https://stackoverflow.com/questions/2034799/how-to-truncate-long-matching-lines-returned-by-grep-or-ack). Personally I use some variation of cut -c 1-80.

I worked out one solution using this strategy by modifying the grep-find-template value to pipe the output through cut and that seems to work. Interestingly (as noted by the docstring), you can't simply (setq grep-find-template ...). You need to use the grep-apply-setting helper:

(grep-apply-setting 'grep-find-template
  (concat grep-find-template " | cut -c 1-2000"))

Your Answer

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

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