ZSH: easy history navigation

Here's a quick help for all of you who want to be able to navigate through history much easier with ZSH.

Paste the following to your ~/.zshrc:

bindkey "\e[A" up-line-or-search
bindkey "\e[B" down-line-or-search

These changes will switch the way history navigation works in zsh; what these do is: whenever you are typing a command at shell prompt, arrow keys will navigate through history for you to find matches for the text you already entered.


Vim: forgot sudo?

Has it ever happened to you that you were modifying file that you had no permission to write in VIm? Most of us quit the editor, sometimes save the file to a temp location, then edit it again as root. The thing is - you don't really have to!

Once you open your file, you can save it as an input to external command. To do that, simply call:

:w !<command>

in this particular case, it's:

:w !sudo tee %

good luck!


Disabling ZSH user completion

ZSH has proven to be one of the most powerful shells i've used so far. It has really surprised me dozens of times and I started to love it much more than bash or anything else.

Unfortunately, ZSH can also suffer from the fact that it's so powerful. Among the things that made me almost cry was the user completion, enabled by default, that made the whole console freeze for long time before it finally resumed, giving me a list of hundreds LDAP users when all I wanted is a completion of local directory.

After longer search I finally mastered the query to get the right solution to the problem:

unsetopt cdablevars

This short line turns off auto-completion based on user names and variables. Once in place you will no longer be able to type:

cd user_name
cd var_name

instead, you're back to standard

cd ~user_name
cd $var_name

but, the upside is, the directory completion (when you press TAB key) is again working smoothly, even when you're completing commands (git etc).

Among available alternatives you can find a solution that would disable completion for a particular command, such as:

zstyle ':completion:*:complete:(cd|pushd):*' tag-order \
    'local-directories path-directories directory-stack' '*'
which will complete cd and pushd commands with directories only (so, no variables and no users for you). You can try to tweak this completion pattern to actually give you some of these mechanisms back to these commands.