Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display current jobs in Terminal titles UNIX
This is a follow-up to cft's earlier tip which showed how to put the current working directory in a terminal window's title. Another nice possibility is to display instead the current job. For instance, right now I have six terminal windows with these titles (quite a bit more informative than ttyp1 thru ttyp6...):
  • cal 2002
  • man tcsh | grep alias
  • ssh me@somewhere.edu
  • telnet 192.168.1.99
  • top
  • vi ~/Library/init/tcsh/aliases.mine
The titles appear alphabetically in Terminal.app's "Window" menu, and are also attached to minimized windows in the dock.

The way to do this is explained in Ric Lister's Xterm-Title Howto and is easy to summarize. First, issue the last command above ("vi ~/Lib...") to create and edit a file named 'aliases.mine' in the appropriate directory. This standard location is defined in /usr/share/init/tcsh/README. Next, type "i" and insert this line:
sched +0:00 alias postcmd 'echo -n "^[]0;\!#^G"'
As in the earlier tip, ^[ and ^G here are not to be typed literally. They are the ESC and BEL escape sequences, obtained in vi by typing ctrl-V ctrl-[ for the first one and ctrl-V ctrl-G for the second. Finally, save and close the file (esc :wq! return), open a new terminal window, and marvel.

Addendum to original story: Based on the long series of comments below, here is a version of the command to use which will not cause errors and seems to work in all situations:
sched +0:00 alias postcmd 'echo -n "^[]0; `hostname|cut -d.[space]
-f1`: `echo \\!# | cut -b 1-25`^G"'
Please note it is shown on two lines, but is really one long line. Replace [space] with an actual space character and enter as one line of text.

Second addendum: From the original author, here's a newer version that actually works in all situations (even the above one causes some errors in certain circumstances):

sched +0:00 alias postcmd 'printf "\033]0; %s \007" "\!#:q"'
I have tested this new version, and it works just fine in tcsh...
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[10,256 views]  

Display current jobs in Terminal titles | 30 comments | Create New Account
Click here to return to the 'Display current jobs in Terminal titles' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How do I get rid of
Authored by: sjonke on Feb 07, '02 10:48:57AM

I like this, but the titles aren't limited to the command being used, rather it also shows the following after the command: "- /usr/bin/login (ttyp2)". Is there any way to remove that so that it just shows the command being run?



[ Reply to This | # ]
How do I get rid of
Authored by: robh on Feb 07, '02 12:34:07PM

Use the terminal Preferences -> Window and adjust the "Title Displays".

Then open a new terminal.



[ Reply to This | # ]
hum, it works so so
Authored by: z0mbi3 on Feb 07, '02 01:18:39PM

well, I really enjoy this tip, but I think that something is happening that shouldn't be.
When I make a 'ls' there is some strange text before the normal output:
[tomb:~] mim% ls
[0mDesktop/ Downloads/ Icon? Movies/ Pictures/ Sites/ gmon.out
Documents/ GNUstep/ Library/ Music/ Public/ Storage/

that [0m shouldn't appear....anyone got this ?



[ Reply to This | # ]
hum, it works so so
Authored by: geohar on Feb 07, '02 01:33:56PM
Me too, the tip is cool, but ls, ll etc. all do daft things to the title... I know why, but don't know a fix. Basically ls and ll etc are all defined as aliases -- type
which ls
to see what I mean. Somehow, aliassed commands are being executed rather than used as strings. Try
unalias ls
and the effect for ls goes away. It stays for ll, though. I think an extra escape or single-quote is needed somewhere :-)

[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: geohar on Feb 07, '02 04:07:21PM

OK, so I have now tracked down the bug further...

* the described command fails to work when ls is typed...
* I thought this was because ls is an alias (by default to /bin/ls -F or some such).
* Doing unalias ls prevents it spewing the listing into the titlebar.
* However, if one types alias lister "/bin/ls -F", then it all works nicely when you type lister...
* upon closer examination of the default ls alias, ls is defined as ls -F
* trying alias ls "/bin/ls -F" solves the problem, and the title reads /bin/ls -F

So, what is going on here?

as a quick check, making a script that does echo "foo", and calling it test, then making an alias like so :

alias test test,

where we are in the directory of the executable script test.... puts foo in the title bar.

There's the bug,

can anyone throw light on it?



[ Reply to This | # ]
Problems with ls and ll FIX
Authored by: geohar on Feb 07, '02 04:25:20PM

make sure ls is defined as

alias ls "/bin/ls -F"

NOT

alias ls "ls -F"

in either ~/.tcshrc or ~/.login, whichever it's defined in on your install



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: hysterion on Feb 07, '02 06:24:46PM

> as a quick check, making a script that does echo "foo",
> and calling it test, then making an alias like so :
>
> alias test test,
>
> where we are in the directory of the executable script
> test.... puts foo in the title bar.
>
> There's the bug,
>
> can anyone throw light on it?


I don't get what you're doing :-) In fact, I doubt the problem has to do with aliasing. What unexpected things are you getting? If I write a script like yours, alias it, and execute it:

[localhost:~] fz% printf '#!/bin/sh n echo "foo" n ' > test1
[localhost:~] fz% chmod 700 test1
[localhost:~] fz% alias test2 ./test1
[localhost:~] fz% test2
foo

then at this point the title bar says "./test1", not "foo".

(Note: the n's in the first line should be preceded by backslashes, which geeklog eats. I call the script test1, because a /bin/test already exists on my system. Also, for me, ls is not among the default aliases in /usr/share/init/tcsh/aliases.)



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: hysterion on Feb 07, '02 07:05:40PM

> Note: the n's in the first line should be preceded by
> backslashes, which geeklog eats.

...and so should the exclamation mark. Geeklog, geeklog...



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: aranor on Feb 07, '02 08:24:20PM

Right. I've looked at the geeklog 1.1 script a bit, and apparently in plain text comments it eats slashes twice (so \\\\\\\\ becomes \\), and in html comments it eats slashes three times (so \\\\\\\\\\\\\\\\ becomes \\). When previewing, slashes are eaten once for plain and twice for html in the textarea, so when doing preview you should copy your text and re-paste before confirming the preview.



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: aranor on Feb 07, '02 08:27:14PM

Whoa, that's even weirder. What's shown in the preview area apparently isn't quite what goes into the saved comment. Hmmm.... Anyway, I guess it eats once for plain text and twice for html, so \\ becomes \ in plain text and \\\\ becomes \ in html



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: geohar on Feb 08, '02 08:37:11AM

the problem is that typing ls puts junk in the title bar (the output of ls).
now ls was defined on my system as : alias ls "ls -F"
so my supposition was that the problem is with aliases.
The test which shows it is the following

printf '#! /bin/sh \n echo "foo"' > test
chmod 700 test
alias test "test dummyarg"
test

will display foo, rather than test in the titlebar.
This is the analogue of what the ls alias does right?

again, the n should be escaped to be a newline :)



[ Reply to This | # ]
Correct alias with no bugs?
Authored by: geohar on Feb 08, '02 09:20:32AM

the best illustration of what's wrong with the script

install the title-change postcmd
type: echo $foo
where foo is non-existant
postcmd will try to interpret foo, will fail and you will get

foo: Undefined variable.
Faulty alias 'postcmd' removed.
foo: Undefined variable.

The problem is that you'd like !# to return the textual representation of the last line, _without_ doing substitutions on it....

I therefore believe that the following is the correct command

alias postcmd 'echo -n "^[]0;"`history -h 1`"^G"'

George



[ Reply to This | # ]
Correct alias with no bugs?
Authored by: geohar on Feb 08, '02 03:03:04PM

_or_ if you really want !# so that aliasses are expanded,

then

alias postcmd echo "'^[]0;\!#^G'"

where the !# above is escaped with a backslash (don't know if geeklog will kill it though ;)

will work, and won't fail on undefined variables, because the whole thing it echoes is in single-quotes...

George



[ Reply to This | # ]
Correct alias with no bugs?
Authored by: hysterion on Feb 08, '02 07:43:23PM
> if you really want !# so that aliasses are expanded, > > then > > alias postcmd echo "'^[]0;!#^G'" > > where the !# above is escaped with a backslash > (don't know if geeklog will kill it though ;) Ah, good! I had also noticed this "Faulty alias removed" problem, when you feed it an undefined variable. And indeed this seems to fix it. Provided, that is, I still include the sched part, thus:
sched +0:00 alias postcmd echo -n "'^[]0;!#^G'"
(with backslash before the "!"). Otherwise, opening a new terminal gives me: Welcome to Darwin! echo: No match. Faulty alias 'postcmd' removed. [localhost:~] fz% That still leaves the ls problem open, though...

[ Reply to This | # ]
Correct alias with no bugs?
Authored by: hysterion on Feb 07, '03 08:34:41AM

Actually this one still gives an error when the undefined variable is in quotes:

[localhost:~] fz% echo '$foo'
foo: Undefined variable.
Faulty alias 'postcmd' removed.
$foo



[ Reply to This | # ]
Correct alias with no bugs?
Authored by: geohar on Feb 08, '02 03:03:17PM

_or_ if you really want !# so that aliasses are expanded,

then

alias postcmd echo "'^[]0;\!#^G'"

where the !# above is escaped with a backslash (don't know if geeklog will kill it though ;)

will work, and won't fail on undefined variables, because the whole thing it echoes is in single-quotes...

George



[ Reply to This | # ]
Correct alias with no bugs?
Authored by: Anonymous on Feb 10, '02 08:56:01PM

Works for me, no problem. At least ls is still displayed as ls, rather than the first line of the output (which is exactly what you DON'T want). I still wish it could display what directory, but hey there's always pwd and the prompt. ;)



[ Reply to This | # ]
Problems with ls and ll investigated
Authored by: hysterion on Feb 08, '02 07:24:32PM

> printf '#! /bin/sh n echo "foo"' > test
> chmod 700 test
> alias test "test dummyarg"
> test
>
> will display foo, rather than test in the titlebar.
> This is the analogue of what the ls alias does right?

I still can't reproduce this one. If I do the exact same thing (again, insert backslash before the n):

[localhost:~] fz% printf '#! /bin/sh \n echo "foo"' > test
[localhost:~] fz% chmod 700 test
[localhost:~] fz% alias test "test dummyarg"
[localhost:~] fz% test

it just sits there (no output, no change in the title bar). If I fix it thus:

[localhost:~] fz% printf '#! /bin/sh \n echo "foo"' > test
[localhost:~] fz% chmod 700 test
[localhost:~] fz% alias test "./test dummyarg"
[localhost:~] fz% test
foo

then the script works but I get "./test dummyarg", not "foo", in the title bar. I tried a few other changes, but none will put "foo" in the title bar.

I see the problem with aliasing ls -<something>, though -- it looks like the title bar catches the first line of output. For example:

[localhost:~] fz% alias ls 'ls -l'
[localhost:~] fz% ls

drwxr-xr-x 22 fz staff 704 Feb 7 06:05 Applications
drwx------ 16 fz staff 500 Feb 7 01:33 Desktop
drwx------ 21 fz staff 670 Feb 8 14:46 Documents
drwx------ 36 fz staff 1180 Jan 31 01:34 Library
drwx------ 3 fz staff 264 Jan 25 02:55 Movies
drwx------ 2 fz staff 264 Dec 10 08:37 Music
drwx------ 3 fz staff 264 Jan 30 06:50 Pictures
drwxr-xr-x 10 fz staff 296 Jan 26 14:18 Projects
drwxr-xr-x 4 fz staff 264 Nov 20 17:51 Public
drwxr-xr-x 5 fz staff 264 Nov 20 17:51 Sites
-rwx------ 1 fz staff 23 Feb 8 19:12 test
-rwx------ 1 fz staff 21 Feb 7 17:29 test1

the title bar says "total 24". The weird thing is, it only seems to happen when the alias itself is named ls. Indeed, compare the above with:


[localhost:~] fz% unalias ls
[localhost:~] fz% alias lt 'ls -l'
[localhost:~] fz% lt
total 24
drwxr-xr-x 22 fz staff 704 Feb 7 06:05 Applications
drwx------ 16 fz staff 500 Feb 7 01:33 Desktop
drwx------ 21 fz staff 670 Feb 8 14:46 Documents
drwx------ 36 fz staff 1180 Jan 31 01:34 Library
drwx------ 3 fz staff 264 Jan 25 02:55 Movies
drwx------ 2 fz staff 264 Dec 10 08:37 Music
drwx------ 3 fz staff 264 Jan 30 06:50 Pictures
drwxr-xr-x 10 fz staff 296 Jan 26 14:18 Projects
drwxr-xr-x 4 fz staff 264 Nov 20 17:51 Public
drwxr-xr-x 5 fz staff 264 Nov 20 17:51 Sites
-rwx------ 1 fz staff 23 Feb 8 19:12 test
-rwx------ 1 fz staff 21 Feb 7 17:29 test1

Can anyone explain this ?!?







[ Reply to This | # ]
hum, it works so so
Authored by: hysterion on Feb 07, '02 04:36:56PM
I can't reproduce your exact problem (for me, an alias like ll simply
appears expanded in the title bar), but [0m is the ANSI escape sequence
that removes all character attributes. It would be used somewhere if,
for instance, you set your $prompt or ls to be bold or colored.


[ Reply to This | # ]
Another use of this useful escape sequence
Authored by: d1taylor on Feb 07, '02 06:00:54PM
I don't need my window title changing to be the last command. Instead, I like to keep track of when I 'ssh' off to another box. Here's how I've implemented that (all of this is in my .cshrc file since I'm running tcsh):
alias winname  "echo -n '^[]0;!^^G'"

alias testbox "winname Connected to testbox;ssh testbox.com;winname Darwin"

winname Darwin
This way when I log in, the window is automatically named "Darwin", then when I ssh out, it titles the window with the remote system name, and restores it to Darwin when I'm done.

[ Reply to This | # ]
Re: Another use of this useful escape sequence
Authored by: Paul Burney on Feb 07, '02 08:16:07PM

That's a great idea. Here's how I'm implementing it:

#!/bin/sh
if [ $# != 1 ]
then
title='Burney Terminal';
else
title="$1";
fi
echo -n "^[]0;$title^G"

The same rules as above apply for creating the control characters ^[ and ^G as in the above postings. I've saved this file in my path as "settermname" so I can just call settermname with an argument to change the title to the argument. If no argument is specified, it defaults to my default.

I now use this for my other aliases, like this:

alias sshweb "echo 'Connecting to burney.ws via SSH...'; settermname 'SSH -> burney.ws'; ssh burney.ws; settermname;"

When I exit the other server, the terminal is set back to the default name.

Note, this simple script works in the default TCSH shell. I don't know about others. YMMV.



[ Reply to This | # ]
Saying 'ls' doesn't help!
Authored by: Anonymous on Feb 07, '02 07:38:35PM

OK, I want to know exactly what good you guys think it is for the title bar to say 'ls'. It gives me NO useful information at all. Now can any of you *nix wizards figure out a way to put the path up there, so it says 'ls /Applications/' for example? That would be very nice.



[ Reply to This | # ]
More form the Howto
Authored by: hysterion on Feb 07, '02 08:15:15PM
Actually, the referenced howto also gives a clever way to put the working directory in the title bar, flexible enough that you can have it exactly as in Terminal.app's default prompt. (That is, limit it to the last 3 components of the path, and replace $HOME by ~.) Again this works on the Mac, and can be done as follows. Create a file named 'rc.mine' in ~/Library/init/tcsh/; edit it to contain this line:
set prompt = "%{^[]0;%c3^G%}[%m:%c3] %n%# "
where again ^[ and ^G are typed as escape sequences; and save. If you have both this rc.mine and the suggested aliases.mine, then... well, see for yourself :-)

(Incidentally, does anyone know if and how one can type those escape sequences in TextEdit, or BBEdit?)



[ Reply to This | # ]
Saying 'ls' doesn't help!
Authored by: bdestena on Mar 02, '02 06:24:23PM

I found out how to get the directory path in the terminal app
I'm using a zsh, just put this in your .zshrc file:

#Title bar displaying the current directory path
precmd () { print -Pn 'e]0;%~a' }

you can replace precmd by chpwd but you wont get a directory path in the title bar before you change directories once.

for a tcsh something like:
alias precmd 'echo -n "



[ Reply to This | # ]
ultimate title bar
Authored by: jeremaya on Feb 07, '02 08:27:08PM
Okay, combining hysterion's arcane knowledge of XTerm title bars, and my unreasonable love of the command line utility "cut," we arrive at the ultimate title bar. The line below, when put in ~/Library/init/tcsh/aliases.mine, will put the name of your machine, followed by a colon, followed by the first 25 charachters of the most recent command. I have the terminal size set to show in my Terminal prefs, so I get something like ornithopter: less -f /dev/random 80x45 in my title bars. When typing in the line below, type the ^[ and ^G as ctrl-v, ctrl-[ and ctrl-v, ctrl-g.

sched +0:00 alias postcmd 'echo -n "^[]0; `hostname|cut -d. -f1`: `echo !# | cut -b 1-25`^G"'

[ Reply to This | # ]
ultimate title bar
Authored by: jkusnier on Feb 08, '02 02:53:10PM

adding
sched +0.00 alias precmd 'echo -n "^[]0;!#^G"'
to your ~/Library/init/tcsh/aliases.mine will display remove the last command from the title bar after the command exits



[ Reply to This | # ]
ultimate title bar
Authored by: hysterion on Feb 07, '03 08:36:52AM

This one still gives the error

[localhost:~] fz% echo $foo
foo: Undefined variable.
Faulty alias 'postcmd' removed.
foo: Undefined variable.

and in addition:

[localhost:~] fz% echo "alpha beta"
Unmatched `.
Faulty alias 'postcmd' removed.
alpha beta



[ Reply to This | # ]
A year later...
Authored by: hysterion on Feb 07, '03 09:07:19AM


Another recent titlebar hint reminded me of this one just in time for its first anniversary :-)

I'm unconvinced by the current "Addendum to original story", because as pointed out above, the proposed "ultimate title bar" still causes the same sort of bugs we've seen. In fact, to recap, postcmd has two (apparently well known) problems with shell expansions:
  1. Expansion of (undefined) variables: if foo is undefined, then (depending on how single and double quotes were nested in the alias definition) one of the following will cause the postcmd alias to be removed as 'faulty':
    • echo $foo
    • echo '$foo'
    • echo "$foo"
  2. Expansion of 'same-name' aliases: if we have 'same-name' aliases like those below, then the first line of their output gets chopped. (Moreover the third example -- which assumes that that GNU ls is installed -- also causes the problem seen by z0mbi3.)
    • alias df 'df -k'
    • alias ls 'ls -a'
    • alias ls 'ls --color'
Workaround: use printf instead of echo, following Marc Liyanage (googlewhack); in addition, (following man tcsh), prevent variable expansion by using a :q modifier; thus:
sched +0:00 alias postcmd 'printf "\\033]0; %s \\007" "\\!#:q"'
With this, one problem remains: same-name aliases never make it into the title bar (but at least now their output isn't chopped). This seems due to the way tcsh avoids a recursive expansion loop -- see item 20 from the bottom of this file. So far as I can see, the only way out of that is to always use backslash escapes when defining such aliases, thus:
    • alias df '\\df -k'
    • alias ls '\\ls -a'
    • alias ls '\\ls --color' (etc.)
Alternatively: if we don't insist on seeing aliases mnemonically expanded in the title bar, then (following George) replace \\!#:q by `history -h 1` in the line above. (We can still use printf, with the benefit that these lines can now be pasted into any editor, with no need to talk about vi escape sequences.)

P.S.: In all of the above, read any number of consecutive backslashes as just one backslash. (I tried to put enough of them that at least some would survive the geeklog treatment...)

[ Reply to This | # ]
A year later...
Authored by: dpwk on Nov 29, '03 03:12:09AM
Oof! I spent too much time looking at this and all related hints I could find, and here is the synthesis I came up with- thanks to all the many people whose code appears in chunks here, you've given me a great education in all of this.

In my aliases.mine:

sched +0:00 alias postcmd 'printf "\033]0; %s \007" "`date "+%H:%M"` `echo -n "${cwd}"| sed -e "s%$HOME%~%"` => `history -h 1`"'

alias precmd 'printf "\033]0; %s \007" "`date "+%H:%M"`" ${cwd}"| sed -e "s%$HOME%~%"'

- prints time, cwd, current job
- works with aliased commands
- substitutes "~" for your home path
- doesn't add blank lines or obscure certain single line responses (as some of the other similar solutions did in my environment)
- doesn't use pesky escape sequences

also, more futzing on other hints elsewhere gave me this oh so fun prompt:

if ( $?prompt && $?tcsh ) then
  set BG_COL = 31
  alias inc_bg 'if ($BG_COL >= 37) @ BG_COL=30; @ BG_COL++'
  alias precmd 'set prompt="%{\033[${BG_COL};01m%}%B%m%b:%~ %h%# "; inc_bg'
  alias uncool 'unalias precmd; set prompt="%B%m%b:%~ %h%# "'

(put this in your .login, start a new shell, and hit return a few times.. you'll need a terminal that supports colors for this.)

now I'm wondering if I set up some sort of precmd collision here.. oh well, it all works, at least for now..

---
gigabling megashiznit

[ Reply to This | # ]

A year later...
Authored by: dpwk on Nov 29, '03 03:18:14AM
Oof! I spent too much time looking at this and all related hints I could find, and here is the synthesis I came up with- thanks to all the many people whose code appears in chunks here, you've given me a great education in all of this.

In all code below, 2 backslashes actually means one backslash.

In my aliases.mine:

sched +0:00 alias postcmd 'printf "\033]0; %s \007" "`date "+%H:%M"` `echo -n "${cwd}"| sed -e "s%$HOME%~%"` => `history -h 1`"'

alias precmd 'printf "\033]0; %s \007" "`date "+%H:%M"`" ${cwd}"| sed -e "s%$HOME%~%"'

- prints time, cwd, current job
- works with aliased commands
- substitutes "~" for your home path
- doesn't add blank lines or obscure certain single line responses (as some of the other similar solutions did in my environment)
- doesn't use pesky escape sequences

also, more futzing on other hints elsewhere gave me this oh so fun prompt:

if ( $?prompt && $?tcsh ) then
  set BG_COL = 31
  alias inc_bg 'if ($BG_COL >= 37) @ BG_COL=30; @ BG_COL++'
  alias precmd 'set prompt="%{\033[${BG_COL};01m%}%B%m%b:%~ %h%# "; inc_bg'
  alias uncool 'unalias precmd; set prompt="%B%m%b:%~ %h%# "'

(put this in your .login, start a new shell, and hit return a few times.. you'll need a terminal that supports colors for this.)

now I'm wondering if I set up some sort of precmd collision here.. oh well, it all works, at least for now..

---
gigabling megashiznit

[ Reply to This | # ]