When I start a new terminal, the prompt is on the first line. After working for a while (or running a command which produces some output), the prompt is on the last line. There it'll stay during the rest of the session unless I press Ctrl-l, run clear or reset or something similar.

To obtain some measure of continuity in the work, I use screen with a trick to display the session name in the caption (I use the last one with the two screen commands in .screenrc).

To avoid cognitive overhead by cluttering the work area, I'd like this caption to be displayed at the top of the terminal.


You're lucky, screen does honor save and resore commands. So with bash it can be done.

This gives instructions how to move the cursor around with special escape sequences. You can write "Hello World!" to the top left corner with this:

echo -e "\033[s\033[2;0HHello World!            \n                        \033[u"

Explained: The character \033[s saves the current cursor position, which is what we want b/c screen terminal sizes can vary, a lot. Then \033[2;0 moves the cursor to the third row to the first character (we begin counting in the top left corner and with 0). Then comes the text and a little illustration of what is possible. Finally the code \033[u puts the cursor back to where it was wenn \033[s was emmited.

This example moves the cursor to the second row because when issuing the command 2 newlines are printed so what is the current third row will be the first row.

You can use this together with the session caption you can already get by the explanation you already linked.

  • 2
    You can use tput to do this in a terminal-independent way, without needing to hardcode those escape codes. For example: echo -n "$(tput sc)$(tput cup 0 0)----Hello, world----$(tput rc)" – Jim Paris Aug 9 '12 at 16:16
  • I'm sure about every shell has a way of emitting these characters, but I only know bash :-) Thanks for the tput input, I didn't know about that. – Bananguin Aug 10 '12 at 6:22
  • @user1129682: I tried screen $SHELL -c 'echo -n "$(tput sc)$(tput cup 0 0)${STY}$(tput rc)"' and screen $SHELL -c 'screen -X caption always "$(tput sc)$(tput cup 0 0)${STY}$(tput rc)"' but neither worked. Any ideas? – l0b0 Aug 21 '12 at 12:42
  • Depends on what you want. If you just want the title off the current session in your screen caption then you would screen -X caption always; screen -X caption string "%t". If you want the current title in the upper left corner you would have to modify your PS1 environment variable to invlude the tput magic. – Bananguin Aug 22 '12 at 7:10
  • @l0b0: and on reading this again, tput cup 0 0 could well have the double newline problem, which is why i put my text at the third line, not the first. have you tried tput cup 2 0 or 0 2 ... still havent read up on tput ... – Bananguin Aug 31 '14 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.