How to keep the command line prompt on the first line of terminal

bashcmd.execommand lineterminal

I need to keep the command line on the top of the screen and see the output of any command below it. How can I achieve this using any available tools on any platform?

Currently, on the all terminal emulators I have worked with if you run ls foo -l the result will be:

$ ls foo -l
hi.txt
bye.txt
$ command prompt is here

In like to see this:

$ command prompt is here
$ ls foo -l
hi.txt
bye.txt

Best Answer

Linux, Bash.

Bash uses stderr (file descriptor 2) to print its command prompt and command line. Use tmux to display two shells one above the other (or just place two GUI windows, terminal emulators one above the other). In the lower one invoke tty. Don't use the lower shell directly from now on. In the upper one redirect file descriptor 1 to the tty of the lower one (e.g. exec 1>/dev/pts/2).

Ctrl+L clears the upper, clear clears the lower. Each portion is multi-line. Thanks to tmux's features you can resize them (i.e. move the border up and down).

Use this to make commands appear also in the lower portion of the display:

trap 'printf "%s\n" "-----$ $BASH_COMMAND"' DEBUG

I tested the solution and at some point my terminal window looked like this:

kamil@foo:~$ ls -l /proc/$$/fd
kamil@foo:~$ uname -r
kamil@foo:~$ cat /etc/issue
kamil@foo:~$ █

──────────────────────────────────────────────────────────────────────
-----$ ls --color=auto -l /proc/$$/fd
total 0
lrwx------ 1 kamil kamil 64 Sep  9 20:42 0 -> /dev/pts/3
l-wx------ 1 kamil kamil 64 Sep  9 20:42 1 -> /dev/pts/2
lrwx------ 1 kamil kamil 64 Sep  9 20:42 2 -> /dev/pts/3
lrwx------ 1 kamil kamil 64 Sep  9 21:13 255 -> /dev/pts/3
-----$ uname -r
4.15.0-33-generic
-----$ cat /etc/issue
Ubuntu 18.04.1 LTS \n \l

(Note: --color=auto appeared because my ls is an alias).

Expect interactive tools (like text editors) to misbehave, so it's better to revert the change while calling them. Example:

1>&2 nano

Some shells (e.g. zsh) use a separate file descriptor 10 for command line. This allows you to redirect stderr to the lower (or yet another, 3rd) tty, keeping the command line in the upper one.

Related Question