I'd like to keep the bash command prompt input at the top of the screen so that outputs from older commands are pushed downwards rather than up.
How can I achieve this?
I'd like to keep the bash command prompt input at the top of the screen so that outputs from older commands are pushed downwards rather than up.
How can I achieve this?
Add these lines to your .bashrc:
prompt_on_top() {
tput cup 0 0
tput el
tput el1
}
pre_cmd() {
if [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] || [ -n "$COMP_LINE" ]; then
return
fi
printf "\33[2J"
}
PROMPT_COMMAND="prompt_on_top"
trap 'pre_cmd' DEBUG
bash have PROMPT_COMMAND, which hold the command will be executed before bash show prompt. Here we set it to function prompt_on_top, which use tput to set the cursor at the top of screen.
bash also have a way to execute a command before executing any command, using trap to handle signal DEBUG. Here we set it to function pre_cmd, which will clear old screen, and do nothing if we did completion (COMP_LINE is not empty) or run command in BASH_PROMPT.
There's a limitation with this approach, if command output is too long too fit in a screen, then the output will be override by prompt_on_top action. In this case, you need to pipe the output to a pager to read the whole output.