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!
-
3i didn't know about that feature, i'm gonna have to try that one!– Roy RicoAug 7, 2009 at 17:11
-
1Which option is that directory autocomplete?– hometoastOct 14, 2009 at 14:24
6 Answers
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
-
8
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 calledfoo
in subdirectories recursively. (And***/foo
also follows symlinks.) Two characters instead of a longfind
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*(*)
(likefoo*
, 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
orpushd
depending on how you configured it). Thecd
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
andpreexec
hook functions: I usepreexec
to set my terminal's title to include the running command, andprecmd
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:
- On an empty command line runs
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
-
3Love 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 Jan 13, 2012 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
-
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. Jul 15, 2009 at 14:56
-
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.
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
-
2I tend to find spelling correction more annoying than actually helpful. Jul 16, 2009 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. Jul 16, 2009 at 2:39
Enable auto-complete when using scp
onto a remote server.
The right prompt for displaying additional info.
-
Indeed! I have a very complex but short prompt. Hostname [current line] <error no> % Jul 15, 2009 at 15:05