What zsh features do you use? [closed]

I do a lot of work in the terminal so I have learned a lot about my shell of choice, zsh. What features of zsh do you use to make yourself that much more productive at work? One of my favorites is the multi-dir autocomplete. So instead of typing cd /fo{tab}/ba{tab}/ba{tab} I can just do cd /fo/ba/ba{tab} and save that many keystrokes!


Solution 1:

Just found this little gem:

cd old new

This form of cd replaces all occurences of old in the full path of the present directory with new and tries to change the working directory to the resulting path.

For example, if your working directory is /home/larry/code/backrub/server and you want to switch to /home/sergei/code/backrub/server, all you need to do is:

cd larry sergei

Solution 2:

I'll keep to things that, as far as I know, bash can't do.

  • Fancy completion. Yes, bash has some of it, but zsh has knows more commands, is often more precise, and has many more configuration possibilities.

  • The ** glob, e.g., **/foo looks for files called foo in subdirectories recursively. (And ***/foo also follows symlinks.) Two characters instead of a long find command (which is hard to get right if some file names include special characters like spaces and quotes).

  • Less often, glob qualifiers as in foo*(*) (like foo*, but only retain executable files), foo(.) (only regular files, not directories), foo(-@) (only dangling symlinks), foo*(m-5) (only files modified in the last 5 minutes), foo*(om[1]) (the most recently modified file), etc.

  • autocd: Typing a directory name as a command changes into it (cd or pushd depending on how you configured it). The cd command is three characters too long! I can't use bash for more than thirty seconds without feeling the pain. I also have a few single-character functions, such as

function - {
  if [[ $# -eq 0 ]]; then
    cd "$OLDPWD"
  else
    builtin - "$@"
  fi
}
  • The zmv builtin, and
alias zcp='noglob zmv -C'
alias zln='noglob zmv -L'
alias zmv='noglob zmv'
  • While I'm at it, the noglob builtin.

  • The precmd and preexec hook functions: I use preexec to set my terminal's title to include the running command, and precmd to replace the command with its exit code. Something like

term_title_base='@%l:  %1~'
preexec () {
  print -nr $'\e]2;'"${(%)term_title_base}  $*"'$\a'
}
precmd () {
  print -nr $'\e]2;'"${(%)term_title_base} ($?)"'$\a'  
}
  • Ctrl+Z:
    • On an empty command line runs bg (so that Ctrl+Z Ctrl+Z suspends a program and immediately resumes it in the background).
    • On a non-empty command line, suspend the current command edition: let me type another command, and when that second command line finished, I get back the first command to edit.
    • This uses the following function:
fancy-ctrl-z () {
  if [[ $#BUFFER -eq 0 ]]; then
    bg
    zle redisplay
  else
    zle push-input
  fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
  • The most important non-completion-related options: setopt append_history autocd extended_glob no_match

Solution 3:

zsh's ability to autocomplete things besides files and directories.

For example, with the git package installed, git-sh{tab} brings up:

- git command -
shortlog     -- summarizes git log output
show-branch  -- shows branches and their commits
show-index   -- displays contents of a pack idx file