Saturday, August 10, 2013

Redirecting terminal output into any X application

Sometimes I want to redirect bash output directly into an X application for further editing. Normally this is done using a temporary file where the bash output is saved and then it is opened by an application. There's a way though to start an X application and paste the bash output into its window automatically.

To do this the xsel and xdotool programs should be installed:
sudo apt-get install xsel xdotool
 
Now the following alias should be defined:
alias 2edit='xsel -b;n=pipe$RANDOM;xdotool exec --terminator -- mousepad $n -- search --sync --onlyvisible --name $n key --window %1 ctrl+v'
 
So the bash output is inserted into the clipboard, then mousepad is started and the clipboard content is pasted. Instead of the mousepad any other editor can be used. I've successfully tested the Sublime Text Editor and it opens a new tab for each new paste.

Usage example:
dpkg -l | 2edit
 
It should be noted that a new window will be open only when the program that pipes into the 2edit has finished. This could be used as a notification to the user about finishing of a time consuming operation with its results pasted into the opened window.

No comments: