parsed.org

Tips by tag: zsh

Alternative to 'which' by cygnus on Jan 19, 2005 10:35 PM

Instead of running:

$ ls -lh `which zsh`

use this:

$ ls -l =zsh
commandswhichzsh
Auto-CD by cygnus on Jan 19, 2005 10:26 PM

To have zsh automatically cd to a directory without requiring you to type cd, set this option:

setopt auto_cd

So, if you have a directory foo in the current directory (and no command foo exists in your path), typing simply foo will be equivalent to cd foo.

auto_cdcommandszsh
Autoloading and Reloading Functions by cygnus on Jan 19, 2005 04:09 PM

To have zsh autoload functions, you can place them in a directory and have zsh search the path when the functions are called. For example, consider a function foo defined in ~/functions/foo. In your ~/.zshrc add:

FPATH=~/functions:$FPATH
autoload foo

Then, whenever you call foo, zsh will attempt to read ~/functions/foo for the function definition. To reload a function after you have changed its source file, run:

unfunction foo
autoload foo

The next invocation of the function will cause a reload of ~/functions/foo.

autoloadfunctionspathreloadzsh
Environment Variable Editing by cygnus on Jan 19, 2005 10:39 PM

You can edit an environment variable in-place by running vared:

$ vared PATH

It will give you a readline-style buffer to edit the variable's contents and will save them back to the environment after you're done.

environmentpathreadlinevaredzsh
File Globbing by cygnus on Jan 19, 2005 04:14 PM

Some examples of useful file globbing supported by Zsh (if setopt extendedglob has been run):

$ ls foo<1-100>
  (Lists files 'foo1', 'foo2', ..., 'foo100')
$ ls foo<-50>
  (Lists files up to 'foo50')
$ ls *.c~foo.c
  (Lists all *.c files except foo.c)
$ ls *.php~foo*(.)
  (Lists all plain *.php files except those starting with 'foo')
extrasglobzsh
Man Page Completion by cygnus on Dec 31, 2005 04:13 PM

You can complete man page names as well as file names. Put these lines in your ~/.zshrc:

setopt SH_WORD_SPLIT
function man_var () {
   man_pages=( /usr/share/man*/*(N:t:r:r) )
   compctl -k man_pages man
   reply=( $man_pages )
}
compctl -K man_var man; man_pages=()

Now type man foo and the hit TAB to get a list of all man pages beginning with foo.

(Note: this tip taken essentially verbatim [after testing and fixing] from http://www.unixtips.org/index.php3?catList=30.)

autocompleteextrasmanpageszsh
Pipe Aliases by cygnus on Dec 31, 2005 04:13 PM

If you pipe the output of command into command like less or grep often, a global alias can save you a number of characters to type:

$ alias -g G='| grep'
$ alias -g L='| less'

so that this command:

$ ls * | grep foo | less

becomes:

$ ls * G foo L

(Note: This tip taken essentially verbatim from http://www.unixtips.org/index.php3?catList=30.)

aliaspipezsh
Process Substitution by cygnus on Jan 20, 2005 10:16 AM

Zsh can run a command and let you do things with a temporary file with the resulting output:

$ emacs -nw =(ps aux)

This will create a temporary file with the output of ps aux and let you edit it in Emacs. Or:

$ diff =(ls) =(ls -F)

Will run diff on the output of the two commands.

diffemacsprocesssubstitutionzsh

If you use pushd and popd to maintain your directory stack, then ~N (where ~0 is the top of the stack) will expand to the appropriate directory in the stack. For example:

cygnus:~$ pushd usr
~/usr ~
cygnus:~/usr$ pushd /etc
/etc ~/usr ~
cygnus:/etc$ pushd /usr/lib
/usr/lib /etc ~/usr ~
cygnus:/usr/lib$ cd ~2
cygnus:~/usr$

Furthermore, you can make cd use pushd and popd functionality implicitly:

~> DIRSTACKSIZE=8
~> setopt autopushd pushdminus pushdsilent pushdtohome
~> alias dh='dirs -v'
~> cd /tmp
/tmp> cd /usr
/usr> cd bin
/usr/bin> cd ../pub
/usr/pub> dh
0       /usr/pub
1       /usr/bin
2       /usr
3       /tmp
4       ~
/usr/pub> cd -3
/tmp> dh
0       /tmp
1       /usr/pub
2       /usr/bin
3       /usr
4       ~
/tmp> ls =2/df
/usr/bin/df
/tmp> cd -4
~>

(Note: The text above is taken essentially verbatim from http://www.b2pi.com/zsh/Intro/intro_6.html.)

directoryextrasstackzsh
Variable Indirection by xinu on Jan 31, 2006 10:06 AM

Sometimes you need to iterate over variable names and access their values. In shell script, you would do something like this to get the values of FOO and BAR:

$ FOO=apple
$ BAR=orange
$ VARS="FOO BAR"
$ for v in $VARS ; do echo ${!v} ; done

The above works in bash. Use this in Zsh:

$ for v in $VARS ; do echo ${(P)v} ; done

(The 'P' flag on ${v} causes a further variable lookup before ${v} is evaluated.)

Thanks to Cliff for the tip!

bashcommandsenvironmentevaluationiterationloopshellzsh
RSS