from Hacker News

My favourite Zsh features

by joejag on 11/15/14, 2:52 PM with 43 comments

  • by stormbrew on 11/15/14, 5:18 PM

    Something I've noticed is that osx users believe bash to be less capable than it is because of the poor out of the box configuration of bash on that platform (in addition to the ancient version installed). Particularly the tab completion stuff. It doesn't pop up menus and all that, but the ubuntu install of bash has a ton of context-sensitive tab completion stuff in it, and the git package properly installs __git_ps1 and very good tab completion (which even the homebrew git recipe on osx doesn't manage to do).

    This is not to say that zsh doesn't have a lot more features, just that the comparison from the perspective of an osx user tends to be a lot more stark.

  • by justinmayer on 11/15/14, 8:02 PM

    While we're on the topic of non-bash shells, I recommend taking a look at Fish: http://fishshell.com/

    I wrote a tutorial on how to install it on Mac OS X and Ubuntu: http://hackercodex.com/guide/install-fish-shell-mac-ubuntu/ For me, the chief advantage to Fish is that you get more functionality out-of-the-box than with most other shells, including zsh.

    I also published two related projects for easily sharing Fish shell enhancements — Tacklebox https://github.com/justinmayer/tacklebox and Tackle https://github.com/justinmayer/tackle) — because I wanted a way for folks to be able to have access to multiple plugin repositories and easily share their favorite shell snippets.

  • by kstock on 11/15/14, 6:58 PM

    A killer feature not listed here is global aliases. These are aliases that can be anywhere in a line. The most use I get out of this is from having

       alias -g L=' | less '
    
    which allows me to simply append "L" to a line to pipe the output to less.

        ls L
    
    The pattern of short all-caps global aliases for ' | command 's make building/editing pipelines really easy/fast.

        alias -g C=' | wc -l '
        alias -g A=' | ack-grep '
        alias -g S=' | sort '
        alias -g JQL=' | jq -C  | less'
    
        ls **/*png L
        ls **/*png A -v regex C
        curl $site JQL
    
    putting

    bindkey '^g' _expand_alias

    in your zshrc allows you to expand aliases with <C-g>

    You can find more global aliases and tips at: http://grml.org/zsh/zsh-lovers.html

  • by jryan49 on 11/15/14, 5:19 PM

    One of my favorites instead of aliasing ".."'s is this function:

      rationalise-dot() {
        if [[ $LBUFFER = *.. ]]; then
          LBUFFER+=/..
        else
          LBUFFER+=.
        fi
      }
      zle -N rationalise-dot
      bindkey . rationalise-dot
    
    When you hit ... it will produce a slash and a ../ for each . you type after that.
  • by andrewstuart2 on 11/15/14, 5:14 PM

    The tab completion isn't limited to the beginning of file names either. Useful when many of your file names start with the same pattern but then diverge.

      ls something<TAB>
    
    will complete to

      ls prefixSomething.sh
    
    if it's unique enough. This is probably my single favorite zsh feature.
  • by billiob on 11/15/14, 4:52 PM

    One feature not listed there that use a lot is "setopt autocd". Just type in a directory name and hit Enter and it will automatically "cd" to it. No need to alias ".." to "cd ..".
  • by seniorsassycat on 11/15/14, 6:49 PM

    I love that if you have vi key bindings active you can update the prompt when you change modes. I never used vi bindings in bash because I would become confused when I entered normal mode without realizing it. In zsh it was easy to configure my prompt to change colors and symbols when outside insert mode.

    https://github.com/everett1992/dotfiles/blob/master/home/.zs...

    I have noticed that zsh's globbing is greedy.

        `grep ^foo *` tries to expand ^foo when bash would pass it as a string to grep.
  • by rafaelnonato on 11/16/14, 12:21 AM

    It seems to me that the only two features I can't get in my modern bash is completion of the kind cd /firstletters/firstletters/<tab> ==> /fullname/fullname and the keyboard navigable menus.
  • by gcb0 on 11/15/14, 5:51 PM

    bash only needs better history manipulation. absolutely nothing else.

    all those z shell features the only useful one (i.e. the only one that's impossible on bash) is the recursion (i typed star star, but hn hides it) hack.

    and it's awful. because now you just learned something that you can't use anywhere else! and to search that same way in a shell script? though luck.

    also it has less parameter then find. and find is available everywhere so you can use it in scripts.

  • by jradd on 11/15/14, 7:48 PM

    Any favorite command I reference is likely to be listed here: http://www.zzapper.co.uk/zshtips.html
  • by changs on 11/15/14, 7:15 PM

    (Shameless self-promotion) I'd like to recommend a small and unobtrusive config for ZSH: https://github.com/changs/slimzsh It is almost a default ZSH, but with small tweaks here and there to make it even nicer. I hope that you find it useful :)
  • by arianvanp on 11/15/14, 9:48 PM

    Clever history doesn't really work for me when I use `sudo`

    say I type

        $ sudo pacman -Syu 
        $ sudo chown blah blah
        $ sudo pa <UP>
    
    I just get `sudo chown blah blah` instead of `sudo pacman -Syu`

    Does anybody know how to fix this? :)

  • by rnhmjoj on 11/15/14, 6:41 PM

    The up arrow to go back in history matching the partial command I typed is has become essential to me.

    I feel lost without it, more than the tab completion.

  • by krick on 11/16/14, 2:47 AM

    <CTRL>+X <CTRL>+E works in bash as well.
  • by zobzu on 11/15/14, 7:11 PM

    interestingly you can s/bash/zsh/ and s/zsh/bash/ in this text and everything also works.