Introduction
Recently I switched from using bash to zsh as my main shell. I'd heard a lot of good things about it (and how complex it is woah) so I decided to try it out for myself, the main reason I decided to try it was because it was already installed on my system (Mac OS X) and it has emacs key bindings. What i found was "the missing shell" in a lot of ways interactive mode is similar to bash, which is great, I can easily fit into it and feel comfortable but it has a lot of things that extend on bash such as completion, globbing and customisation. Next we'll look at some of the most important aspects of zsh that I found in my short experience with it, and some examples which might motivate you into using it yourself. NOTE: I have purposely left out the scripting side of zsh because I haven't had much experience with it yet and I have about the same amount of experience with bash scripting.Why use zsh?
As mentioned before, here's some of the most important aspects of zsh for me.Expressive
One aspect of zsh that first stood out for me was its extensive globbing capabilities, for example:ls -d ^*.zsh
This will display all files except (^
) files with the .zsh
extension.ls *.zsh~i*
This will list all files with the .zsh
extension except files beginning with the letter 'i'.ls *.(zsh|rb|)
This is grouping and will print files with zsh
and rb
extensions.ls **/*zsh
Will output:configs/aliases.zsh
configs/bindings.zsh
configs/completion.zsh
configs/exports.zsh
configs/prompts.zsh
init.zsh
This globs the directories with the asterisk for zsh files.To enable these features you'll need
setopt extendedglob
in your config.Completion
Zsh's completion is more advanced than what I was used to with bash, with zsh you can get suggestions in a menu that you can browse with the arrow keys, more intelligent context aware suggestions, and more.Here's an example of context aware suggestions:
kill <TAB>
With this you'll get a list of running processes.When you're browsing a directory you also get more information about what the file is, for example:
ls ~/.z <TAB>
And you'll get what you expect, but if you have symlinked files they'll look like this:.zshrc@
The @
symbol denotes a symlink. This is only a small feature but it's nice and you don't need to ls -l
.There is also support for remote completion!
Prompts
You can have multiline prompts which can have many features attached to it like battery charge and load. see this blog for a good example. The prompt is at the heart of zsh customisation capabilities, you can pretty much configure it to look anyway you want and there are plenty of examples out there ranging from simple to mad!alias -g alias -s
With zsh you can have a file for global (-g
) aliases and suffix (-s
) aliases, for example:alias -s pdf=xpdf
So now if you execute a single file ending in pdf
is will be re-written to xpdf foo.pdf
.Global aliases are expandable anywhere on the command line, not just the beginning. Global aliases can be dangerous if something gets expanded that shouldn't.
Easy setup
To get a reasonable config it only takes about 4 lines of code:autoload -U compinit promptinit
compinit
promptinit
prompt walters
With that you get a tab completion (compinit
) and coloured prompt (promptinit
). You can see the list of built in prompts with:prompt -l
You can also add a prompt to your config with:export PS1="$(print '%{\e[1;34m%}%n%{\e[0m%}'):
$(print '%{\e[0;34m%}%~%{\e[0m%}') →
This can all go into your config file at ~/.zshrc.
As zsh is fully customisable I prefer to split up my files (see a link to my config at the end of this article). With this you're ready to go with some of the best features of zsh to play with, and as you grow your config will grow with you.Emacs
Being able to use one set of keybinding across applications is handy and as an emacs user I was pleased to find out zsh supports emacs keybinding out the box by default (Vim users can set the$EDITOR
variable in their config), so normal navigation rules apply!Conclusion
I've only scratched the surface here of what you can do with zsh and how it can make your terminal life a little bit easier. You should give zsh a try it's expressive, very powerful and fits nicely into a programmers' toolbox. If you want somewhere to get started you can take a look at my configs or if you're a git user you can clone it with: git clone git@github.com:jbw/zsh.git
Happy tweaking!References
- Z shell Homepage Here you can find links to a lot resources.
- Z shell Intro A quick-ish read on zsh to give you an overview.
- Z shell Refcard A cheatsheet sort of document.
- Caphuso's Z shell Talk A long talk on zsh magnifying some of its cool features.
Stolen from: http://jbw.cc/2010/07/20/using-zsh/
0 comments:
Post a Comment