Name your terminal tabs: a tiny title function for zsh and bash
On this page
On a normal day I have five or more tabs open in Windows Terminal: this site's dev server, a toastback build, a test run, podman, and one that tails the production logs. Every one of them was called "FedoraLinux-44", cut off to "FedoraLinux-4" because the tabs are narrow. Finding the test run meant clicking through tabs, and the production tab looked exactly like the others.

Now I type title 🧪 tests in a tab, and that's its name:

The emoji is the part I didn't expect to like this much. It shows in colour in the tab, and I find a red dot or a test tube faster than any word. On Windows, Win + . opens the emoji picker.
The zsh version
# title [text...]: set the terminal tab title; no args clears it
title() {
# Strip control characters so the text can't end the escape sequence early
local t=${*//[[:cntrl:]]/}
printf '\033]0;%s\007' "$t"
}ESC ] 0 ; text BEL is the xterm escape sequence that sets the window and tab title. Windows Terminal understands it, and so do most other terminals. Removing the control characters matters more than it looks: without it, a stray BEL or ESC in the text would end the sequence early and the rest would reach the terminal as a new escape sequence.
title with no arguments sends an empty title, and Windows Terminal goes back to the profile's name. Plain words need no quotes: title 🔴 prod logs works as it is.
If you use Oh My Zsh, you don't need this function: it has a title command built in. Put the name in quotes there (title "🧪 tests"), because it takes a second argument as a separate window title. Its automatic titles overwrite yours at every prompt, so put DISABLE_AUTO_TITLE="true" in ~/.zshrc before the line that loads Oh My Zsh.
The bash version
The same function works in bash, but on most distros the title is gone after one prompt. Fedora's /etc/bashrc writes user@host:dir as the title from PROMPT_COMMAND at every prompt, and Ubuntu's default ~/.bashrc does the same from PS1. So the bash version also sends your title again at the end of each prompt, after the distro's:
# title [text...]: set the terminal tab title; no args goes back to the default one
title() {
local t="$*"
TAB_TITLE=${t//[[:cntrl:]]/}
printf '\033]0;%s\007' "$TAB_TITLE"
}
# The distro's prompt sets the title too: send ours again at the end of the prompt
PS1+='\[${TAB_TITLE:+\e]0;${TAB_TITLE}\a}\]'Put it at the end of ~/.bashrc, after the lines that set PS1. While TAB_TITLE is empty, the prompt adds nothing, so title without arguments hands the tab back to the distro's usual title. The \[ and \] tell bash that the sequence takes up no space on the line, so editing long commands still works.

A few habits
- Name tabs by what they're for, not by the folder. Two tabs in the same project, one running the app and one the tests, are the reason this function exists.
- Mark the dangerous ones. I start every tab that can touch production with 🔴. It's hard to miss before you paste a command.
- Use it in aliases and scripts, so the tab names itself:
alias logs='title 📜 logs; journalctl -f'. - Programs can set the title too. Neovim with
set title, a shell on a server you ssh into, and some CLIs replace yours while they run, and some leave theirs behind. In bash the next prompt brings yours back; in zsh, runtitleagain. - Nothing happens? Check the profile in Windows Terminal: with Suppress title changes on (
suppressApplicationTitle), it ignores titles that programs send. - Renaming by hand works too: right-click a tab, Rename tab. That name wins over
titleuntil you clear it. The function is quicker when your hands are on the keyboard, and scripts can call it.
That's all it is: one escape sequence, and the tabs finally tell me what's in them.
How do you mark your tabs? If you have an emoji system of your own, or another small shell trick like this one, leave a comment below.
Read it online: https://gergodrazsdik.com/blog/name-your-terminal-tabs
Comments
No comments yet.
Leave a comment