How can I get the Terminal.app in OS X to display the current directory in its window or tab title?
I'm using the bash shell.
|
How can I get the Terminal.app in OS X to display the current directory in its window or tab title? I'm using the bash shell. | ||||
feedback
|
|
Depends on your shell. This article displays multiple methods. I personally use zsh which has a convenient precmd() function which is run before each prompt.
Although the other questions list bash methods, they alias cd. Bash provides an inherent method that chains off just the prompt.
| |||||||||||
feedback
|
|
Bash solutions involving PS1 and the PROMPT_COMMAND both fail if the directory has Unicode characters in it (at least on Snow Leopard). The best solution I could come up with was to do the equivalent of
Unfortunately this doesn't work directly, so instead I defined a shell function to do the
This works well for Latin accents, which will be in Unicode NFD (the accents will disappear but the underlying Latin character will remain). Unfortunately, it will completely fail for things like Chinese. | |||
feedback
|
|
As of Mac OS X Lion 10.7, Terminal has an explicit escape sequence for setting the working directory, which Terminal displays using the standard window "proxy" icon. This enables you to Command-Click it to see the path, reveal in Finder, or drag it like any other folder. In addition, Terminal can use this to create another terminal at the same directory, and to restore the working directory when quitting/restarting Terminal (when Resume is enabled). It also enables restoring directories for Window Groups. It's the same Operating System Command (OSC) escape sequence as for the window and tab titles, but with the first parameter set to 7. The value should be a "file:" URL, which enables percent-encoding special characters so it can handle all valid pathnames. You should also include the hostname so Terminal can determine whether it's a local directory; Terminal will avoid using it as the current working directory if it's from a different host. On a related note, Terminal similarly supports setting the "represented file" using the OSC escape sequence with a parameter of 6. If set, the proxy icon will display this instead of the working directory. For example, I have emacs and less configured to reflect the currently displayed file/buffer in the proxy icon. This enables these tty-based programs to be more integrated with the surrounding OS. The working directory behaviors are enabled by default for bash (the default shell on Mac OS X). See /etc/bashrc for the relevant code. It's also probably worth mentioning that Lion Terminal now supports setting the tab title independently from the window title using the OSC escape sequence. | |||
|
feedback
|
|
Enter this into your ~/.profile or equivalent file:
The first line contains two special characters that can't be copied/pasted, but you can download the text from here: http://blog.nottoobadsoftware.com/files/setterminaltitle.sh. | |||
feedback
|
|
Copy & paste into file ~/.profile: This will set the title of the current terminal tab to the name of the folder you are in (NOT the whole path). | |||
|
feedback
|