When I run grep by M-x grep, I get results in a buffer.

When i run grep again M-x grep, the new results overwrite the previous ones in the same buffer.

How can I create a new buffer for the second run of grep, to avoid overwriting the results of the first run of grep?



M-x rename-buffer.

Use M-n to retrieve the current name, and edit it. E.g., M-n puts *grep*in the minibuffer. Type 1 to change it to *grep*1, etc.

Then just use M-x grep again, to have the new search output go to the (new) buffer *grep*.

(I bind rename-buffer to C-M-S-<f1>.)

As @JeanPierre pointed out in a comment, you will need to first make another buffer, besides the grep-output buffer, current (or change the mode of that buffer, so that it is not grep-mode), before you issue the second grep command.

This is because grep uses compilation-buffer-name to determine which buffer to use, and that function returns the name of the current buffer if the buffer is in the same mode as the command - in this case, grep-mode.

If you don't want to change to another buffer manually, you could change to another buffer temporarily as part of a command that wraps grep. Or you can set or bind variable compilation-buffer-name-function to a function that returns "grep". If that variable is non-nil then its function value is used by compilation-buffer-name to determine the name of the buffer to use.

For example:

(defun foo (_mode)
  "Return \"*grep*\" as the name of the buffer to use."

(defun my-grep ()
  "`grep`, but always use buffer `*grep*'."
  (let ((compilation-buffer-name-function  'foo))
    (call-interactively 'grep)))

Then you can use command my-grep instead of grep, even from a buffer that is in mode grep-mode.

(I just use grep from another buffer.)

  • This doesn't work for me if I run the second grep from the just renamed buffer, it will reuse it in this case. So I have to select the *grep* buffer, rename it and go back to another buffer before running grep again. Same with emacs -q.
    – JeanPierre
    Mar 21 '17 at 7:27
  • How about modifying M-x grep to take a prefix arg and create a new buffer, just like C-u M-x shell? Although the devil is in the details and the details might be devilish...
    – NickD
    Mar 21 '17 at 16:04
  • @Nick: You can do that by advising grep instead of redefining it. But why not just define your own command? All you want to change is the interactive interface, not the behavior of the function grep itself.
    – Drew
    Mar 21 '17 at 19:33
  • @Nick Problem is grep already has a use for the prefix arg.
    – JeanPierre
    Mar 22 '17 at 11:04

For the same task, I use:

(global-set-key "\C-s" 'counsel-grep-or-swiper)

Search interactively for what I need, and press C-c C-o to save the results to a buffer, similarly to how grep or occur do it.

A new buffer is generated for each C-c C-o. Each buffer name contains the search string.

  • 1
    Where is this counsel-... command defined?
    – JeanPierre
    Mar 22 '17 at 11:06
  • counsel package on MELPA.
    – abo-abo
    Mar 22 '17 at 16:29

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.