What zsh features do you use?

33

34

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!

Frew Schmidt

Posted 2009-07-15T14:47:42.683

Reputation: 1 062

Question was closed 2011-08-31T04:06:40.107

2i didn't know about that feature, i'm gonna have to try that one! – Roy Rico – 2009-08-07T17:11:14.477

1Which option is that directory autocomplete? – hometoast – 2009-10-14T14:24:14.480

Answers

50

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

Agnel Kurian

Posted 2009-07-15T14:47:42.683

Reputation: 2 197

7Ok, that rules. – hometoast – 2009-10-14T14:26:01.110

21

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

Gilles 'SO- stop being evil'

Posted 2009-07-15T14:47:42.683

Reputation: 58 319

3

Love the ^Z ^Z trick, thanks! Originally was looking http://superuser.com/questions/378018/ for a solution to bind it to single key but ^Z ^Z makes so much more sense

– Evgeniy Dolzhenko – 2012-01-13T14:41:19.943

13

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

Mark Thalman

Posted 2009-07-15T14:47:42.683

Reputation: 990

1Yes, this is excellent! I also use this when I'm not quite sure what a command line switch is but I don't need to delve into the man file. – Frew Schmidt – 2009-07-15T14:56:17.350

I believe bash has this capability as well. – Keith Thompson – 2014-01-24T23:54:54.333

10

I really like the global aliases so

alias -g L='|less
alias -g DN='>/dev/null'

let me do things like

somecommand file arg L

and get paging.

hometoast

Posted 2009-07-15T14:47:42.683

Reputation: 433

I also like G for grep. – Frew Schmidt – 2009-10-21T19:32:15.967

7

As mentioned by others, zsh's autocomplete is excellent.

You can setup your own autocomplete for custom commands without too much hassle as well. To tab complete usernames as arguments to finger:

# ~/.zshrc
function userlist {
        reply=(`users | tr ' ' '\n' |sort -u `);}
compctl -K userlist finger
set COMPLETE_ALIASES

Other options I like to have set:

# turn on spelling correction
setopt correct
# don't save duplicates in command history
setopt histignoredups
# don't allow accidental file over-writes
setopt noclobber

Pushd and popd are also pretty handy.

# Always pushd when changing directory
setopt auto_pushd
# Have pushd with no arguments act like `pushd $HOME'.
setopt PUSHD_TO_HOME

Annoyingly the home and end keys don't by default work on zsh like they do on other shells, but you can fix this.

# Make home and end keys work.
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line

theotherreceive

Posted 2009-07-15T14:47:42.683

Reputation: 798

2I tend to find spelling correction more annoying than actually helpful. – Frew Schmidt – 2009-07-16T01:33:50.810

@Frew heh, you're not the first person to say that to me. One of the nicer features of correction is that it'll put the corrected version into the history, so you won't accidentally run the same thing again. – theotherreceive – 2009-07-16T02:39:25.780

5

Enable auto-complete when using scp onto a remote server.

The right prompt for displaying additional info.

Rob Wells

Posted 2009-07-15T14:47:42.683

Reputation: 400

Indeed! I have a very complex but short prompt. Hostname [current line] <error no> % – Frew Schmidt – 2009-07-15T15:05:25.837