The best solution I could come up with for what you think you want, is to add this to your .bashrc
:
lol()
{
if [ -t 1 ]; then
"$@" | lolcat
else
"$@"
fi
}
bind 'RETURN: "\e[1~lol \e[4~\n"'
The lol()
function takes any command, executes it, and pipes its stdout to lolcat if stdout is a terminal.
You can run lol ls
and it will pipe ls
to lolcat
.
The bind
command tells readline, the library bash uses to read input, to behave differently when you press Enter in bash.
The normal behavior is to just insert \n
.
This command (shamelessly stolen from this answer) will cause Enter to go to the beginning of the line, enter the text lol
then go back to the end of the line, and only then insert \n
.
If you use this, you'll see in the terminal how your commands are being prefixed with lol
.
You will quickly discover that this is quite annoying. Because you redirect stuff, then things stop working.
For example, with this, man ls
doesn't open in a pager, because it man
detects that stdout is not a terminal but rather a pipe. And don't even try to use vim.
Another issue is that you'll type ls
, and it will turn into lol ls
, and then when you press the Up key to have the last command be executed, and press Enter, it will turn into lol lol ls
.
The gist is, you want to be specific about the commands that you redirect, and be able to run the original commands, by having this in your .bashrc
:
lol()
{
if [ -t 1 ]; then
"$@" | lolcat
else
"$@"
fi
}
COMMANDS=(
ls
cat
)
for COMMAND in "${COMMANDS[@]}"; do
alias "${COMMAND}=lol ${COMMAND}"
alias ".${COMMAND}=$(which ${COMMAND})"
done
The lol()
function is the same as before, except now the COMMANDS
array contains all the commands (argument-agnostic) that you may redirect.
For example, ls /usr
will actually run lol ls /usr
, but running .ls /usr
will run /bin/ls /usr
.
$HOME/lolcat
), and in it create a file containingexport PATH=$(echo "$PATH" | sed "s%$HOME/lolcat%%g")
——$(basename $0) "$@" | lolcat
. Theexport
removes the 'lolcat' directory from PATH; the second runs the command avoiding the version in the 'lolcat' directory. Then add a symlink for every command you want mapped ('aliased') in the directory that points to the wrapper. FInally, add$HOME/lolcat
to the front of your PATH so that those names are picked up first. Care needed!ls | lolcat
probably doesn't produce the same output asls
does (give or take colouring). You won't want to usevim
piped tololcat
either.