I'm trying to set a little pomodoro/countdown timer for myself in the terminal.

This comment on Superuser offers a handy little shell function that will act as a countdown timer, which printfs the remaining time to STDOUT (awesome). I was thinking it would be even better if I could see the live countdown in the title of that terminal emulator tab, so I could monitor my remaining time without having to switch back and forth between tabs.

It appears the PROMPT_COMMAND environment variable can be used to update the window title, but since its contents are executed only just before Bash displays a prompt, there's no way it can live update the title while the process is running (that I know of).

Is there any way this can be done?

(It has occurred to me that I could solve this more easily by splitting a new pane in tmux, but I'm on a small screen, and I'd appreciate being able to save all the screen real estate I can.)

  • Just output the ANSI codes to stdout or stderr. Some terminals might also provide API to change their titles programmatically. – choroba Jun 22 '16 at 9:07

Terminal codes might be what you need, see http://wiki.bash-hackers.org/scripting/terminalcodes and https://en.wikipedia.org/wiki/ANSI_escape_code for details (or http://invisible-island.net/xterm/ctlseqs/ctlseqs.html for quite a technical description of the the codes relating to xterm).

Here an example of how to set the title:

set_title() {
  printf '\033]0;%s\007' "$*"
  • Holy crap you're a genius. Shame I can't upvote yet. – Ryan Lue Jun 22 '16 at 11:29

Just output the relevant ANSI codes to stdout or stderr.

The following works for me in an xterm:

for i in {10..1} ; do printf '\e]0;'$i'\a' ; sleep 1 ; done &

Some terminals might also provide API to change their titles programmatically.

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.