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.

18
  • Possible duplicate of Pass the output of previous command to next as an argument
    – muru
    Jul 11 '19 at 2:55
  • @muru I read the question and answer you've referenced and don't understand how it helps me with my question. Jul 11 '19 at 3:00
  • @muru I looked at this and can't figure out how it solves my problem. Maybe you can help? Jul 11 '19 at 7: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 7:05
  • @muru echo foo | xargs -I {} emacs -nw {} -f org-mode returns an error for me. Does it work on your machine? Jul 11 '19 at 7:41
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

3
  • 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. Jul 11 '19 at 3:06
  • @BrianFitzpatrick I don't understand what you're saying there. You can use arbitrary commands in command substitution.
    – muru
    Jul 11 '19 at 7: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. Jul 11 '19 at 8: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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.