Copy string to clipboard and display into output window simultaeneously
To copy the output to the clipboard also outputting to the current terminal you could simply use tee
, which will output its stdin
to any file passed to it as an argument and to stdout
; you can use a process substitution running xclip
to "fake" a regular file and output to it instead of to a regular file:
echo -n $GEDIT_CURRENT_DOCUMENT_URI | tee >(xclip -selection clipboard)
Another option is to output to one of the free "default" pseudo-terminals (tty1 to tty6), which have a correspodent device file in "/dev" ("/dev/tty1" to "/dev/tty6"):
echo -n $GEDIT_CURRENT_DOCUMENT_URI | tee >(xclip -selection clipboard) >/dev/tty1
Yet another option is to output to another "listening" pseudo-terminal using a named pipe; this requires a setup to set the "listening" terminal:
First open the "listening" terminal and run this script (for the sake of the example I'll assume that the script is running in ~/tmp
):
#!/bin/bash
mkfifo fifo # creates a named pipe named "fifo" in the current working directory
trap 'rm fifo; exit 0' 1 2 3 13 15 # traps SIGHUP, SIGINT, SIGQUIT, SIGPIPE and SIGTERM; removes "fifo" and exits upon the reception of each of them
while [ 1 ]; do
cat fifo # outputs the content of "fifo"
done
This will create a named pipe named "fifo" in the current working directory and will continuously output its content until the script execution is halted;
Then using Gedit's External Tools run this modified version of the second command, which instead of redirecting the output to "/dev/tty1" redirects it to the named pipe:
echo -n $GEDIT_CURRENT_DOCUMENT_URI | tee >(xclip -selection clipboard) >>~/tmp/fifo
Sample output using two gnome-terminal
instances:
Running the script on the right terminal
Running
echo -n $GEDIT_CURRENT_DOCUMENT_URI | tee >(xclip -selection clipboard) >>~/tmp/fifo
on the left terminal
Hitting CTRL+SHIFT+V
More informations on named pipes