The echo area is not really a buffer. It is an area of a window. It is used to display messages and to display the minibuffer (which is a buffer).
You cannot "visit" the echo area and select text there, as you would a buffer.
You or your code needs instead to visit buffer *Messages*
, where messages are logged. (Not everything displayed in the echo area gets logged there, however.)
This is what C-h e
does, for reference:
(defun view-echo-area-messages ()
"View the log of recent echo-area messages: the `*Messages*' buffer.
The number of messages retained in that buffer
is specified by the variable `message-log-max'."
(interactive)
(with-current-buffer (messages-buffer)
(goto-char (point-max))
(display-buffer (current-buffer))))
From that you can see that you want to visit the buffer that is returned by evaluating (messages-buffer)
- this is generally buffer *Messages*
.
You want a command something like this (e.g., bind it to a key):
(defun visit-*Messages* ()
"Visit buffer `*Messages*."
(interactive)
(pop-to-buffer (messages-buffer))
(goto-char (point-max)))
That will put you in the buffer at its end. But you still need to select the text you want, use M-w
, and then use C-x 0
to hide the buffer.
If you know the beginning and end positions of the text you want to copy then you can do everything you want in the function. But those need to be positions in buffer *Messages*
. In that case, something like this:
(defun copy-from-*Messages* (start end &optional msgp)
"Copy text from BEG to END in buffer `*Messages* to kill ring."
(interactive (list (read-number "Start: ") (read-number "End: ") t))
(with-current-buffer (messages-buffer)
(copy-region-as-kill start end))
(when msgp (message "Copied: %S" (car kill-ring)))
(car kill-ring))
It's unlikely, I think, that you would want to invoke this interactively, as you won't know what the start and end positions are. But you might want to invoke it from Lisp code that knows the positions you're interested in.
C-u M-! uname -a RET
. This meaning of a prefix argument is common to many of the commands which normally only display a message.(read-minibuffer "Dummy: " (current-message))
would be very close to what I want. It doesn’t work for me right away, as(current-message)
seems to benil
. If you can post an answer with a working example like that, I would accept it.current-message
isnil
when the message disappears from the echo area. That’s why they created a function that reads from the*Messages*
buffer if needed.