My favourite Zsh features
code.joejag.com
code.joejag.com
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.
For example, I contributed bracketed paste support to bash and readline. Yay! No more inadvertent command execution! No security problems pasting shell commands from web pages! You'd think we'd want that feature enabled by default, right?
Well, no. The feature will be off by default. For reasons.
http://lists.gnu.org/archive/html/bug-bash/2014-10/msg00211....
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.
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
puttingbindkey '^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
touch 'L'
ls 'L'
ls \LMuch better is to use abbreviations which expand to the full command they abbreviate after you hit the space bar.[1]
This way you can see exactly what's going to be done before you hit ENTER. It does cost one extra keystroke, but the safety is worth it.
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. alias ..="cd .."
alias ...="cd ../.."
After that, the more dots you type, the more error-prone it is, as you start to have to carefully count exactly how many directories you have to go up to get to where you want.A better alternative is a script that shows a list of parent directories and lets you select them by typing the number or letter associated with them.
For example, if your current directory is "/a/b/c/d/e/f/g/h", then the script would display something like:
List of parent directories:
1 - a
2 - b
3 - c
4 - d
5 - e
6 - f
7 - g
Please choose a directory: _
and you could type "3" to get to directory "c". That would be equivalent to typing "......", but much less error-prone and it'll save you the tedium of counting dots too. ls something<TAB>
will complete to ls prefixSomething.sh
if it's unique enough. This is probably my single favorite zsh feature.Just trying to save others some time here.
The real magic of ido is the completion engine. It's not made super explicit in that video, but it's actually possible to complete on arbitrary subsequences of strings you want.
With my configuration (using the popular "flx-ido" matching engine), if you wanted to match "README.md", you could achieve this by just typing ED. Even though E and D aren't consecutive, E appears before D. (But "GOOD_EDITING_SOFTWARE" would be matched before "README.md", because it prioritizes consecutive matches and characters at the beginnings of logical groups of characters.)
It's like ido, but reorders the commands it presents to you in most-frequently-used order, which can be a real time-saver.
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.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.
$ echo **/*.txt
Of course if you're using an ancient version of bash, you're out of luck. $ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
GNU bash, version 4.1.13(7)-release (x86_64-unknown-cygwin)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? :)
$ sudo foo
$ sudo bar
$ sudo fo<UP>
gives me `sudo foo`.I'm using zsh 5.0.7 (x86_64-apple-darwin14.0.0) on OS X 10.10 (with prezto[1]).
Even better is the function in Functions/Zle/up-line-or-beginning-search To use that, you need to first do: autoload -U up-line-or-beginning-search zle -N up-line-or-beginning-search
I feel lost without it, more than the tab completion.
Zsh would probably be worth using, except for the fact that bash is almost everywhere, and I already know it very well. It's a tough one to call.
bind '"^[[1;5A": history-search-backward'
bind '"^[[1;5B": history-search-forward'
where ^[ is the escape character. This way, crtl-up goes to previous partial match (and up goes to last command as usual). I use it all the time. # Use up/down arrows to search on partially typed command
bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward
Just type the first letter(s) and use the up/down arrows to scroll through the filtered command history. Incredibly convenient.I don't want to miss this plugin and can't really understand why not everyone wants it. I guess I am a little biased though since I wrote that plugin ;)
For menu complete with bash, the easiest is to add:
TAB menu-complete
inside of "~/.inputrc" then start a new shell to test it. Thats because it uses readline to handle command line (and inputrc configures readline).
Note: im not saying the experience is not better in zsh, just that both can do all this stuff. ive been switching between zsh and bash quite a few times myself over the last decade and use bash daily most of the time lately.
In my eyes:
zsh:
- nice and neat to configure/less bloat maybe
- powerful scripting language
- some scripting syntax requires zsh so when you get used to that... you write non-portable stuff ;)
bash:
- does approx everything zsh does and some more
- scripting language works everywhere because everyone has default to bash
- scripting language is slightly less powerful than zsh in some ways
- configuration is a little more all over the place (just like the .inputrc above)
Thus, all things considered, and given the improvements in bash over the last past .. 5 years or so - i'm using bash more often even thus zsh is also a very nice shell.