0

The command

emacs -nw foo -f org-mode

opens the file foo in emacs in org-mode.

Now, suppose that the filename foo is the output of a script. How could I pipe to my command?

So, I'm trying to get something like

echo foo | emacs -nw {GET OUTPUT HERE} -f org-mode

to work. Is this possible?

EDIT: This question has been voted to close, but the answer referenced doesn't work and wasn't tested by the user who voted to close.

  • @muru I read the question and answer you've referenced and don't understand how it helps me with my question. – Brian Fitzpatrick Jul 11 '19 at 03:00
  • @muru I looked at this and can't figure out how it solves my problem. Maybe you can help? – Brian Fitzpatrick Jul 11 '19 at 07:03
  • echo foo | xargs -I {} emacs -nw {} -f org-mode, or as alnx suggested, emacs -nw "$(echo foo)" -f org-mode. – muru Jul 11 '19 at 07:05
  • @muru echo foo | xargs -I {} emacs -nw {} -f org-mode returns an error for me. Does it work on your machine? – Brian Fitzpatrick Jul 11 '19 at 07:41
  • emacs: standard input is not a tty – Brian Fitzpatrick Jul 11 '19 at 08:02
  • @muru If you didn't test, then why did you say the command would work and vote to close? – Brian Fitzpatrick Jul 11 '19 at 08:02
  • because those are the standard ways to use some command's outputs as the argument of another command. If you have a specific error with some of them, then you should include that in your post, which you haven't done at all. By the way: the method worked fine - emacs was run with the input as argument. You problem is that emacs doesn't like working in a pipeline, which means the idea behind your entire question is broken. Use a better editor – muru Jul 11 '19 at 08:08
  • @muru You suggested echo foo | xargs -I {} emacs -nw {} -f org-mode and now you're saying that my question is broken. The existence of an answer and my question being "broken" are mutually exclusive. – Brian Fitzpatrick Jul 11 '19 at 08:13
  • @muru Why not just post an answer that explains the flaws in my question so we can all learn from the problem? Despite the comments, I'm still confused why my problem isn't solvable. – Brian Fitzpatrick Jul 11 '19 at 08:14
  • @muru Command substitution is not the same as piping, which is what my question is explicitly about. If "broken" is not mutually exclusive from "solvable", then I don't understand what the adjective "broken" means. – Brian Fitzpatrick Jul 11 '19 at 08:26
  • Ok, let me try again: The xargs method there works in this aspect: the output of the command is used as an argument and emacs is started with that argument. In as much as this is your question, the method works - you can use stdout in the middle of a command. Try, say, echo or cat or vim, and you'll see the effect. The error you're getting is this: After emacs has been started with that argument, it sees that the input is a pipe, and it breaks. So the problem in the question has been solved, and now you have a new problem, which breaks your overall idea. Is that clearer? – muru Jul 11 '19 at 08:31
  • @muru I don't understand why emacs behaves differently than the other commands. Why? This is totally not obvious. – Brian Fitzpatrick Jul 11 '19 at 08:33
  • Emacs is an OS that expects input to be connected to a terminal and doesn't work otherwise. Vim, for example, also expects the same thing, complains, but still works. – muru Jul 11 '19 at 08:35
  • @muru I didn't know Emacs was an OS. I thought emacs was a text editor. – Brian Fitzpatrick Jul 11 '19 at 08:36
  • FWIW echo foo | xargs -I{} emacs -t /dev/tty -nw {} -f org-mode appears to work (no idea if there are other implications though) – steeldriver Jul 11 '19 at 11:10
  • Note, many emacs users keep the editor running permanently and connect to it with emacsclient when you want to edit a new file. You can set EDITOR=emacsclient.... in various ways, see the link. – meuh Jul 11 '19 at 15:26

2 Answers2

2

How about simply using a subshell (as suggested in the comments):

emacs -nw $(<COMMAND GOES HERE>) -f org-mode

For example

emacs -nw $(echo foo) -f org-mode

Will open "foo" in org-mode

alnx
  • 116
  • This would require that I already know the command. It would be nice to be able to pipe in from an arbitrary command with the same line of code. – Brian Fitzpatrick Jul 11 '19 at 03:06
  • @BrianFitzpatrick I don't understand what you're saying there. You can use arbitrary commands in command substitution. – muru Jul 11 '19 at 07:08
  • For instance, say I want my environment variable to be $EDITOR=emacs -nw {my file} -f org-mode. I can't use $EDITOR=emacs -nw $(??) -f org-mode. – Brian Fitzpatrick Jul 11 '19 at 08:17
0

Use a read to get the filename from the pipe and then pass it as a variable to your command.

For example, using a shell function (probably not the best name for it, maybe you can think of something better):

emacspipe () {
    local filename
    read filename
    emacs -nw "$filename" -f org-mode
}

Then you can use:

$ echo foo | emacspipe

But using command substitution $(...) like the other answer suggests is definitely a more appropriate way to accomplish the same. If you have a command you can pipe to something, you can just as easily run it in a $(...) block and pass the resulting filename as an argument to the script or function starting emacs...

filbranden
  • 21,751
  • 4
  • 63
  • 86