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

10.7: Use Terminal proxy icons in zsh UNIX
The useful site macosxautomation.com reveals that Terminal.app in Lion can use proxy icons, those document icons to the left of the window title in many apps. Right-clicking on that icon opens a menu and you can open your current directory (or its parents) in the Finder. You can also drag the proxy icon onto the desktop to create an alias to your current directory.

If you don't use bash (the default shell on Mac OS X), you won't see the new proxy icons, though.

Here's how I extended the proxy icons feature to zsh. To work, your shell has to use special escape codes to pass the current directory to Terminal.app. This is the same mechanism used to set custom title bars.

Apple placed code in /etc/bashrc to support proxy icons. With slight modification, that code can go into your ~/.zshrc and do the same thing:
update_terminal_cwd() {
    # Identify the directory using a "file:" scheme URL,
    # including the host name to disambiguate local vs.
    # remote connections. Percent-escape spaces.
    local SEARCH=' '
    local REPLACE='%20'
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
    printf '\e]7;%s\a' "$PWD_URL"
}
autoload add-zsh-hook
add-zsh-hook chpwd update_terminal_cwd
update_terminal_cwd
The update_terminal_cwd function is Apple's, taken from /etc/bashrc. The last three lines are mine. They set up the proxy icon at the beginning of the shell session, and cause it to be updated every time you change directory. The code to do that in bash won't work in zsh.

[crarko adds: I haven't tested this one.]
    •    
  • Currently 3.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[4,843 views]  

10.7: Use Terminal proxy icons in zsh | 4 comments | Create New Account
Click here to return to the '10.7: Use Terminal proxy icons in zsh' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.7: Use Terminal proxy icons in zsh
Authored by: zhell on Aug 26, '11 09:26:49AM
Thanks, great hint! One of the many escape sequences in my zsh prompt seems to enable this feature already and it also works on remote hosts that have the same .zshrc. What I did not know is the function "add-zsh-hook". Thanks so much for showing how it is used. Its use is explained in "man zshcontrib", or at the ZSH sourceforge.net site

[ Reply to This | # ]
10.7: Use Terminal proxy icons in zsh
Authored by: BlaatMekker on Aug 26, '11 03:27:28PM
I have a screenshot right here of it in action.

[ Reply to This | # ]
10.7: Use Terminal proxy icons in zsh
Authored by: usovalx on Aug 27, '11 09:10:45AM
Slighty more correct version is given below. This adds a guard aroud it (to prevent it sending crap if you use non-apple terminal) and uses $HOST instead of $HOSTNAME.
if [ "$TERM_PROGRAM" = "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
        # Identify the directory using a "file:" scheme URL,
        # including the host name to disambiguate local vs.
        # remote connections. Percent-escape spaces.
        local SEARCH=' '
        local REPLACE='%20'
        local PWD_URL="file://$HOST${PWD//$SEARCH/$REPLACE}"
        printf '\e]7;%s\a' "$PWD_URL"
    }
    autoload add-zsh-hook
    add-zsh-hook chpwd update_terminal_cwd
    update_terminal_cwd
fi
Edited on Aug 27, '11 09:11:30AM by usovalx


[ Reply to This | # ]
10.7: Use Terminal proxy icons in zsh
Authored by: radiola on Aug 30, '11 10:30:00PM

@usovalx: Good catch on $HOSTNAME vs. $HOST for zsh. The basic proxy icon functionality works even without that, so I didn't notice it.

To be clear about the guard, that's only necessary for emacs's shell-mode. If you don't use it, you don't need to modify your .zshrc to add it. Too bad emacs doesn't set $TERM_PROGRAM on OS X -- that check does successfully keep the proxy-icon stuff out of the way of other terminal emulators and things like DTerm, which either don't set $TERM_PROGRAM or set it to something else.

I suppose that if you use zsh, you're practically bound to be using other weirdo stuff. :)



[ Reply to This | # ]