Source Guru

zsh seems to be getting a lot of press of late …

by Mez on Dec.17, 2008, under Personal

And so it should – it’s an awesome shell.

Anyway, I’ve just spent a lot of time sorting out my zsh so it does titles in the right way. By this, I mean changing the title of your xterm window, and/or your screen window.

Anyway, here’s the code that I use for my zsh titles

precmd () {
 case $TERM in
 screen)
 print -Pn "\e]0;%n@%m: %~\a"
 print -Pn "\ekzsh: %~\e\\"
 ;;
 xterm*|rxvt)
 print -Pn "\e]0;%n@%m: %~\a"
 ;;
 esac
}

preexec () {
 case $TERM in
 screen)
 print -Pn "\e]0;%n@%m: $1\a"
 print -Pn "\ek$1\e\\"
 ;;
 xterm*|rxvt)
 print -Pn "\e]0;%n@%m: $1\a"
 ;;
 esac

}

I find this works in the same way that the default bash shell does ;) though – it also works quite nicely with screen, and sets the screen titles for you too :D

Also – my prompt lines are (inspired by Daniel Silverstone circa 2005)

export RPROMPT='%B%d%b %t '
export PROMPT='%B%n@%m %#%b '

Happy zsh’ing folks!

:, , , , ,

7 Comments for this entry

  • Jason 'vanRijn' Kasper

    Hi Martin!

    Thanks for the cool blog post! I’ve been using zsh for a long time and I’m absolutely in love with it. I tried your functions and they are really nice! =:) One thing, though…. I use the prompt built-in for my zsh shell theme (autoload promptinit; promptinit; prompt fade green) and I think it does something with precmd and preexec too, so your functions and those done by prompt were conflicting (one or the other wasn’t getting executed. Anyway, I think i’ve found a way around it. $(man zshall) shows that zsh actually allows an array of function names for precmd and preexec. So I’ve changed your function names to myprecmd and mypreexec and then declared arrays so that zsh calls my precmd/preexec as well as whatever else declares these. What I have looks like this:

    myprecmd () {
    # … the same stuff you had above…
    }
    mypreexec () {
    # … the same stuff you had above…
    }
    # now declare arrays for precmd and preexec…
    set -A precmd_functions precmd myprecmd
    set -A preexec_functions preexec mypreexec
    # now set our prompt…
    autoload promptinit
    promptinit
    prompt fade green

    Anyway, thanks again for the great blog post! Zsh rocks!! =:)

  • Mark Kretschmann

    While ZSH is getting a lot of press, I find it worth checking out the Fish shell, which is powerful and yet easy to use:

    http://www.fishshell.org/
    http://arstechnica.com/articles/columns/linux/linux-20051218.ars/2

  • Mez

    Jason:- thanks for the tip

    Mark:- I’ll have a look at it sometime ;)

  • Shadowfiend

    I’d like to offer a +1 for the Fish shell. This baby’s the best shell I’ve ever used, even though it took a little bit of getting used to due to my embedded bashisms.

    I’ve been using http://github.com/zmalltalker/fish-nuggets/tree/master for some more goodness including marking the current branch when you are in a git repository directory and such.

  • MJ Ray

    zsh? fish? Use rc, the shell whose grammar you can understand!

  • jpds

    This code looks familiar. ;-)

  • Mez

    jpds – I wrote it myself ;)

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!