Using Eshell To Output To A Buffer

As many of you know, your can output to a buffer directly from within Eshell by redirecting the output to a #<buffer ...> structure like this

Welcome to the Emacs shell

~/org/blog $ echo "Hello World!" > #<buffer hello-buffer>

That naming convention may seem weird but it’s exactly how Emacs refers to buffers. As Christian Tietze says in this post about redirecting output to a buffer you can see this by executing the current-buffer command (it’s not an interactive function so you have to call it as (current-buffer) in some Elisp context).

Tietze covers all this in his post but the interesting thing is that he hates having to remember and type the #<buffer ...> structure. I can sympathize. I got it wrong at least once while preparing this post. Tietze has the solution. He wrote a function, new-buffer, that generates a temporary buffer and returns it just as if you had specified it with the buffer notation. Then he can write Hello World! into a temporary buffer like this

Welcome to the Emacs shell

~/org/blog $ echo "Hello World!" > (new-buffer)

That’s a bit easier to remember and less clumsy to type than the #<buffer ...> construct.

Of course, as Tietze says, all this makes sense only if you’re already in Eshell. Otherwise, you can achieve the same result by simply using shell-command or async-shell-command. Regardless, Tietze’s post is interesting and worth a couple minutes of your time.

This entry was posted in General and tagged . Bookmark the permalink.