3

According to the Emacs manual, M-: evaluates an Emacs Lisp expression and prints the value in the echo area. How can I direct the value to the current buffer instead?

Evan Aad
  • 1,461
  • 1
  • 14
  • 29
  • 1
    The contents of the echo area are generally in the `*Messages*` buffer. You can switch to that buffer using `(view-echo-area-messages)` which is bound to `C-h e` by default. From that buffer you can freely copy text. – glucas Apr 04 '17 at 14:47
  • I think the current answer is a better choice because it the question you've asked in the title and #1 & #2 in the text. My comment is really about #3, which seems like a separate question. – glucas Apr 04 '17 at 14:58
  • OK, then I think you need to split this up because you are asking three different things: How does one insert the `M-:` output in to the current buffer? Why does that not work for one specific case, `magit-version`? And totally unrelated, how does one get back to text that was shown in the echo area? – glucas Apr 04 '17 at 15:00
  • @glucas: The current answer doesn't answer questions #2 and #3. #3 was in the original post. I can't accept their answer. All three questions are answered by your comment, for all my purposes. – Evan Aad Apr 04 '17 at 15:00
  • To make this site generally useful, it helps to have focused questions -- that way other users with similar questions can find the answers they need. Right now the question you have asked is "How do I insert the output of `M-:` in to the current buffer", which has already been answered. If the question you really intended to ask was "How can I copy the message echoed by `magit-version`" then please change the question, because that answer would have nothing to do with `M-:` at all. – glucas Apr 04 '17 at 15:27
  • @glucas: I will revert the post to its original form and change the title correspondingly. Unfortunately, it still means I can't accept the current answer, since it doesn't address one of the two questions asked. – Evan Aad Apr 04 '17 at 15:32
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/56548/discussion-between-glucas-and-evan-aad). – glucas Apr 04 '17 at 15:35

1 Answers1

8

use C-u M-: insert the result into current buffer instead of printing it in the echo area.

(eval-expression EXP &optional INSERT-VALUE)

Evaluate EXP and print value in the echo area. When called interactively, read an Emacs Lisp expression and evaluate it. Value is also consed on to front of the variable ‘values’. If the resulting value is an integer, it will be printed in several additional formats (octal, hexadecimal, and character). Optional argument INSERT-VALUE non-nil (interactively, with prefix argument) means insert the result into the current buffer instead of printing it in the echo area.

南山竹
  • 206
  • 1
  • 5
  • Thanks for replying. I tried to follow your advice and evaluated `C-u M-x magit-version RET` while in the `*sctach*` buffer, but the output wrote to the echo area rather than `*scratch*`. – Evan Aad Apr 04 '17 at 09:04
  • ```C-u M-: (magit-version) ``` – 南山竹 Apr 04 '17 at 09:09
  • `C-u M-x (magit-version) RET` prints `[No match]` to the minibuffer. – Evan Aad Apr 04 '17 at 09:12
  • sorry about, there a typo: ```C-u M-: (magit-version) ``` – 南山竹 Apr 04 '17 at 09:14
  • `C-u M-: (magit-version) RET` works, but it doesn't print the same output as `M-x magit-version RET`, for instance the Git version is not printed. Is there a way to direct the complete output of the `M-x` version to the current buffer? – Evan Aad Apr 04 '17 at 09:18
  • ```M-x magit-version``` eval ```command``` version, ```M-: (magit-version)``` eval ```variable``` version – 南山竹 Apr 04 '17 at 09:42
  • Is there a way, then, to direct the output of the *command* `M-x magit-version RET` to the current buffer? – Evan Aad Apr 04 '17 at 09:45
  • 1
    The `magit-version` command returns as a result only the version number, so that is what you can insert in to the buffer. If you look at the source for `magit-version` you'll find that the additional information is displayed via the `(message ....)` function -- so if you want that text you would need to temporarily advise / override `message`. – glucas Apr 04 '17 at 14:45
  • 1
    What @glucas said. Or just pick up some of the code that prints that info in `*Messages*` (or similar) and use it to print the info wherever else you want. – Drew Apr 04 '17 at 14:47
  • do it yourself: ``` (defun self-magit-version () (require 'magit) (format "Magit %s, Git %s, Emacs %s, %s" (or magit-version "(unknown)") (or (magit-git-version t) "(unknown)") emacs-version system-type)) ``` – 南山竹 Apr 04 '17 at 15:31