Is there a function I could use to quickly copy the message in the echo area (if any) into my working buffer? Should I define the shortcut for that function with define-key
or global-set-key
or some other way?
Copy the last Emacs message into the current buffer
emacs
Related Question
- How to get current buffer’s filename in emacs
- How to send the content of an emacs buffer to gcc without writing to disk
- emacs – Removing the Last n Characters from Every Line in an Emacs Buffer
- Add and Remove Text from Same Place on Every Line in Emacs
- Window display table and buffer display table conflict in Emacs
- In emacs, how to force switch to a different buffer in a dedicated window
Best Answer
The function
current-message
returns the message that is currently displayed in the echo area, if any. You can insert it with(insert (current-message))
. However, anything that causes something else to occupy the echo area will causecurrent-message
to returnnil
.Messages from Emacs are archived in the
*Messages*
buffer. It would be more useful to grab the last line from there. This isn't perfectly reliable since it's possible to have a multi-line message, but that's rare: themessage
function is meant for short messages that fit in one line.The following function inserts the last message (more precisely, the last line from the
*Messages*
buffer. With a prefix argument, it returns older messages: 1 for the latest message, 2 for the next-to-last one, etc. The argument 0 inserts(current-message)
if any.Bind it to a key in the normal way. For example, if you want the command available on
C-c m
at all times:There may be easier ways than invoking two custom commands to insert the file name, see the Emacs wiki.
To insert the output from evaluating a Lisp snippet with
C-x C-e
, pass a prefix argument:C-u C-x C-e
.