Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was just curious if there's a way to get input not from StdIn but from $EDITOR - be it vim, nano, emacs or even a non-command-line-editor (MacVim/TextMate). What are the options/workarounds/bestpractices?

Basically I'd wish for a workflow as with heredoc - but from the $editor instead.

$ sort -nr << FOO | uniq -c 
pipe heredoc> foo 
pipe heredoc> bar
pipe heredoc> baz
pipe heredoc> foo
pipe heredoc> FOO
   2 foo
   1 baz
   1 bar
share|improve this question
2  
I'm not sure what you mean by "get input from an editor," could you clarify just how you think the process should go? –  Kevin Mar 22 '12 at 14:07
    
@Kevin I hope the heredoc example makes it clearer. –  noxoc Mar 22 '12 at 16:27
    
@NilsRiedemann: So in this example in place of heredoc there should be opened text editor that allows you to input text, and after you quit editor all that you write in it is treated as input to sort and uniq? –  pbm Mar 22 '12 at 16:32
    
How about just using a temporary file? –  Kevin Mar 22 '12 at 16:43
    
@pbm yup, exactly. –  noxoc Mar 22 '12 at 18:22
add comment

4 Answers

up vote 6 down vote accepted

The command vipe in the package moreutils allows you to launch $EDITOR in the middle of a pipeline.
You can get the desired behaviour like so:

$ </dev/null vipe |sort -nr | uniq -c 
share|improve this answer
add comment

I guess your only chance to make it work with any kind of editor is to use temporary files:

FILE=$(mktemp); $EDITOR "$FILE"; <"$FILE" …command… ; rm "$FILE"

(However this does not allow to start the command before a complete input is provided.)

Also you can use echo "# please insert your input bellow" >"$FILE" before the call to $EDITOR to insert a comment that will show up inside the editor. Be sure that it won't affect the behavior of the command (or ask the user to explicitly remove it).

share|improve this answer
1  
Also, notice that rlwrap command allows line editing (with history) from the terminal and might be enough for your needs. –  Stéphane Gimenez Mar 22 '12 at 15:11
1  
It is also common to pre populate the file with some comment lines giving instructions. –  psusi Mar 22 '12 at 15:22
    
Can you expand the example to have a prefilled tempfile like psusi describes? Besides your example does not work - it lacks an argument. Seems like this answer is the way to go. Also: why is it <"$FILE" command; not command <"$FILE";? –  noxoc Mar 22 '12 at 18:20
    
@Nils: See the edit. command was meant to be replaced by the actual command :-) And for the last question: it's the same. –  Stéphane Gimenez Mar 22 '12 at 18:31
    
When I use FILE=$(mktemp); $EDITOR "$FILE"; <"$FILE" sort -c; rm "$FILE" I get usage: mktemp [-d] [-q] […shortened] and zsh: no such file or directory:\n rm: : No such file or directory :/ Vim also opens, but i can't save the file (no filename) –  noxoc Mar 22 '12 at 18:37
show 1 more comment

Emacs has the possibility to run the shell in one of its buffer (use M-X shell). But the launched processes would still get their input from stdin, their stdin would simply be a pseudo-terminal whose other end would be emacs.

share|improve this answer
add comment

In Emacs you can run any command and provide content of one of Emacs buffers (selected region to be exact) as stdin to application.

For example if content of your Emacs buffer will be ls and you mark it as region (using C-SPC), then run shell-command-on-region (M-|) and type as command bash (full combination: M-| command RET) there will be ls executed in bash and output will be opened as new buffer.

If you use C-u M-| command RET on region, output will replace marked command in your current buffer.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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