how to copy the results from a grep command to the bash clipboard?

1

1

If I type something in a Linux bash terminal with no X, and then use Ctrl+u, whatever I typed is stored in the bash "clipboard" (for lack of a better term), and I can type it again doing Ctrl+y.

How can I copy the results from a grep command on a text file to such bash clipboard? For example, if I have an INSTALL file like this:

./installprocedure --do-some-long-and-complicated-operation-on-dir dir1

How can I copy the content of a grep command so that it's available doing Ctrl+y? For example:

copy content to bash clipboard "grep installprocedure INSTALL"  
Ctrl+y  
./installprocedure --do-some-long-and-complicated-operation-on-dir dir1 #cursor available here

719016

Posted 2011-06-30T14:24:03.787

Reputation: 2 899

http://stackoverflow.com/questions/749544/pipe-to-from-clipboard may be related to this. – N.N. – 2011-06-30T14:30:19.033

1On Mac OS X, you can pipe into pbcopy when you're in a GUI session (so not via SSH) and it puts it into the system-wide clipboard. pbpaste to retrieve. – Daniel Beck – 2011-06-30T14:47:32.123

added comment this is on linux bash terminal with no X – 719016 – 2011-06-30T14:48:41.573

It's actually "the kill ring in GNU Readline" if you want the correct name. – JdeBP – 2011-07-01T12:13:46.513

Answers

2

As far as I know there is no direct way to feed the internal bash buffer, but if your aim is to have the string on the command line to be able to edit it, that can be done utilizing two bash features:

  1. Parameter substitution (as mentioned by RedGrittyBrick) and

  2. the readline function shell-expand-line:

    shell-expand-line (M-C-e)

    Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions.

Example:

$ $(grep installprocedure INSTALL)Meta+Ctrl+e

This will replace the command line with the result of the command substitution.

Expanding on that, you can create a readline macro to do that automatically on any arbitrary command, without the need to enclose in backticks or $():

$ bind '"\C-xs": "\C-a$(\C-e)\e\C-e"'

Pressing Ctrl+X followed by s will then move the cursor to the beginning of the line (\C-a), literally insert $(, move to the end of the line (\C-e), literally insert ), and finally call shell-expand-line (\e\C-e = Meta+Ctrl+e).

In other words, it will replace the command line with its output.

peth

Posted 2011-06-30T14:24:03.787

Reputation: 7 990

1

Are you sure you don't want command history editing? Or maybe command substitution?

Command history

In bash, press the up-arrow key to recall the previous command. You can edit the line in the usual way.

Alternatively you can refer to previous commands, and to parts of previous commands using the exclamation-mark notation.

Using the output of one command as parameters for another command

 ./bar barparam `grep foo foofile`

Using the output of one command as input for another command

  grep foo foofile | ./bar barparam

Using a shell variable as a named clipboard

  clipboard = `grep foo foofile`
  ./bar barparam $clipboard

RedGrittyBrick

Posted 2011-06-30T14:24:03.787

Reputation: 70 632