I often lose track of the command flow due to the long text output of some commands, so I would like to have a main terminal window where user commands are executed and other terminal windows that „pop up“ displaying the commands' output and then are closed with the use of a short-cut.
Is it possible to code such a behavior? Any tips about the language I should use?
An even more user-friendly interaction would be for the terminal to ask whether it should display the output externally if it exceeds a user-set limit.
Best Answer
I know little about programming languages, but Bash can definitely set up to behave like you want it, so here's how I would do it.
General Setup
Define a function in your
~/.bash_aliases
or~/.bashrc
file that allows you to start your command in a new terminal, e.g.This way
foo echo bar
opens up a newxterm
which shows just the output ofecho bar
, here this would be a window with just the wordbar
in it. A chain of commands must be quoted, e.g.foo "echo bar && echo baz"
. You can substitutexterm
by the terminal emulator of your choice, but you need to figure out what the stay open option is – read theman
page. Option-e
should work for all of them. Note that as a function name you can choose even a single symbol like:
,_
or·
(AltR+,).Explanations
foo () { … ;}
– define a functionfoo
which does…
( … )
– start…
in a subshell, this releases the original terminal so that a new command can be executed even when other output terminals are still openxterm -hold -e …
– runxterm
, run…
and-hold
it open after the command finishedbash -c " … "
– execute the command(s)…
, needed for being able to run multiple commands$*
– matches every argument offoo
= the full chain of commands afterfoo
Fine-tuning
Closing the output terminal window
You can close the output terminal using your desktop environment's standard shortcut for that – Alt+F4 by default.
If you want the output terminal to stay open for let's say five seconds after the command finished, you need to define the function like so:
Replace
&&
by&
for the terminal to stay open only until the command is finished, but at least five seconds.Input facilitation
Using
xdotool
(ugly and dirty, boo)It'll bother you to type
foo
before every single command in your main terminal, so I set up a dirty workaround for that – installxdotool
first if necessary. When you open your main terminal, first set this variable:This will automatically type
foo
followed by a space after, but unfortunately also before every new prompt. All left for you to do is to type your (chain of) command(s).Using a
perl
function (massively cool)The question how to perform the following was asked on Unix.SE and got a brilliant answer by Stéphane Chazelas, whose solution is used here. If this helps you in any way please visit the linked question and upvote his answer.
Add the following lines to your
~/.bashrc
file and save it:Now when you open a new terminal and run
auto_enter foo
, the string “foo ” (with the space automatically added) will appear after every new prompt. If you type something while a command is executed, “foo ” is going to be inserted even before this text.User-friendly interaction
This version of the function saves the output in a temp file, counts its lines and asks the user whether to display it in a new window:
Of course this can be highly tweaked and customized, but I think it's a good start.
And Now for Something Completely Different™
A totally different way of achieving your goal is using shell output redirection.
who
gives you a list of running terminals, e.g.:You can send the output to any of them by simply appending e.g.
> /dev/pts/1
to your command, which of course can also be used in a function as shown above.GNOME Terminal emulator (
gnome-terminal
) sessions are not displayed bywho
, nonetheless thepts
devices exist equally for them. You can determine open terminal sessions just by trying or withls /dev/pts | sed '/^[0p]/d'
instead.