14

For example, I want to be able to type something like:

$ git diff | tempbuffer

and have the diff opened in a new, unsaved buffer.

1
  • 4
    If I may suggest, try magit. it may not do exactly what you want. but I see it fits your need github.com/magit/magit Commented Dec 21, 2011 at 14:25

4 Answers 4

11

You can just use M-! -- it will run the command within the same cwd as your shell buffer, and output the results to a *Shell Command Output* buffer.

Note that if the results are brief, that buffer will not be raised and the output will be copied to the echo area; however the buffer is still used and available. C-hf shell-command RET has details of what constitutes "brief" output:

If the output is short enough to display in the echo area (determined by the variable max-mini-window-height if resize-mini-windows is non-nil), it is shown there. Otherwise, the buffer containing the output is displayed.

Sign up to request clarification or add additional context in comments.

5 Comments

You can also use C-u M-! to put the output in the current buffer.
@Daimrod: That's exactly what the OP does not want to do. She's inside an eshell and wants the output of a command to go to a new buffer.
This is brilliant - thank you. The only piece missing is putting the new buffer in the right mode. For example, if I send the results of git diff to a new buffer, I'd like the buffer to be in diff mode.
I discovered that you can just set the mode manually. For example, if I have just done M-! and then git diff, I can set the diff mode of the buffer with M-x diff-mode.
@MaryRoseCook: you may not need this anymore, but have you tried to prepend to the diff an emacs modeline to specify the mode? It might work, as it would be parsed when loading the buffer.
11

If you use eshell you can redirect output to a buffer, e.g.

 print foo > #<buffer bar>

which creates a new buffer bar with the content 'foo'. For further details, see the Emacswiki at http://www.emacswiki.org/emacs/EshellRedirection.

6 Comments

Thank you. That does work for eshell. However, I am using the normal shell (i.e. M-x shell).
If eshell is not an option for you, you can pipe your output to emacsclient. You'll need a script or shell function for this. As a starting point see emacswiki.org/emacs/EmacsClient#toc43.
This is really useful when using TRAMP, eg. "foo /ssh:user@machine:/some/remote/file > #<buffer bar>"
@u-punkt, is there a way to customize this redirection? I wanted to make it a bit simpler, something like print foo > #bar.
yeah I'd also like a less clunky solution that doesn't require me to type all the #<> characters. It feel it should be doable to simplify it but I'm still a beginner so I don't know yet how.
|
4

Unfortunately emacsclient doesn't read its standard input, so you need some kind of wrapper. Here's a Bash shell function that works for me:

tempbuffer() {
  perl -MFile::Temp -MFile::Copy -e \
  'copy *STDIN, $file = File::Temp->new; system "emacsclient", $file';
}

2 Comments

Thank you for your comment. Sorry, I am being a bit stupid. I have put this function in my bash file, and can invoke it from the console. However, I don't understand how to pass in the command I actually want to run (i.e. git diff).
Just as in your original post, ie. git diff | tempbuffer.
1

My personal preference is for something you can type in Bash without having to manage any files:

git diff | (f=$(mktemp); cat > $f; emacsclient $f; rm -v $f)

emacsclient waits for you to be finished with the buffer before Bash deletes the temporary file.

I would use M-! (phils's answer) if I was starting the shell command from scratch and the above (which is similar to Sean's answer) if I was 'in the middle of something' in the shell and then decided 'I want to pipe this to Emacs'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.