3
$ grep -nrH would . | tee file.txt
$ emacs file.txt

If I have a buffer with grep search results, how can I make Emacs jump to the file and line number on the cursor line, similar to diff-goto-source in diff-mode?

./path/to/my/one.txt:1234 RMS and YAK would hit it off
./other/two.txt:5678 They sure would

Furthermore, when I'm visiting the first file, one.txt, how can I make Emacs jump to the next file and line in the grep buffer, two.txt?

4

The grep parameters you are using should be adequate. Just do M-x grep-mode after opening your results file and you should get the links you desire.

You could also skip writing the grep results to a file and use a command like grep-find directly.

In grep mode, n/p are bound to next-error-no-select/previous-error-no-select respectively (open the file and line for the next/previous result).

Also (as @InHarmsWay points out): M-g n/M-g p have global bindings to next-error and previous-error, so you can move between grep results without switching back to your grep results buffer!

  • Ah! That's lovely! I didn't find anything on grep because it wasn't loaded. But (require 'grep) + grep-mode = Lovely workflow with split windows. I'm almost crying here... 1) I have to do C-x o before I can do n. Is there a key sequence to jump to the next grep item without switching back to the grep buffer? 2) I can't edit the grep buffer! Normal typing says <key> is undefined How do I make the grep buffer editable? (If full editing is a problem, the specific need is being able to delete lines.) – forthrin Mar 29 '18 at 13:52
  • Those constraints certainly make things trickier. 1) You probably need to define your own command to enable the navigation you have in mind. 2) You can make it writable with toggle-read-only, but I'm not sure how to fix the insertion behavior. Also: you might want to check out helm-swoop or helm-ag. – ebpa Mar 29 '18 at 13:57
  • I'll switch to fundamental-mode to do editing as a workaround. I'll leave the question open to see if someone has suggestions. The grep buffer is sort of a "To Do" list, so I want to delete items as part of my workflow. – forthrin Mar 29 '18 at 16:06
  • I could be wrong, but there are probably text properties that are added contemporaneously as the grep results are normally being generated within Emacs. It is difficult for me to imagine that simply enabling a major mode would cause Emacs to scan the buffer and lay those text properties with page references and buffer-file-names .... I would imagine that there is a process filter that monitors the grep output as it is being generated and places text properties during that potentially very lengthy output ... – lawlist Mar 29 '18 at 19:05
  • M-g n and M-g p should allow you to visit successive grep entries even when not in the grep buffer. – InHarmsWay Mar 30 '18 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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