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

Another useful and interesting terminal title trick UNIX
Too many Terminal windows, all alike ... for every account you care about, add the following into the .tcshrc init file:
  setenv WHOAMI `whoami`
setenv HOSTNAME `hostname -s`
if ($?TERM_PROGRAM) then
if ("$TERM_PROGRAM" == "Apple_Terminal") then
alias precmd 'echo -n "^[]2;[$WHOAMI@$HOSTNAME]^G"'
endif
endif
As usual, ^[ stands for ESC, 0x1B, ^G is BELL, 0x07 [ed: enter in vi by typing Control-V then either Control-Open Bracket or Control-G.]

The reason this is a "precmd" alias is to have the title change when 'su root' and/or remote SSH sessions are suspended. On the remote SSH host, it may be necessary to substitute a "TERM" check for "TERM_PROGRAM" ("TERM_PROGRAM" isn't passed over to the remote environment by default.)

Setting the default "Window" prefs to "Active Process Name" and "Command Key" in "Window Settings..." will have the title look like (at least with Terminal.app v1.3.1):
  [root@localhost] -- emacs -- -1
PS. Don't believe KB Article 107106; Apple's interpretation of $?TERM_PROGRAM is whishful thinking under tcsh.

[Editor's note: There are a few other useful terminal titling tricks on the site as well: 1, 2, 3, 4, 5...]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (3 votes cast)
 
[7,327 views]  

Another useful and interesting terminal title trick | 8 comments | Create New Account
Click here to return to the 'Another useful and interesting terminal title trick' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Different Directory Structure?
Authored by: pete on Jan 23, '03 03:21:53PM
Why is my directory structure significantly different from yours?

I am a Unix weenie, slowly learning, but I have noted that I do not have a .tcshrc or even a .xinitrc file anywhere. I do though have a tcsh.defaults file buried in /usr/share/tcsh/examples directory.

I have been trying to figure out how to fiddle with Terminal.app and now X11 from Apple. This is enough to throw a loop in my learning curve. Any ideas as to why mine is so different?

[ Reply to This | # ]
Different Directory Structure?
Authored by: BananaFish on Jan 24, '03 12:37:48AM
You may not have a .tcshrc file if you don't use TCSH. If memory serves, TCSH was the default shell under OS X 10.1 and before. BASH (Bourne Again SHell) was added under 10.2 and it may be the default shell now -- I just don't recall.

You'll know which shell you're using by typing echo $SHELL at the command line. When I run this command it returns /bin/bash because I use BASH instead of TCSH.

Different shells use different files for initialization. BASH uses ~/.profile and/or ~/.bashrc

[ Reply to This | # ]
Different Directory Structure?
Authored by: eckbert on Jan 24, '03 11:02:45AM

I use tcsh and have the same problems.
Since 10.2 APPLE changed some things. For my aliases i now have a file in ~/Library/init/tcsh/aliases.mine e.g.
So far so good but putting the code in a file tcsh.mine did not do any good.
Tell me if anyone had success on this



[ Reply to This | # ]
bash variant
Authored by: acdha on Jan 24, '03 02:18:54AM

I have this in my .bashrc:
PS1="\\[\\033]0;\\u@\\h:\\w\\007\\]\\u@\\h:\\w $ "

The escape sequence also works with xterm and Van Dyke's SecureCRT for Windows, so I have it in my .bashrc at work and my hosting company. It's really handy if you find yourself commonly using multiply-nested SSH connections.

I enable the current command, window size and hotkey display in my Terminal settings to complete things.



[ Reply to This | # ]
Fix the double slashes
Authored by: acdha on Jan 24, '03 03:21:12AM

That post looked right in the preview but was double-slashed in the post. Turn \\ into \.



[ Reply to This | # ]
bash variant
Authored by: jzkpr on Mar 24, '03 07:05:34PM

Will this work with GLTerm as well?

remote ssh locale to work as well. Any help is appreciated.



[ Reply to This | # ]
Getting around tcsh bugs
Authored by: hysterion on Jan 25, '03 12:09:49AM
[Editor's note: There are a few other useful terminal titling tricks on the site as well: 1, 2, 3, 4, 5...]
Regarding hint #2 here ("Display current jobs in Terminal titles"): Recently Marc Liyanage suggested yet another addendum to it. Namely, instead of echo he uses printf with a variable substitution, thus:
sched +0:00 alias postcmd "printf '^[]0;%s^G' 'BACKSLASH!#'"
(Note that BACKSLASH stands for the ever-geeklog-unfriendly backslash -- let's hope this finally gets fixed rsn... -- and that I have switched quotes ' and double quotes " in Marc's version.) The advantage is that, at least on my machine, this gets around the two known tcsh bugs already discussed under that hint:
  • if you had anything aliased to ls, then postcmd would eat its first output line;
  • if you passed an undefined variable $foo to the shell, it would barf and declare the alias `faulty'.
One more thing: somehow Marc seems able to replace the escape sequences ^[ and ^G here by BACKSLASH033 and BACKSLASH007, so that one could presumably enter this stuff using BBEdit rather than vi. Unfortunately I'm not able to get this to work. So, can anyone point to a vi-/emacs-free way?

[ Reply to This | # ]
Getting around tcsh bugs
Authored by: hysterion on Feb 07, '03 09:46:55AM
Just a word to confirm that the line with printf, \033 and \007 in place of echo, ^[ and ^G does work in BBEdit Lite. (This applies, of course, also to the precmd line of this hint.) My initial failure was due to the conjunction of three conditions:
  • The line must be terminated by a Unix line break (aka newline, ascii 0a);
  • Unlike vi, BBEdit Lite does not automatically insert such a break at ends of files;
  • If in BBEdit Lite you save, close and reopen a file which contains no Unix line breaks, it switches to Mac line breaks (ascii 0d) even if preferences are set to Unix.
So it took some time to understand why the shell couldn't see the newlines I was inserting. Apparently this bug is fixed in BBEdit 6.5.1: "When opening a file that contains no line-break characters (possibly because it's empty), BBEdit will use the preferred line-break setting (Text Files:Saving) instead of assuming the Mac line-break convention." (Also, TextEdit works fine for this.)

Besides that, I was also a little overoptimistic about the postcmd line above -- it still needs a bit more work. But since this is not directly relevant to the present hint, I've put that info where it belongs.

[ Reply to This | # ]