Monday, 7 January 2013

Oh My ZSH


Sometimes (=always) I get really frustrated when I know how little I know about things. Shell scripting is one of them. But apart from that shell is an essential tool for me. I couldn't live without it. So I tell you some nice things about my favorite shell: ZSH.


ZSH is sort of a standard shell interpreter, very similar to Bash. Scripting works the same way as in Bash, with some extra additions. I say this to those few who afraid of changing their shells :) Don't be. None of my old scripts broke when I moved. (Btw see there is an awesome ZSH cheat sheet too.)

So what's the fuzz about that? Thanks for asking, it's the Oh my Zsh plugin that you can find on Github: https://github.com/robbyrussell/oh-my-zsh. Installation is fairly simple, you need to have Ruby and Git already, and then one command in your terminal:

curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

That's all you need to start :) Neat, huh.
First thing you'll see you have a cool new design. Actually you can have so fancy designs you can craft or get a new one each day. Here you are a small gallery of it: https://github.com/robbyrussell/oh-my-zsh/wiki/themes.

The basic architecture is pretty simple. You have your personal config file in your home:

~/.zshrc

If you want to add some alias, change the theme, add a plugin or a script - put it here. Like for changing the theme just edit this line:

ZSH_THEME="selected_theme's_name"

and then open a new shell instance so it knows what to load. Other important folder is:

~/.oh-my-zsh/

where you can find all the additions to ZSH. We'll cover that shortly.

And here comes the fun. Yeah, not like Katy Perry fun, it's command line fun. ZSH has some built in candies. History is central, so you can access your commands in any terminal instances. ZSH can lookup old commands by prefix. For example if you need to SSH and you know you used it before on that particular host - just type:

# ssh 

and hit the up key. It will lookup your history where you used SSH. Awesome, huh? I tell you better. Type SSH and hit TAB key twice. It offers all available hosts you've touched. That double TAB works on file lookup too. Whenever you about to enter a path and lazy (yeah I know) just hit TAB twice and you can navigate through the files with the arrow keys. It even works with patterns. You only have to guess the file name and ZSH completes it. Also you can jump in the path history easily, see the other cheat sheet for that.

And that's just the beginning. Ready? Look at the Plugins folder in ~/.oh-my-zsh/plugins . Here you are a short list of my essential plugin stack.


Git

The Git plugin offers awesome shortcuts and helpers to version control. It shows you in the prompt which branch you're in, if you have uncommitted code in your local area, you have conflict, etc.


OS-X

It's a great addition to the terminal to access default system functions, such as terminal actions, the trash or preview. Very useful.


Rsync

If you're like me forgetting params all the time it's a nice tool to use rsync the proper way (copy, update, sync or move).


Screen

Screen is really handy terminal app to work with screens and permanent sessions. The plugin enhance the experience for you.


Sprunge

Sprunge is a great minimalistic pastebin application. It lets you to use the functionality from your terminal and publish it online (and get the sharing URL ofc.).


Sublime

I use Sublime a lot. So for me it's really productive I can fire up the editor with the selected target easily.


Symfony2

It provides you some nice shortcuts to the Symfony2 console. Really helpful.


Taskwarrior

It has a great completion to Taskwarrior the command line todo list tool.


LOL

Lolspeak to your terminal. Seriously a must have feature ^^.


Nyan

Nyan cat to your terminal. Another must have.



I didn't explain these functionalities in details on purpose, they know too much and it's easy to check their capabilities. Just pick a plugin, check its folder and read the *.zsh file. They are very talkative. And there are much more plugins than I've listed, for Ruby people, Java fans, etc.

---

Did I miss any important feature of ZSH and Oh-my-ZSH? Let me know.

Peter

6 comments:

  1. Recommended:
    http://grml.org/zsh/zsh-lovers.html

    For zsh lovers :)

    ReplyDelete
    Replies
    1. Wow that looks neat :) Thanks a lot for the link :)

      Delete
  2. Hi Peter, I'm a convert to zsh since reading this!

    Here are some notes for other newbies.

    NEWBIE-FRIENDLY THEME

    As I was starting from using bash I didn't want a theme that was too radically different, so I have gone with alanpeabody - the most bash-like one I could find.

    - In my .zshrc ...
    ZSH_THEME="alanpeabody"

    (To see it, go here http://zshthem.es/ and type alanpeabody).

    FIND ALL FILES CHANGED IN THE LAST DAY

    Check this out:
    ls -tld **/*(m-1)

    (it takes a while to run first-time-round, then lets you see everything changed in the past day)

    For more like this see:
    http://www.rayninfo.co.uk/tips/zshtips.html
    ... and as recommended by Balasz:
    http://grml.org/zsh/zsh-lovers.html

    PER-DIRECTORY HISTORY

    I'm just starting to get into using plugins. I'd like to give a shout-out to the per-directory-history plugin.

    This makes me happy because now I can go to any directory and just see the history for commands that were run in that directory before. I've been looking for something like this for ages.

    https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/per-directory-history/per-directory-history.plugin.zsh

    Instructions:

    - add or append 'per-directory-history' to the plugins to your .zshrc
    plugins=(per-directory-history)
    - if you already have plugins enabled then append 'per-directory-history'
    plugins=(git cyan per-directory-history)

    - run source to reload the .zshrc
    source ~/.zshrc
    (alternatively restart your terminal)

    now, you have history both globally and per-terminal.
    toggle using ctrl G

    ReplyDelete
  3. Hi Michael,
    You're addition is pretty cool. The directory based history is very useful, I missed that indeed. Thank you for your comment :)

    ReplyDelete
  4. I have no idea if you still will read this, but what theme is that? I am very intrigued.

    ReplyDelete
  5. Hi xDexter,
    It's called agnoster: https://gist.github.com/agnoster/3712874
    Peter

    ReplyDelete