Sometimes, I want to insert the result of an Emacs command (that has been echoed in the echo area) to another buffer or another running X program. So, I'd like to put it to the kill-ring. What would be a convenient way to do this?

For example: I could run a query with a shell command while in dired mode, say: !rpm -qf (to find out which package owns the selected file in the directory listing), and then want to insert the result somewhere else.

Or, another example: if I needed the filename of the current buffer (as in Is there a user interface in Emacs allowing one to "grab" the buffer's filename conveniently?), and there was not yet any predefined command for this, I could at least do M-:(buffer-file-name) and then use this general-purpose way to copy the shown result to the kill-ring in order to paste it later. (Of course, I could eval (kill-new (buffer-file-name)), but this example here is to illustrate what kind of general-purpose way to do the grabbing of the echoed result I'm looking for.)

3 Answers 3


Type C-u before either M-: or M-! to get the result inserted instead of sent to the echo area.

To get things directly into the kill ring, you need to dabble in Elisp. Something like this (untested):

;;; kill ring version of M-!
(defun shell-command-to-kill-ring (command)
        (read-shell-command "Shell command: " nil nil
                (let ((filename
                    ((eq major-mode 'dired-mode)
                     (dired-get-filename nil t)))))
                  (and filename (file-relative-name filename))))
    (kill-new (shell-command-to-string command)))

;;; kill-ring version of M-:
(defun eval-expression-to-kill-ring ()
    (call-interactively 'eval-expression)
    (kill-new (car values)))
  • My example for M-: was specifically made up so that the elisp command depends on the current buffer (the path of the file visited), and I might want to insert it to another buffer. My other example was like M-!, but in dired mode (!), where inserting the result into the buffer wouldn't make sense. Mar 20, 2011 at 17:47
  • 2
    I was a bit surprised to discover that there's no general interactive mechanism to save output in a register; it seems rather obvious.
    – geekosaur
    Mar 20, 2011 at 18:36

All messages echoed in the message area are saved in the *Messages* buffer, so just switch to it (C-h e, view-echo-area-messages) and select what you want.

If you want to get the value an expression that doesn't depend on the current buffer, you can also switch to the *scratch* buffer. Type your expression and press C-j (eval-print-last-sexp).

  • 1
    Thank you for your answer! Actually, there is an inconvenience with *Messages*: if I'm at the end of *Messages* (after C-h e) and want to select the last line, I try to do the obvious: set the mark, move one line up. But C-SPC (setting the mark) itself prints a message to *Messages*, so my new region gets "spoilt" with the new message. I need to do extra movements to avoid this bad effect. This comment is here just to name one disadvantage of this solution, in order to be able to compare it to other solutions, if they will be suggested; not that I discard this solution. Mar 20, 2011 at 17:16
  • 1
    @imz: Good points. I've added the key binding (it didn't exist when I learnt Emacs). I've set up Emacs so I rarely get a Mark set message, but geekosaur's solution is more convenient anyway. Mar 20, 2011 at 17:45

A slight improvement on geekosaur's solution. This does the right thing even if the expression evaluates to something other than a string.

(defun c5-eval-to-kill-ring ()
  (kill-new (with-output-to-string (princ (call-interactively 'eval-expression)))))

(global-set-key (kbd "C-;") 'c5-eval-to-kill-ring)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .