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 have a script that outputs a string that I would like to edit and then run as a command.

For example, I am using

cat ... | xsel -p

And then I want to edit the output of `xsel -op´ and run the edited string as a new command.

So I am trying:

cat ... | xsel -p
$(xsel -op) TAB

But this of course fails.

So how can I output the contents of xsel -op (in this case) to my command line, edit that new command, and then run the command?

Thanks

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

From zshbuiltins:

-z Push the arguments onto the editing buffer stack, separated by spaces.

To output content of xsel to your command line:

print -z $(xsel -op)
share|improve this answer
    
Ok, better than print -s, works on zsh –  simao Oct 16 '12 at 14:29
add comment

You can get it added to history (at least in bash, I'm not sure on zsh) by doing the following

history -s $(xsel -op)

Then retrieve and edit it.

I've since found that

print -s $(xsel -op)

works equivalently under zsh

share|improve this answer
    
this looks like what I want, but doesnt work in zsh –  simao Oct 10 '12 at 10:16
    
I found you can do the same under zsh with print -s $(xsel -op) –  Julian Oct 10 '12 at 10:25
    
yep it works. Thanks! –  simao Oct 10 '12 at 10:41
1  
you can even run fc immediately after history -s $(xsel -op) or print -s $(xsel -op) if you want to edit the command in $EDITOR (vi, nano, whatever). –  cas Oct 11 '12 at 5:46
add comment

Rather than adding it to history manually, you can just press bash's shell-expand-line key (M-C-e):

   shell-expand-line (M-C-e)
          Expand the line as the shell does.  This performs alias and his‐
          tory expansion as well as all of the shell word expansions.  See
          HISTORY EXPANSION below for a description of history expansion.
share|improve this answer
add comment

I am not entirely sure what you are trying to do but something like this should work:

`xsel -op | sed 'do something'`

The backticks will cause BASH to execute, in this case, whatever is saved in X's primary selection buffer which you have just edited using sed. For example:

echo "find . -name foo" | xsel -p
`xsel -op | sed 's/foo/bar/`

The above commands will cause find to be run searching for files or folders named "bar".

share|improve this answer
    
Not really what I would like, I want to edit it in the terminal, not with sed –  simao Oct 10 '12 at 10:17
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.