How can I insert directly the text displayed in the echo area? (also wrongly called the minibuffer).

When I for example run (+ 2 2) in a buffer and execute it with C-x C-e it correctly displays the result '2' in the echo area. There is also a '2' in the Messages buffer. I'd like this '2' inserted below the line executed, in the same buffer, or something like that.

Is there a command to, for example, pipe the echo area into the buffer or copy the last line of the Messages buffer into the present working buffer?

  • 1
    C-u C-x C-e will insert the result into the buffer. Not what you ask, but what you'd like in 2nd paragraph. – Juancho Feb 2 '14 at 17:52
  • @Juancho: yes, that is what I'd like. – Quora Feans Feb 2 '14 at 17:59

In lisp-interaction-mode which is the default mode for the *scratch* buffer you can use eval-print-last-sexp bound to C-j.


In any buffer you can switch to lisp-interaction-mode with M-x: lisp-interaction-mode [RET]

Now C-j should be bound by default to eval-print-last-sexp unless the binding is shadowed by another mode. You can check the quick description of the current major-mode with C-h m.

If it is shadowed or simply not bound you can eval

(global-set-key (kbd "C-c p") 'eval-print-last-sexp)

with C-x C-e. That will bind the command to C-c p.

To make the change permanent write it in your .emacs file.

  • Isn't the default mode for *scratch* Fundamental? It is on my system. Or is that just a name for lisp-interaction-mode? Your eval-print-last-sexp works fine in Fundamental but C-j us not bound. – terdon Feb 2 '14 at 17:38

You need to use one of the EMACS insert commands. To accomplish what you illustrated in your simple example you would need to do the following.

(insert-string (number-to-string (+ 2 2)))

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.