Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

closed as not constructive by 8088, Sathya Aug 31 '11 at 4:06

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
i didn't know about that feature, i'm gonna have to try that one! –  Roy Rico Aug 7 '09 at 17:11
1  
Which option is that directory autocomplete? –  hometoast Oct 14 '09 at 14:24

6 Answers 6

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
share|improve this answer
3  
Ok, that rules. –  hometoast Oct 14 '09 at 14:26

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
share|improve this answer
2  
Love the ^Z ^Z trick, thanks! Originally was looking superuser.com/questions/378018 for a solution to bind it to single key but ^Z ^Z makes so much more sense –  Evgeniy Dolzhenko Jan 13 '12 at 14:41

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
share|improve this answer
1  
Yes, 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 Jul 15 '09 at 14:56
    
I believe bash has this capability as well. –  Keith Thompson Jan 24 at 23:54

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.

share|improve this answer
    
I also like G for grep. –  Frew Oct 21 '09 at 19:32

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
share|improve this answer
1  
I tend to find spelling correction more annoying than actually helpful. –  Frew Jul 16 '09 at 1:33
    
@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 Jul 16 '09 at 2:39

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

The right prompt for displaying additional info.

share|improve this answer
    
Indeed! I have a very complex but short prompt. Hostname [current line] <error no> % –  Frew Jul 15 '09 at 15:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.