home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / ap / zsh / zsh-2.4 / zsh-2 / zsh-2.4.306 / doc / intro.ms next >
Text File  |  1994-03-12  |  79KB  |  2,667 lines

  1. .nr PI 0
  2. .de Ds
  3. .LD
  4. .ft C
  5. ..
  6. .de De
  7. .DE
  8. .ft R
  9. ..
  10. .de Sh
  11. .SH
  12. \\$1
  13. .XS
  14. \\$1
  15. .XE
  16. ..
  17. .TL
  18. An Introduction to the Z Shell
  19. .AU
  20. Paul Falstad
  21. pf@z-code.com
  22. .AU
  23. Bas de Bakker
  24. bas@phys.uva.nl
  25. .PP
  26. .Sh "Introduction"
  27. .PP
  28. \fBzsh\fP is a shell designed for interactive use, although it is also
  29. a powerful scripting language.  Many of the useful features of bash,
  30. ksh, and tcsh were incorporated into \fBzsh\fP; many original features were
  31. added.  This document details some of the unique features of \fBzsh\fP.  It
  32. assumes basic knowledge of the standard UNIX shells; the intent is to
  33. show a reader already familiar with one of the other major shells what
  34. makes \fBzsh\fP more useful or more powerful.  This document is not at all
  35. comprehensive; read the manual entry for a description of the shell
  36. that is complete and concise, although somewhat overwhelming and
  37. devoid of examples.
  38. .PP
  39. The text will frequently mention options that you can set to change
  40. the behaviour of \fBzsh\fP.  You can set these options with the
  41. command
  42. .Ds
  43. % setopt \fIoptionname\fC
  44. .De
  45. and unset them again with
  46. .Ds
  47. % unsetopt \fIoptionname\fC
  48. .De
  49. Case is ignored in option names, as are embedded underscores.
  50. .Sh "Filename Generation"
  51. .PP
  52. Otherwise known as \fIglobbing\fP, filename generation
  53. is quite extensive in \fBzsh\fP.  Of course, it has all the
  54. basics:
  55. .Ds
  56. % ls
  57. Makefile   file.pro   foo.o      main.o     q.c        run234     stuff
  58. bar.o      foo        link       morestuff  run123     run240     sub
  59. file.h     foo.c      main.h     pipe       run2       run303
  60. % ls *.c
  61. foo.c  q.c
  62. % ls *.[co]
  63. bar.o   foo.c   foo.o   main.o  q.c
  64. % ls foo.?
  65. foo.c  foo.o
  66. % ls *.[^c]
  67. bar.o   file.h  foo.o   main.h  main.o
  68. % ls *.[^oh]
  69. foo.c  q.c
  70. .De
  71. Also, if the \fIEXTENDEDGLOB\fP option is set,
  72. some new features are activated.
  73. For example, the \fC^\fP character negates the pattern following it:
  74. .Ds
  75. % setopt extendedglob
  76. % ls -d ^*.c
  77. Makefile   file.pro   link       morestuff  run2       run303
  78. bar.o      foo        main.h     pipe       run234     stuff
  79. file.h     foo.o      main.o     run123     run240     sub
  80. % ls -d ^*.*
  81. Makefile   link       pipe       run2       run240     stuff
  82. foo        morestuff  run123     run234     run303     sub
  83. % ls -d ^Makefile
  84. bar.o      foo        link       morestuff  run123     run240     sub
  85. file.h     foo.c      main.h     pipe       run2       run303
  86. file.pro   foo.o      main.o     q.c        run234     stuff
  87. % ls -d *.^c
  88. \&.rhosts   bar.o     file.h    file.pro  foo.o     main.h    main.o
  89. .De
  90. An expression of the form
  91. \fC<\fIx\fR\-\fIy\fC>\fR
  92. matches a range of integers:
  93. .Ds
  94. % ls run<200-300>
  95. run234  run240
  96. % ls run<300-400>
  97. run303
  98. % ls run<-200>
  99. run123  run2
  100. % ls run<300->
  101. run303
  102. % ls run<>
  103. run123  run2    run234  run240  run303
  104. .De
  105. The \fINUMERICGLOBSORT\fP option will sort files with numbers
  106. according to the number.  This will not work with \fCls\fP as it
  107. resorts its arguments:
  108. .Ds
  109. % setopt numericglobsort
  110. % echo run<>
  111. run2 run123 run234 run240 run303
  112. .De
  113. Grouping is possible:
  114. .Ds
  115. % ls (foo|bar).*
  116. bar.o  foo.c  foo.o
  117. % ls *.(c|o|pro)
  118. bar.o     file.pro  foo.c     foo.o     main.o    q.c
  119. .De
  120. Also, the string \fC**/\fP forces a recursive search of
  121. subdirectories:
  122. .Ds
  123. % ls -R
  124. Makefile   file.pro   foo.o      main.o     q.c        run234     stuff
  125. bar.o      foo        link       morestuff  run123     run240     sub
  126. file.h     foo.c      main.h     pipe       run2       run303
  127.  
  128. morestuff:
  129.  
  130. stuff:
  131. file  xxx   yyy
  132.  
  133. stuff/xxx:
  134. foobar
  135.  
  136. stuff/yyy:
  137. frobar
  138. % ls **/*bar
  139. stuff/xxx/foobar  stuff/yyy/frobar
  140. % ls **/f*
  141. file.h            foo               foo.o             stuff/xxx/foobar
  142. file.pro          foo.c             stuff/file        stuff/yyy/frobar
  143. % ls *bar*
  144. bar.o
  145. % ls **/*bar*
  146. bar.o             stuff/xxx/foobar  stuff/yyy/frobar
  147. % ls stuff/**/*bar*
  148. stuff/xxx/foobar  stuff/yyy/frobar
  149. .De
  150. .PP
  151. It is possible to exclude certain files from the patterns using
  152. the ~ character.  A pattern of the form \fC*.c~bar.c\fP lists all
  153. files matching \fC*.c\fP, except for the file \fCbar.c\fP.
  154. .Ds
  155. % ls *.c
  156. foo.c    foob.c    bar.c
  157. % ls *.c~bar.c
  158. foo.c    foob.c
  159. % ls *.c~f*
  160. bar.c
  161. .De
  162. .PP
  163. One can add a number of \fIqualifiers\fP to the end of
  164. any of these patterns, to restrict matches to certain
  165. file types.  A qualified pattern is of the form
  166. .DS
  167. \fIpattern\fC(\fR...\fC)\fR
  168. .De
  169. with single-character qualifiers inside the parentheses.
  170. .Ds
  171. % alias l='ls -dF'
  172. % l *
  173. Makefile    foo*        main.h      q.c         run240
  174. bar.o       foo.c       main.o      run123      run303
  175. file.h      foo.o       morestuff/  run2        stuff/
  176. file.pro    link@       pipe        run234      sub
  177. % l *(/)
  178. morestuff/  stuff/
  179. % l *(@)
  180. link@
  181. % l *(*)
  182. foo*        link@       morestuff/  stuff/
  183. % l *(x)
  184. foo*        link@       morestuff/  stuff/
  185. % l *(X)
  186. foo*        link@       morestuff/  stuff/
  187. % l *(R)
  188. bar.o       foo*        link@       morestuff/  run123      run240
  189. file.h      foo.c       main.h      pipe        run2        run303
  190. file.pro    foo.o       main.o      q.c         run234      stuff/
  191. .De
  192. Note that \fC*(x)\fP and \fC*(*)\fP both match executables.
  193. \fC*(X)\fP matches files executable by others, as opposed to
  194. \fC*(x)\fP, which matches files executable by the owner.
  195. \fC*(R)\fP and \fC*(r)\fP match readable files;
  196. \fC*(W)\fP and \fC*(w)\fP, which checks for writable files.
  197. \fC*(W)\fP is especially important, since it checks for world-writable
  198. files:
  199. .Ds
  200. % l *(w)
  201. bar.o       foo*        link@       morestuff/  run123      run240
  202. file.h      foo.c       main.h      pipe        run2        run303
  203. file.pro    foo.o       main.o      q.c         run234      stuff/
  204. % l *(W)
  205. link@   run240
  206. % l -l link run240
  207. lrwxrwxrwx  1 pfalstad       10 May 23 18:12 link -> /usr/bin/
  208. -rw-rw-rw-  1 pfalstad        0 May 23 18:12 run240
  209. .De
  210. If you want to have all the files of a certain type as well as all
  211. symbolic links pointing to files of that type, prefix the qualifier
  212. with a \fC-\fP:
  213. .Ds
  214. % l *(-/)
  215. link@       morestuff/  stuff/
  216. .De
  217. You can filter out the symbolic links with the \fC^\fP character:
  218. .Ds
  219. % l *(W^@)
  220. run240
  221. % l *(x)
  222. foo*        link@       morestuff/  stuff/
  223. % l *(x^@/)
  224. foo*
  225. .De
  226. To find all plain files, you can use \fC.\fP:
  227. .Ds
  228. % l *(.)
  229. Makefile  file.h    foo*      foo.o     main.o    run123    run234    run303
  230. bar.o     file.pro  foo.c     main.h    q.c       run2      run240    sub
  231. % l *(^.)
  232. link@       morestuff/  pipe        stuff/
  233. % l s*(.)
  234. stuff/   sub
  235. % l *(p)
  236. pipe
  237. % l -l *(p)
  238. prw-r--r--  1 pfalstad        0 May 23 18:12 pipe
  239. .De
  240. \fC*(U)\fP matches all files owned by you.
  241. To search for all files not owned by you, use \fC*(^U)\fP:
  242. .Ds
  243. % l -l *(^U)
  244. -rw-------  1 subbarao       29 May 23 18:13 sub
  245. .De
  246. This searches for setuid files:
  247. .Ds
  248. % l -l *(s)
  249. -rwsr-xr-x  1 pfalstad       16 May 23 18:12 foo*
  250. .De
  251. This checks for a certain user's files:
  252. .Ds
  253. % l -l *(u[subbarao])
  254. -rw-------  1 subbarao       29 May 23 18:13 sub
  255. .De
  256. .Sh "Startup Files"
  257. .PP
  258. There are five startup files that \fBzsh\fP will read commands from:
  259. .Ds
  260. $ZDOTDIR/.zshenv
  261. $ZDOTDIR/.zprofile
  262. $ZDOTDIR/.zshrc
  263. $ZDOTDIR/.zlogin
  264. $ZDOTDIR/.zlogout
  265. .De
  266. If \fBZDOTDIR\fP is not set, then the value of \fBHOME\fP is used;
  267. this is the usual case.
  268. .\".KE    <--- missing .KS or .KF above
  269. .PP
  270. \&\fC.zshenv\fP is sourced on all invocations of the shell,
  271. unless the \fC-f\fP option is set.  It should contain commands to set
  272. the command search path, plus other important environment
  273. variables.
  274. \&\fC.zshenv\fP should not contain commands that produce output
  275. or assume the shell is attached to a tty.
  276. .PP
  277. \&\fC.zshrc\fP is sourced in interactive shells.  It should contain
  278. commands to set up aliases, functions, options, key bindings, etc.
  279. .PP
  280. \&\fC.zlogin\fP is sourced in login shells.  It should contain
  281. commands that should be executed only in login shells.
  282. \&\fC.zlogout\fP is sourced when login shells exit.
  283. \&\fC.zprofile\fP is similar to \fC.zlogin\fP, except that it is sourced before
  284. \&\fC.zshrc\fP.
  285. \&\fC.zprofile\fP is meant as an alternative to \fC.zlogin\fP for
  286. ksh fans;
  287. the two are not intended to be used together, although this
  288. could certainly be done if desired.
  289. \&\fC.zlogin\fP is not the place for alias definitions, options, environment
  290. variable settings, etc.;
  291. as a general rule, it should not change the shell environment
  292. at all.  Rather, it should be used to set the terminal type
  293. and run a series of external commands (\fCfortune\fP, \fCmsgs\fP, etc).
  294. .Sh "Shell Functions"
  295. .PP
  296. \fBzsh\fP also allows you to create your own commands by defining shell
  297. functions.  For example:
  298. .Ds
  299. % yp () {
  300. >       ypmatch $1 passwd.byname
  301. > }
  302. % yp pfalstad
  303. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  304. .De
  305. This function looks up a user in the NIS password map.
  306. The \fC$1\fP expands to the first argument to \fCyp\fP.
  307. The function could have been equivalently defined in one of the following
  308. ways:
  309. .Ds
  310. % function yp {
  311. >       ypmatch $1 passwd.byname
  312. > }
  313. % function yp () {
  314. >       ypmatch $1 passwd.byname
  315. > }
  316. % function yp () ypmatch $1 passwd.byname
  317. .De
  318. Note that aliases are expanded when the function definition is
  319. parsed, not when the function is executed.  For example:
  320. .Ds
  321. % alias ypmatch=echo
  322. % yp pfalstad
  323. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  324. .De
  325. Since the alias was defined after the function was parsed, it has
  326. no effect on the function's execution.
  327. However, if we define the function again with the alias in place:
  328. .Ds
  329. % function yp () { ypmatch $1 passwd.byname }
  330. % yp pfalstad
  331. pfalstad passwd.byname
  332. .De
  333. it is parsed with the new alias definition in place.
  334. Therefore, in general you must define aliases before functions.
  335. .\".KE    <--- missing .KS or .KF above
  336. .PP
  337. We can make the function take multiple arguments:
  338. .Ds
  339. % unalias ypmatch
  340. % yp () {
  341. >       for i
  342. >       do ypmatch $i passwd.byname
  343. >       done
  344. > }
  345. % yp pfalstad subbarao sukthnkr
  346. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  347. subbarao:*:3338:35:Kartik Subbarao:/u/subbarao:/usr/princeton/bin/zsh
  348. sukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  349. .De
  350. The \fCfor i\fP loops through each of the function's arguments,
  351. setting \fCi\fP equal to each of them in turn.
  352. We can also make the function do something sensible
  353. if no arguments are given:
  354. .Ds
  355. % yp () {
  356. >       if (( $# == 0 ))
  357. >       then echo usage: yp name ...; fi
  358. >       for i; do ypmatch $i passwd.byname; done
  359. > }
  360. % yp
  361. usage: yp name ...
  362. % yp pfalstad sukthnkr
  363. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  364. sukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  365. .De
  366. \fC$#\fP is the number of arguments supplied to the function.
  367. If it is equal to zero, we print a usage message; otherwise,
  368. we loop through the arguments, and \fCypmatch\fP all of them.
  369. .\".KE    <--- missing .KS or .KF above
  370. .PP
  371. Here's a function that selects a random line from a file:
  372. .Ds
  373. % randline () {
  374. >       integer z=$(wc -l <$1)
  375. >       sed -n $[RANDOM % z + 1]p $1
  376. > }
  377. % randline /etc/motd
  378. PHOENIX WILL BE DOWN briefly Friday morning, 5/24/91 from 8 AM to
  379. % randline /etc/motd
  380. SunOS Release 4.1.1 (PHOENIX) #19: Tue May 14 19:03:15 EDT 1991
  381. % randline /etc/motd
  382. | Please use the "msgs" command to read announcements.  Refer to the   |
  383. % echo $z
  384.  
  385. %
  386. .De
  387. \fCrandline\fP has a local variable, \fCz\fP, that holds the number of
  388. lines in the file.  \fC$[RANDOM % z + 1]\fP expands to a random number
  389. between 1 and \fCz\fP.  An expression of the form \fC$[\fR...\fC]\fR
  390. expands to the value of the arithmetic expression within the brackets,
  391. and the \fBRANDOM\fP variable returns a random number each time it
  392. is referenced.  \fC%\fP is the modulus operator, as in C.
  393. Therefore, \fCsed -n $[RANDOM%z+1]p\fP picks a random line from its
  394. input, from 1 to \fCz\fP.
  395. .PP
  396. Function definitions can be viewed with the \fCfunctions\fP builtin:
  397. .Ds
  398. % functions randline
  399. randline () {
  400.         integer z=$(wc -l <$1)
  401.         sed -n $[RANDOM % z + 1]p $1
  402.  
  403. }
  404. % functions
  405. yp () {
  406.         if let $# == 0 
  407.         
  408.         then
  409.                 echo usage: yp name ...
  410.         
  411.         fi
  412.         for i
  413.         do
  414.                 ypmatch $i passwd.byname
  415.         
  416.                 done
  417.  
  418. }
  419. randline () {
  420.         integer z=$(wc -l <$1)
  421.         sed -n $[RANDOM % z + 1]p $1
  422.  
  423. }
  424. .De
  425. Here's another one:
  426. .Ds
  427. % cx () { chmod +x $* }
  428. % ls -l foo bar
  429. -rw-r--r--  1 pfalstad       29 May 24 04:38 bar
  430. -rw-r--r--  1 pfalstad       29 May 24 04:38 foo
  431. % cx foo bar
  432. % ls -l foo bar
  433. -rwxr-xr-x  1 pfalstad       29 May 24 04:38 bar
  434. -rwxr-xr-x  1 pfalstad       29 May 24 04:38 foo
  435. .De
  436. Note that this could also have been implemented as an alias:
  437. .Ds
  438. % chmod 644 foo bar
  439. % alias cx='chmod +x'
  440. % cx foo bar
  441. % ls -l foo bar
  442. -rwxr-xr-x  1 pfalstad       29 May 24 04:38 bar
  443. -rwxr-xr-x  1 pfalstad       29 May 24 04:38 foo
  444. .De
  445. .PP
  446. Instead of defining a lot of functions in your \fC.zshrc\fP,
  447. all of which you may not use,
  448. it is often better to use the \fCautoload\fP builtin.
  449. The idea is, you create a directory where function
  450. definitions are stored, declare the names in
  451. your \fC.zshrc\fP, and tell the shell where to look for them.
  452. Whenever you reference a function, the shell
  453. will automatically load it into memory.
  454. .Ds
  455. % mkdir /tmp/funs
  456. % cat >/tmp/funs/yp
  457. ypmatch $1 passwd.byname
  458. ^D
  459. % cat >/tmp/funs/cx
  460. chmod +x $*
  461. ^D
  462. % FPATH=/tmp/funs
  463. % autoload cx yp
  464. % functions cx yp
  465. undefined cx ()
  466. undefined yp ()
  467. % chmod 755 /tmp/funs/{cx,yp}
  468. % yp egsirer
  469. egsirer:*:3214:35:Emin Gun Sirer:/u/egsirer:/bin/sh
  470. % functions yp
  471. yp () {
  472.         ypmatch $1 passwd.byname
  473. }
  474. .De
  475. This idea has other benefits.  By adding a \fC#!\fP header
  476. to the files, you can make them double as shell scripts.
  477. (Although it is faster to use them as functions, since a
  478. separate process is not created.)
  479. .Ds
  480. % ed /tmp/funs/yp
  481. 25
  482. i
  483. #! /usr/local/bin/zsh
  484. .
  485. w
  486. 42
  487. q
  488. % </tmp/funs/yp
  489. #! /usr/local/bin/zsh
  490. ypmatch $1 passwd.byname
  491. % /tmp/funs/yp sukthnkr
  492. sukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  493. .De
  494. Now other people, who may not use \fBzsh\fP, or who don't want to
  495. copy all of your \fC.zshrc\fP, may use these functions as shell
  496. scripts.
  497. .Sh "Directories"
  498. .PP
  499. One nice feature of \fBzsh\fP is the way it prints directories.
  500. For example, if we set the prompt like this:
  501. .Ds
  502. phoenix% PROMPT='%~> '
  503. ~> cd src
  504. ~/src>
  505. .De
  506. the shell will print the current directory in the prompt,
  507. using the \fC~\fP character.
  508. However, \fBzsh\fP is smarter than most other shells in this respect:
  509. .Ds
  510. ~/src> cd ~subbarao
  511. ~subbarao> cd ~maruchck
  512. ~maruchck> cd lib
  513. ~maruchck/lib> cd fun
  514. ~maruchck/lib/fun> foo=/usr/princeton/common/src
  515. ~maruchck/lib/fun> cd ~foo
  516. ~foo> cd ..
  517. /usr/princeton/common> cd src
  518. ~foo> cd news/nntp
  519. ~foo/news/nntp> cd inews
  520. ~foo/news/nntp/inews>
  521. .De
  522. Note that \fBzsh\fP prints \fIother\fP users' directories 
  523. in the form \fC~user\fP.  Also note that you can
  524. set a parameter and use it as a directory name;
  525. \fBzsh\fP will act as if \fCfoo\fP is a user
  526. with the login directory \fC/usr/princeton/common/src\fP.
  527. This is convenient, especially if you're sick of seeing
  528. prompts like this:
  529. .Ds
  530. phoenix:/usr/princeton/common/src/X.V11R4/contrib/clients/xv/docs>
  531. .De
  532. If you get stuck in this position, you can give the current
  533. directory a short name, like this:
  534. .Ds
  535. /usr/princeton/common/src/news/nntp/inews> inews=$PWD
  536. /usr/princeton/common/src/news/nntp/inews> echo ~inews
  537. /usr/princeton/common/src/news/nntp/inews
  538. ~inews>
  539. .De
  540. When you reference a directory in the form \fC~inews\fP,
  541. the shell assumes that you want the directory displayed
  542. in this form; thus simply typing \fCecho ~inews\fP or
  543. \fCcd ~inews\fP causes the prompt to be shortened.
  544. You can define a shell function for this purpose:
  545. .Ds
  546. ~inews> namedir () { $1=$PWD ;  : ~$1 }
  547. ~inews> cd /usr/princeton/bin
  548. /usr/princeton/bin> namedir pbin
  549. ~pbin> cd /var/spool/mail
  550. /var/spool/mail> namedir spool
  551. ~spool> cd .msgs
  552. ~spool/.msgs>
  553. .De
  554. You may want to add this one-line function to your \fC.zshrc\fP.
  555.  
  556. \fBzsh\fP can also put the current directory in your title bar,
  557. if you are using a windowing system.
  558. One way to do this is with the \fCchpwd\fP function, which is
  559. automatically executed by the shell whenever you change
  560. directory.  If you are using xterm, this will work:
  561. .Ds
  562. chpwd () { print -Pn '^[]2;%~^G' }
  563. .De
  564. The \fC-P\fP option tells \fCprint\fP to treat its arguments like a prompt
  565. string; otherwise the \fC%~\fP would not be expanded.
  566. The \fC-n\fP option suppresses the terminating newline, as with \fCecho\fP.
  567. .PP
  568. If you are using an IRIS \fCwsh\fP, do this:
  569. .Ds
  570. chpwd () { print -Pn '\e2201.y%~\e234' }
  571. .De
  572. The \fCprint -D\fP command has other uses.  For example, to
  573. print the current directory to standard output in short form,
  574. you can do this:
  575. .Ds
  576. % print -D $PWD
  577. ~subbarao/src
  578. .De
  579. and to print each component of the path in short form:
  580. .Ds
  581. % print -D $path
  582. /bin /usr/bin ~locbin ~locbin/X11 ~/bin
  583. .De
  584. .Sh "Directory Stacks"
  585. .PP
  586. If you use csh, you may know about directory stacks.
  587. The \fCpushd\fP command puts the current directory on the
  588. stack, and changes to a new directory; the \fCpopd\fP command
  589. pops a directory off the stack and changes to it.
  590. .Ds
  591. phoenix% cd 
  592. phoenix% PROMPT='Z %~> '
  593. Z ~> pushd /tmp
  594. /tmp ~
  595. Z /tmp> pushd /usr/etc
  596. /usr/etc /tmp ~
  597. Z /usr/etc> pushd /usr/bin
  598. /usr/bin /usr/etc /tmp ~
  599. Z /usr/bin> popd
  600. /usr/etc /tmp ~
  601. Z /usr/etc> popd
  602. /tmp ~
  603. Z /tmp> pushd /etc
  604. /etc /tmp ~
  605. Z /etc> popd 
  606. /tmp ~
  607. .De
  608. \fBzsh\fP's directory stack commands work similarly.  One
  609. difference is the way \fCpushd\fP is handled if no arguments
  610. are given.  As in csh, this exchanges the top two elements
  611. of the directory stack:
  612. .Ds
  613. Z /tmp> dirs
  614. /tmp ~
  615. Z /tmp> pushd
  616. ~ /tmp
  617. .De
  618. unless the stack only has one entry:
  619. .Ds
  620. Z ~> popd
  621. /tmp
  622. Z /tmp> dirs
  623. /tmp
  624. Z /tmp> pushd
  625. ~ /tmp
  626. Z ~>
  627. .De
  628. or unless the \fIPUSHDTOHOME\fP option is set:
  629. .Ds
  630. Z ~> setopt pushdtohome
  631. Z ~> pushd
  632. ~ ~ /tmp
  633. .De
  634. .PP
  635. As an alternative to using directory stacks in this manner,
  636. we can get something like a \fIdirectory history\fP
  637. by setting a few more options and parameters:
  638. .Ds
  639. ~> DIRSTACKSIZE=8
  640. ~> setopt autopushd pushdminus pushdsilent pushdtohome
  641. ~> alias dh='dirs -v'
  642. ~> cd /tmp
  643. /tmp> cd /usr
  644. /usr> cd bin
  645. /usr/bin> cd ../pub
  646. /usr/pub> dh
  647. 0       /usr/pub
  648. 1       /usr/bin
  649. 2       /usr
  650. 3       /tmp
  651. 4       ~
  652. /usr/pub> cd -3
  653. /tmp> dh
  654. 0       /tmp
  655. 1       /usr/pub
  656. 2       /usr/bin
  657. 3       /usr
  658. 4       ~
  659. /tmp> ls =2/df
  660. /usr/bin/df
  661. /tmp> cd -4
  662. ~>
  663. .De
  664. Note that \fC=2\fP expanded to the second directory in the
  665. history list, and that \fCcd -3\fP recalled the third
  666. directory in the list.
  667. .PP
  668. You may be wondering what all those options do.
  669. \fIAUTOPUSHD\fP made \fCcd\fP act like \fCpushd\fP.
  670. (\fCalias cd=pushd\fP is not sufficient, for various reasons.)
  671. \fIPUSHDMINUS\fP swapped the meaning of \fCcd +1\fP and
  672. \fCcd -1\fP; we want them to mean the opposite of what they mean in csh,
  673. because it makes more sense in this scheme, and it's easier to type:
  674. .Ds
  675. ~> dh
  676. 0       ~
  677. 1       /tmp
  678. 2       /usr/pub
  679. 3       /usr/bin
  680. 4       /usr
  681. ~> unsetopt pushdminus
  682. ~> cd +1
  683. /tmp> dh
  684. 0       /tmp
  685. 1       ~
  686. 2       /usr/pub
  687. 3       /usr/bin
  688. 4       /usr
  689. /tmp> cd +2
  690. /usr/pub>
  691. .De
  692. \fIPUSHDSILENT\fP keeps the shell from printing
  693. the directory stack each time we do a \fCcd\fP,
  694. and \fIPUSHDTOHOME\fP we mentioned earlier:
  695. .Ds
  696. /usr/pub> unsetopt pushdsilent
  697. /usr/pub> cd /etc
  698. /etc /usr/pub /tmp ~ /usr/bin /usr
  699. /etc> cd
  700. ~ /etc /usr/pub /tmp ~ /usr/bin /usr
  701. ~> unsetopt pushdtohome
  702. ~> cd
  703. /etc ~ /usr/pub /tmp ~ /usr/bin /usr
  704. /etc>
  705. .De
  706. \fBDIRSTACKSIZE\fP keeps the directory stack
  707. from getting too large, much like \fIHISTSIZE\fP:
  708. .Ds
  709. /etc> setopt pushdsilent
  710. /etc> cd /
  711. /> cd /
  712. /> cd /
  713. /> cd /
  714. /> cd /
  715. /> cd /
  716. /> cd /
  717. /> cd /
  718. /> dh
  719. 0       /
  720. 1       /
  721. 2       /
  722. 3       /
  723. 4       /
  724. 5       /
  725. 6       /
  726. 7       /
  727. .De
  728. .Sh "Command/Process Substitution"
  729. .PP
  730. Command substitution in \fBzsh\fP can take two forms.
  731. In the traditional form, a command enclosed in
  732. backquotes (\fC`\fP...\fC`\fP) is replaced on the command line with its output.
  733. This is the form used by the older shells.
  734. Newer shells (like \fBzsh\fP) also provide another form,
  735. \fC$(\fR...\fC)\fR.  This form is much easier to nest.
  736. .Ds
  737. % ls -l `echo /vmunix`
  738. -rwxr-xr-x  1 root      1209702 May 14 19:04 /vmunix
  739. % ls -l $(echo /vmunix)
  740. -rwxr-xr-x  1 root      1209702 May 14 19:04 /vmunix
  741. % who | grep mad
  742. subbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  743. pfalstad ttyu1   May 23 16:25   (mad55sx14.Prince)
  744. subbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  745. pfalstad ttyv3   May 23 16:25   (mad55sx14.Prince)
  746. % who | grep mad | awk '{print $2}'
  747. ttyt7
  748. ttyu1
  749. ttyu6
  750. ttyv3
  751. % cd /dev; ls -l $(who |
  752. > grep $(echo mad) |
  753. > awk '{ print $2 }')
  754. crwx-w----  1 subbarao  20,  71 May 23 18:35 ttyt7
  755. crw--w----  1 pfalstad  20,  81 May 23 18:42 ttyu1
  756. crwx-w----  1 subbarao  20,  86 May 23 18:38 ttyu6
  757. crw--w----  1 pfalstad  20,  99 May 23 18:41 ttyv3
  758. .De
  759. Many common uses of command substitution, however, are
  760. superseded by other mechanisms of \fBzsh\fP:
  761. .Ds
  762. % ls -l `tty`
  763. crw-rw-rw-  1 root      20,  28 May 23 18:35 /dev/ttyqc
  764. % ls -l $TTY
  765. crw-rw-rw-  1 root      20,  28 May 23 18:35 /dev/ttyqc
  766. % ls -l `which rn`
  767. -rwxr-xr-x  1 root       172032 Mar  6 18:40 /usr/princeton/bin/rn
  768. % ls -l =rn
  769. -rwxr-xr-x  1 root       172032 Mar  6 18:40 /usr/princeton/bin/rn
  770. .De
  771. A command name with a \fC=\fP prepended is replaced with its full
  772. pathname.  This can be very convenient.  If it's not convenient
  773. for you, you can turn it off:
  774. .Ds
  775. % ls
  776. =foo    =bar
  777. % ls =foo =bar
  778. zsh: foo not found
  779. % setopt noequals
  780. % ls =foo =bar
  781. =foo    =bar
  782. .De
  783. .PP
  784. Another nice feature is process substitution:
  785. .Ds
  786. % who | fgrep -f =(print -l root lemke shgchan subbarao)
  787. root     console May 19 10:41
  788. lemke    ttyq0   May 22 10:05   (narnia:0.0)
  789. lemke    ttyr7   May 22 10:05   (narnia:0.0)
  790. lemke    ttyrd   May 22 10:05   (narnia:0.0)
  791. shgchan  ttys1   May 23 16:52   (gaudi.Princeton.)
  792. subbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  793. subbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  794. shgchan  ttyvb   May 23 16:51   (gaudi.Princeton.)
  795. .De
  796. A command of the form \fC=(\fR...\fC)\fR is replaced with the name of a \fIfile\fP
  797. containing its output.  (A command substitution, on the other
  798. hand, is replaced with the output itself.)
  799. \fCprint -l\fP is like \fCecho\fP, excepts that it prints its arguments
  800. one per line, the way \fCfgrep\fP expects them:
  801. .Ds
  802. % print -l foo bar
  803. foo
  804. bar
  805. .De
  806. We could also have written:
  807. .Ds
  808. % who | fgrep -f =(echo 'root
  809. > lemke
  810. > shgchan
  811. > subbarao')
  812. .De
  813. Using process substitution,
  814. you can edit the output of a command:
  815. .Ds
  816. % ed =(who | fgrep -f ~/.friends)
  817. 355
  818. g/lemke/d
  819. w /tmp/filbar
  820. 226
  821. q
  822. % cat /tmp/filbar
  823. root     console May 19 10:41
  824. shgchan  ttys1   May 23 16:52   (gaudi.Princeton.)
  825. subbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  826. subbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  827. shgchan  ttyvb   May 23 16:51   (gaudi.Princeton.)
  828. .De
  829. or easily read archived mail:
  830. .Ds
  831. % mail -f =(zcat ~/mail/oldzshmail.Z)
  832. "/tmp/zsha06024": 84 messages, 0 new, 43 unread
  833. >  1  U  TO: pfalstad, zsh (10)
  834.    2  U  nytim!tim@uunet.uu.net, Re: Zsh on Sparc1 /SunOS 4.0.3
  835.    3  U  JAM%TPN@utrcgw.utc.com, zsh fix (15)
  836.    4  U  djm@eng.umd.edu, way to find out if running zsh? (25)
  837.    5  U  djm@eng.umd.edu, Re: way to find out if running zsh? (17)
  838.    6   r djm@eng.umd.edu, Meta . (18)
  839.    7  U  jack@cs.glasgow.ac.uk, Re: problem building zsh (147)
  840.    8  U  nytim!tim@uunet.uu.net, Re: Zsh on Sparc1 /SunOS 4.0.3
  841.    9     ursa!jmd, Another fix... (61)
  842.   10  U  pplacewa@bbn.com, Re: v18i084: Zsh 2.00 - A small complaint (36)
  843.   11  U  lubkin@cs.rochester.edu, POSIX job control (34)
  844.   12  U  yale!bronson!tan@uunet.UU.NET
  845.   13  U  brett@rpi.edu, zsh (36)
  846.   14  S  subbarao, zsh sucks!!!! (286)
  847.   15  U  snibru!d241s008!d241s013!ala@relay.EU.net, zsh (165)
  848.   16  U  nytim!tim@uunet.UU.NET, Re: Zsh on Sparc1 /SunOS 4.0.3
  849.   17  U  subbarao, zsh is a junk shell (43)
  850.   18  U  amaranth@vela.acs.oakland.edu, zsh (33)
  851. 43u/84 1: x
  852. % ls -l /tmp/zsha06024
  853. /tmp/zsha06024 not found
  854. .De
  855. Note that the shell creates a temporary file, and deletes it
  856. when the command is finished.
  857. .Ds
  858. % diff =(ls) =(ls -F)
  859. 3c3
  860. < fortune
  861. ---
  862. > fortune*
  863. 10c10
  864. < strfile
  865. ---
  866. > strfile*
  867. .De
  868. If you read \fBzsh\fP's man page, you may notice that \fC<(\fR...\fC)\fR
  869. is another form of process substitution which is similar to
  870. \fC=(\fR...\fC)\fR.
  871. There is an important difference between the two.
  872. In the \fC<(\fR...\fC)\fR case, the shell creates a named pipe (FIFO)
  873. instead of a file.  This is better, since it does not
  874. fill up the file system; but it does not work in all cases.
  875. In fact, if we had replaced \fC=(\fR...\fC)\fR with \fC<(\fR...\fC)\fR in 
  876. the examples above, all of them would have stopped working
  877. except for \fCfgrep -f <(\fR...\fC)\fR.
  878. You can not edit a pipe, or open it as a mail folder;
  879. \fCfgrep\fP, however, has no problem with reading
  880. a list of words from a pipe.
  881. You may wonder why \fCdiff <(foo) bar\fP doesn't work, since
  882. \fCfoo | diff - bar\fP works; this is because \fCdiff\fP creates
  883. a temporary file if it notices that one of its arguments
  884. is \fC-\fP, and then copies its standard input to the temporary
  885. file.
  886. .PP
  887. \fC>(\fR...\fC)\fR is just like \fC<(\fR...\fC)\fR except that the
  888. command between the parentheses will get its input from the named
  889. pipe.
  890. .Ds
  891. % dvips -o >(lpr) zsh.dvi
  892. .De
  893. .Sh "Redirection"
  894. .PP
  895. Apart from all the regular redirections like the Bourne shell has,
  896. \fBzsh\fP can do more.  You can send the output of a command to more
  897. than one file, by specifying more redirections like
  898. .Ds
  899. % echo Hello World >file1 >file2
  900. .De
  901. and the text will end up in both files.  Similarly, you can send the
  902. output to a file and into a pipe:
  903. .Ds
  904. % make > make.log | grep Error
  905. .De
  906. The same goes for input.  You can make the input of a command come
  907. from more than one file.
  908. .Ds
  909. % sort <file1 <file2 <file3
  910. .De
  911. The command will first get the contents of file1 as its standard
  912. input, then those of file2 and finally the contents of file3.  This,
  913. too, works with pipes.
  914. .Ds
  915. % cut -d: -f1 /etc/passwd | sort <newnames
  916. .De
  917. The sort will get as its standard input first the output of \fCcut\fP
  918. and then the contents of \fCnewnames\fP.
  919. .PP
  920. Suppose you would like to watch the standard output of a command on
  921. your terminal, but want to pipe the standard error to another command.
  922. An easy way to do this in \fBzsh\fP is by redirecting the standard
  923. error using \fC2> >(\fR...\fC)\fR.
  924. .Ds
  925. % find / -name games 2> >(grep -v 'Permission' > realerrors)
  926. .De
  927. The above redirection will actually be implemented with a regular
  928. pipe, not a temporary named pipe.
  929. .Sh "Aliasing"
  930. .PP
  931. Often-used commands can be abbreviated with an alias:
  932. .Ds
  933. % alias uc=uncompress
  934. % ls
  935. hanoi.Z
  936. % uc hanoi
  937. % ls
  938. hanoi
  939. .De
  940. or commands with certain desired options:
  941. .Ds
  942. % alias fm='finger -m'
  943. % fm root
  944. Login name: root                        In real life: Operator
  945. Directory: /                            Shell: /bin/csh
  946. On since May 19 10:41:15 on console     3 days 5 hours Idle Time
  947. No unread mail
  948. No Plan.
  949.  
  950. % alias lock='lock -p -60000'
  951. % lock
  952. lock: /dev/ttyr4 on phoenix. timeout in 60000 minutes
  953. time now is Fri May 24 04:23:18 EDT 1991
  954. Key: 
  955.  
  956. % alias l='ls -AF'
  957. % l /
  958. \&.bash_history              kadb*
  959. \&.bashrc                    lib@
  960. \&.cshrc                     licensed/
  961. \&.exrc                      lost+found/
  962. \&.login                     macsyma
  963. \&\fR...
  964. .De
  965. Aliases can also be used to replace old commands:
  966. .Ds
  967. % alias grep=egrep ps=sps make=gmake
  968. % alias whoami='echo root'
  969. % whoami
  970. root
  971. .De
  972. or to define new ones:
  973. .Ds
  974. % cd /
  975. % alias sz='ls -l | sort -n +3 | tail -10'
  976. % sz
  977. drwxr-sr-x  7 bin          3072 May 23 11:59 etc
  978. drwxrwxrwx 26 root         5120 May 24 04:20 tmp
  979. drwxr-xr-x  2 root         8192 Dec 26 19:34 lost+found
  980. drwxr-sr-x  2 bin         14848 May 23 18:48 dev
  981. -r--r--r--  1 root       140520 Dec 26 20:08 boot
  982. -rwxr-xr-x  1 root       311172 Dec 26 20:08 kadb
  983. -rwxr-xr-x  1 root      1209695 Apr 16 15:33 vmunix.old
  984. -rwxr-xr-x  1 root      1209702 May 14 19:04 vmunix
  985. -rwxr-xr-x  1 root      1209758 May 21 12:23 vmunix.new.kernelmap.old
  986. -rwxr-xr-x  1 root      1711848 Dec 26 20:08 vmunix.org
  987. % cd
  988. % alias rable='ls -AFtrd *(R)' nrable='ls -AFtrd *(^R)'
  989. % rable
  990. README      func/       bin/        pub/        News/       src/
  991. nicecolors  etc/        scr/        tmp/        iris/       zsh*
  992. % nrable
  993. Mailboxes/  mail/       notes
  994. .De
  995. (The pattern \fC*(R)\fP matches all readable files in the current
  996. directory, and \fC*(^R)\fP matches all unreadable files.)
  997. .PP
  998. Most other shells have aliases of this kind (\fIcommand\fP aliases).
  999. However, \fBzsh\fP also has \fIglobal\fP aliases, which are substituted
  1000. anywhere on a line.
  1001. Global aliases can be used to abbreviate frequently-typed
  1002. usernames, hostnames, etc.
  1003. .Ds
  1004. % alias -g me=pfalstad gun=egsirer mjm=maruchck
  1005. % who | grep me
  1006. pfalstad ttyp0   May 24 03:39   (mickey.Princeton)
  1007. pfalstad ttyp5   May 24 03:42   (mickey.Princeton)
  1008. % fm gun
  1009. Login name: egsirer                     In real life: Emin Gun Sirer
  1010. Directory: /u/egsirer                   Shell: /bin/sh
  1011. Last login Thu May 23 19:05 on ttyq3 from bow.Princeton.ED
  1012. New mail received Fri May 24 02:30:28 1991;
  1013.   unread since Fri May 24 02:30:27 1991
  1014. % alias -g phx=phoenix.princeton.edu warc=wuarchive.wustl.edu
  1015. % ftp warc
  1016. Connected to wuarchive.wustl.edu.
  1017. .De
  1018. Here are some more interesting uses.
  1019. .Ds
  1020. % alias -g M='| more' GF='| fgrep -f ~/.friends'
  1021. % who M   # \fIpipes the output of \fCwho\fI through \fCmore
  1022. % who GF  # \fIsee if your friends are on\fC
  1023. % w GF    # \fIsee what your friends are doing
  1024. .De
  1025. Another example makes use of \fBzsh\fP's process substitution.
  1026. If you run NIS, and you miss being able to do this:
  1027. .Ds
  1028. % grep pfalstad /etc/passwd
  1029. .De
  1030. you can define an alias that will seem more natural
  1031. than \fCypmatch pfalstad passwd\fP:
  1032. .Ds
  1033. % alias -g PASS='<(ypcat passwd)'
  1034. % grep pfalstad PASS
  1035. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1036. .De
  1037. If you're really crazy, you can even call it \fC/etc/passwd\fP:
  1038. .Ds
  1039. % alias -g /etc/passwd='<(ypcat passwd)'
  1040. % grep pfalstad /etc/passwd
  1041. pfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1042. .De
  1043. The last example shows one of the perils of global aliases;
  1044. they have a lot of potential to cause confusion.
  1045. For example, if you defined a global alias called \fC|\fP (which is
  1046. possible), \fBzsh\fP would begin to act very strangely; every pipe
  1047. symbol would be replaced with the text of your alias.
  1048. To some extent, global aliases are like macros in C;
  1049. discretion is advised in using them and in choosing names for them.
  1050. Using names in all caps is not a bad idea, especially
  1051. for aliases which introduce shell metasyntax (like \fCM\fP and \fCGF\fP
  1052. above).
  1053. .PP
  1054. Note that \fBzsh\fP aliases are not like csh aliases.  The syntax for
  1055. defining them is different, and they do not have arguments.
  1056. All your favorite csh aliases will probably not work under \fBzsh\fP.
  1057. For example, if you try:
  1058. .Ds
  1059. alias rm mv '\e!* /tmp/wastebasket'
  1060. .De
  1061. no aliases will be defined, but \fBzsh\fP will not report an error.
  1062. In csh, this line defines an alias that makes \fCrm\fP safe---files
  1063. that are \fCrm\fP'd will be moved to a temporary directory instead of
  1064. instantly destroyed.  In \fBzsh\fP's syntax, however, this line asks
  1065. the shell to print any existing alias definitions for \fCrm\fP,
  1066. \fCmv\fP, or \fC!*\ /tmp/wastebasket\fP.  Since there are none, most
  1067. likely, the shell will not print anything, although \fCalias\fP will
  1068. return a nonzero exit code.  The proper syntax is this:
  1069. .Ds
  1070. alias rm='mv \e!* /tmp/wastebasket'
  1071. .De
  1072. However, this won't work either:
  1073. .Ds
  1074. % rm foo.dvi
  1075. zsh: no matches found: !*
  1076. .De
  1077. While this makes \fCrm\fP safe, it is certainly not what the user
  1078. intended.  In \fBzsh\fP, you must use a shell function for this:
  1079. .Ds
  1080. % unalias rm
  1081. % rm () { mv $* /tmp/wastebasket }
  1082. % rm foo.dvi
  1083. % ls /tmp/wastebasket
  1084. foo.dvi
  1085. .De
  1086. While this is much cleaner and easier to read (I hope you will
  1087. agree), it is not csh-compatible.  Therefore, a script to convert
  1088. csh aliases and variables has been provided.  You should only need to use it
  1089. once, to convert all your csh aliases and parameters to \fBzsh\fP format:
  1090. .Ds
  1091. % csh
  1092. csh> alias
  1093. l       ls -AF
  1094. more    less
  1095. on      last -2 !:1 ; who | grep !:1
  1096. csh> exit
  1097. % c2z >neat_zsh_aliases
  1098. % cat neat_zsh_aliases
  1099. alias l='ls -AF'
  1100. alias more='less'
  1101. on () { last -2 $1 ; who | grep $1 }
  1102. \&...
  1103. .De
  1104. The first two aliases were converted to regular \fBzsh\fP aliases, while
  1105. the third, since it needed to handle arguments, was converted to
  1106. a function.  \fCc2z\fP can convert most aliases to \fBzsh\fP format without
  1107. any problems.  However, if you're using some really arcane csh tricks,
  1108. or if you have an alias with a name like \fCdo\fP (which is reserved
  1109. in \fBzsh\fP), you may have to fix some of the aliases by hand.
  1110. .PP
  1111. The \fCc2z\fP script checks your csh setup, and produces a list
  1112. of \fBzsh\fP commands which replicate your aliases and parameter settings
  1113. as closely as possible.  You could include its output in your
  1114. startup file, \fC.zshrc\fP.
  1115. .Sh "History"
  1116. .PP
  1117. There are several ways to manipulate history in \fBzsh\fP.
  1118. One way is to use csh-style \fC!\fP history:
  1119. .Ds
  1120. % /usr/local/bin/!:0 !-2*:s/foo/bar/ >>!$
  1121. .De
  1122. If you don't want to use this, you can turn it off
  1123. by typing \fCsetopt nobanghist\fP.  If you are afraid of accidentally
  1124. executing the wrong command you can set the \fIHISTVERIFY\fP option.
  1125. If this option is set, commands that result from history expansion
  1126. will not be executed immediately, but will be put back into the editor
  1127. buffer for further consideration.
  1128. .PP
  1129. If you're not familiar with \fC!\fP history, here follows some
  1130. explanation.  History substitutions always start with a \fC!\fP,
  1131. commonly called \*Qbang\*U.  After the \fC!\fP comes an (optional)
  1132. designation of which \*Qevent\*U (command) to use, then a colon, and
  1133. then a designation of what word of that command to use.  For example,
  1134. \fC!-\fIn\fR refers to the command \fIn\fP commands ago.
  1135. .Ds
  1136. % ls
  1137. foo  bar
  1138. % cd foo
  1139. % !-2
  1140. ls
  1141. baz  bam
  1142. .De
  1143. No word designator was used, which means that the whole command
  1144. referred to was repeated.  Note that the shell will echo the result of
  1145. the history substitution.  The word designator can, among other
  1146. things, be a number indicating the argument to use, where \fC0\fP is
  1147. the command.
  1148. .Ds
  1149. % /usr/bin/ls foo
  1150. foo
  1151. % !:0 bar
  1152. /usr/bin/ls bar
  1153. bar
  1154. .De
  1155. In this example, no event designator was used, which tells \fBzsh\fP
  1156. to use the previous command.  A \fC$\fP specifies the last argument
  1157. .Ds
  1158. % mkdir /usr/local/lib/emacs/site-lisp/calc
  1159. % cd !:$
  1160. cd /usr/local/lib/emacs/site-lisp/calc
  1161. .De
  1162. If you use more words of the same command, only the first \fC!\fP
  1163. needs an event designator.
  1164. .Ds
  1165. % make prig >> make.log
  1166. make: *** No rule to make target `prig'.  Stop.
  1167. % cd src
  1168. % !-2:0 prog >> !:$
  1169. make prog >> make.log
  1170. .De
  1171. This is different from csh, where a bang with no event designator
  1172. always refers to the previous command.  If you actually like this
  1173. behaviour, set the \fICSHJUNKIEHISTORY\fP option.
  1174. .Ds
  1175. % setopt cshjunkiehistory
  1176. % !-2:0 prog2 >> !:$
  1177. make prog2 >> cshjunkiehistory
  1178. .De
  1179. Another way to use history is to use the \fCfc\fP command.  For
  1180. example, if you type an erroneous command:
  1181. .Ds
  1182. % for i in `cat /etc/clients` 
  1183.  do 
  1184.  rpu $i 
  1185.  done
  1186. zsh: command not found: rpu
  1187. zsh: command not found: rpu
  1188. zsh: command not found: rpu
  1189. \&\fR...
  1190. .De
  1191. typing \fCfc\fP will execute an editor on this command, allowing
  1192. you to fix it.  (The default editor is \fCvi\fP, by the way,
  1193. not \fCed\fP).
  1194. .Ds
  1195. % fc
  1196. 49
  1197. /rpu/s//rup/p
  1198.  rup $i 
  1199. w
  1200. 49
  1201. q
  1202. for i in `cat /etc/clients` 
  1203.  do 
  1204.  rup $i 
  1205.  done
  1206.         beam    up  2 days, 10:17,    load average: 0.86, 0.80, 0.50
  1207.          bow    up  4 days,  8:41,    load average: 0.91, 0.80, 0.50
  1208.         burn    up          17:18,    load average: 0.91, 0.80, 0.50
  1209.        burst    up  9 days,  1:49,    load average: 0.95, 0.80, 0.50
  1210.          tan    up          11:14,    load average: 0.91, 0.80, 0.50
  1211.        bathe    up  3 days, 17:49,    load average: 1.84, 1.79, 1.50
  1212.         bird    up  1 day,   9:13,    load average: 1.95, 1.82, 1.51
  1213.       bonnet    up  2 days, 21:18,    load average: 0.93, 0.80, 0.50
  1214. \&\fR...
  1215. .De
  1216. A variant of the \fCfc\fP command is \fCr\fP, which redoes the last
  1217. command, with optional changes:
  1218. .Ds
  1219. % echo foo
  1220. foo
  1221. % r
  1222. echo foo
  1223. foo
  1224.  
  1225. % echo foo
  1226. foo
  1227. % r foo=bar
  1228. echo bar
  1229. bar
  1230. .De
  1231. .Sh "Command Line Editing"
  1232. .PP
  1233. \fBzsh\fP's command line editor, \fBZLE\fP, is quite powerful.
  1234. It is designed to emulate either emacs or vi; the default
  1235. is emacs.  To set the bindings for vi mode, type \fCbindkey -v\fP.  If
  1236. your \fBEDITOR\fP or \fBVISUAL\fP environment variable is vi,
  1237. \fBzsh\fP will use vi emulation by default.  You can then switch to
  1238. emacs mode with \fCbindkey -e\fP.
  1239. .PP
  1240. In addition to basic editing, the shell allows you to 
  1241. recall previous lines in the history.  In emacs mode,
  1242. this is done with \fI^P\fP (control-P) or (on many terminals) with the
  1243. cursor-up key:
  1244. .Ds
  1245. % ls ~
  1246. -           README      file        mail        pub         tmp
  1247. Mailboxes   bin         func        nicecolors  scr         zsh
  1248. News        etc         iris        notes       src
  1249. % echo foobar
  1250. foobar
  1251. % \fI^P\fC
  1252. % echo foobar\fI^P\fC
  1253. % ls ~_
  1254. .De
  1255. Pressing \fI^P\fP once brings up the previous line (\fCecho foobar\fP);
  1256. pressing it again brings up the line before that (\fCls ~\fP).
  1257. The cursor is left at the end of the line, allowing you to
  1258. edit the line if desired before executing it.
  1259. In many cases, \fBZLE\fP eliminates the need for the \fCfc\fP command,
  1260. since it is powerful enough to handle even multiline commands:
  1261. .Ds
  1262. % for i in a b c d e
  1263. > do
  1264. > echo $i
  1265. > done
  1266. a
  1267. b
  1268. c
  1269. d
  1270. e
  1271. % \fI^P\fC
  1272. % for i in a b c d e 
  1273.  do 
  1274.  echo $i 
  1275.  done_
  1276. .De
  1277. Now you can just move up to the part you want to change...
  1278. .Ds
  1279. % for i in \kxa\l'|\nxu\(ul' b c d e
  1280.  do 
  1281.  echo $i 
  1282.  done
  1283. .De
  1284. change it, and execute the new command.
  1285. .Ds
  1286. % for i in f g h i j
  1287.  do 
  1288.  echo $i 
  1289.  done
  1290. f
  1291. g
  1292. h
  1293. i
  1294. j
  1295. .De
  1296. Also, you can search the history for a certain command using
  1297. \fIESC-P\fP, this will look for the last command that started with the
  1298. (part of the) word at the beginning of the current line.  Hitting
  1299. \fIESC-P\fP another time gets you the command before that, etc.
  1300. .Ds
  1301. % set \fIESC-P\fC
  1302. % setopt autolist \fIESC-P\fC
  1303. % setopt nocorrect_
  1304. .De
  1305. Another way is to do an incremental search, emacs-style:
  1306. .Ds
  1307. % \fI^R\fC
  1308. % _
  1309. i-search:
  1310.  
  1311. % l\kxs\l'|\nxu\(ul' /usr/bin
  1312. i-search: l
  1313.  
  1314. % date > foofile\kx.\l'|\nxu\(ul'c
  1315. i-search: le
  1316. .De
  1317. Suppose you have retrieved an old history event in one of these ways
  1318. and would like to execute several consecutive old commands starting
  1319. with this one.  \fC^O\fP will execute the current command and then put
  1320. the next command from the history into the editor buffer.  Typing
  1321. \fC^O\fP several times will therefore reexecute several consecutive
  1322. commands from the history.  Of course, you can edit some of those
  1323. commands in between.
  1324. .PP
  1325. In addition to completion (see below), \fITAB\fP performs expansion if
  1326. possible.
  1327. .Ds
  1328. % ls *.c\fITAB\fC
  1329. % ls foofile.c fortune.c rnd.c strfile.c unstr.c_
  1330. .De
  1331. For example, suppose you have a bunch of weird files in an important
  1332. directory:
  1333. .Ds
  1334. % ls
  1335.   * * *       ; & % $??foo  dspfok        foo.c
  1336.   !"foo"!       ` \e `         foo           rrr
  1337. .De
  1338. You want to remove them, but you don't want to damage \fCfoo.c\fP.
  1339. Here is one way to do this:
  1340. .Ds
  1341. % rm *\fITAB\fC
  1342. % rm \e \e \e*\e \e*\e \e*\e \e \e  \e!\e"foo\e"\e! \e;\e \e&\e %\e \e$'
  1343. ''
  1344. 'foo \e`\e \e\e\e \e` dspfok foo foo.c rrr_
  1345. .De
  1346. When you expand \fC*\fP, \fBzsh\fP inserts the names of all the files
  1347. into the editing buffer, with proper shell quoting.
  1348. Now, just move back and remove \fCfoo.c\fP from the buffer:
  1349. .Ds
  1350. % rm \e \e \e*\e \e*\e \e*\e \e \e  \e!\e"foo\e"\e! \e;\e \e&\e %\e \e$'
  1351. ''
  1352. 'foo \e`\e \e\e\e \e` dspfok foo \kxr\l'|\nxu\(ul'rr
  1353. .De
  1354. and press return.
  1355. Everything except \fCfoo.c\fP will be deleted from the directory.  If
  1356. you do not want to actually expand the current word, but would like to
  1357. see what the matches are, type \fC^Xg\fP.
  1358. .Ds
  1359. % rm f*\fI^Xg\fP
  1360. foo    foo.c
  1361. % rm f*_
  1362. .De
  1363. Here's another trick; let's say you have typed this command in:
  1364. .Ds
  1365. % gcc -o x.out foob.c -g -Wpointer-arith -Wtrigraphs_
  1366. .De
  1367. and you forget which library you want.  You need to escape
  1368. out for a minute and check by typing
  1369. \fCls /usr/lib\fP, or some other such command;
  1370. but you don't want to retype the whole command again,
  1371. and you can't press return now because the current command
  1372. is incomplete.
  1373. In \fBzsh\fP, you can put the line on the \fIbuffer stack\fP, using
  1374. \fIESC-Q\fP, and type some other commands.  The next time a prompt is printed,
  1375. the \fCgcc\fP line will be popped off the stack and put
  1376. in the editing buffer automatically; you can then enter the
  1377. proper library name and press return (or, \fIESC-Q\fP again and look
  1378. for some other libraries whose names you forgot).
  1379. .PP
  1380. A similar situation: what if you forget the option to gcc that
  1381. finds bugs using AI techniques?  You could either use \fIESC-Q\fP
  1382. again, and type \fCman gcc\fP, or you could press \fIESC-H\fP, which
  1383. essentially does the same thing; it puts the current line on
  1384. the buffer stack, and executes the command \fCrun-help gcc\fP,
  1385. where \fCrun-help\fP is an alias for \fCman\fP.
  1386. .PP
  1387. Another interesting command is \fIESC-A\fP.  This executes the
  1388. current line, but retains it in the buffer, so that it appears
  1389. again when the next prompt is printed.
  1390. Also, the cursor stays in the same place.
  1391. This is useful for executing a series of similar commands:
  1392. .Ds
  1393. % cc grok.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1394. % cc fubar.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1395. % cc fooble.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1396. .De
  1397. .PP
  1398. The \fIESC-'\fP command is useful for managing the shell's quoting
  1399. conventions.  Let's say you want to print this string:
  1400. .Ds
  1401. don't do that; type 'rm -rf \e*', with a \e before the *.
  1402. .De
  1403. All that is necessary is to type it into the editing buffer:
  1404. .Ds
  1405. % don't do that; type 'rm -rf \e*', with a \e before the *.
  1406. .De
  1407. press \fIESC-'\fP (escape-quote):
  1408. .Ds
  1409. % 'don'\e''t do that; type '\e''rm -rf \e*'\e'', with a \e before the *.'
  1410. .De
  1411. then move to the beginning and add the \fCecho\fP command.
  1412. .Ds
  1413. % echo 'don'\e''t do that; type '\e''rm -rf \e*'\e'', with a \e before the *.'
  1414. don't do that; type 'rm -rf \e*', with a \e before the *.
  1415. .De
  1416. Let's say you want to create an alias to do this \fCecho\fP command.
  1417. This can be done by recalling the line with \fI^P\fP and pressing
  1418. \fIESC-'\fP again:
  1419. .Ds
  1420. % 'echo '\e''don'\e''\e'\e'''\e''t do that; type '\e''\e'\e'''\e''rm -rf
  1421. \e*'\e''\e'\e'''\e'', with a \e before the *.'\e'''
  1422. .De
  1423. and then move to the beginning and add the command to create
  1424. an alias.
  1425. .Ds
  1426. % alias zoof='echo '\e''don'\e''\e'\e'''\e''t do that; type '\e''\e'\e'''\e''rm
  1427. -rf \e*'\e''\e'\e'''\e'', with a \e before the *.'\e'''
  1428. % zoof
  1429. don't do that; type 'rm -rf \e*', with a \e before the *.
  1430. .De
  1431. If one of these fancy editor commands changes your command line in a
  1432. way you did not intend, you can undo changes with \fC^_\fP, if you can
  1433. get it out of your keyboard, or \fC^X^U\fP, otherwise.
  1434. .PP
  1435. Another use of the editor is to edit the value of variables.
  1436. For example, an easy way to change your path is to use
  1437. the \fCvared\fP command:
  1438. .Ds
  1439. % vared PATH
  1440. > /u/pfalstad/scr:/u/pfalstad/bin/sun4:/u/maruchck/scr:/u/subbarao/bin:/u/maruc
  1441. hck/bin:/u/subbarao/scripts:/usr/princeton/bin:/usr/ucb:/usr/bin:/bin:/usr/host
  1442. s:/usr/princeton/bin/X11:/./usr/lang:/./usr/etc:/./etc
  1443. .De
  1444. You can now edit the path.  When you press return, the contents
  1445. of the edit buffer will be assigned to \fBPATH\fP.
  1446. .Sh "Completion"
  1447. .PP
  1448. Another great \fBzsh\fP feature is completion.  If you hit \fITAB\fP, \fBzsh\fP
  1449. will complete all kinds of stuff.  Like commands or filenames:
  1450. .Ds
  1451. % comp\fITAB\fC
  1452. % compress _
  1453.  
  1454. % ls nic\fITAB\fC
  1455. % ls nicecolors _
  1456.  
  1457. % ls /usr/pr\fITAB\fC
  1458. % ls /usr/princeton/_
  1459.  
  1460. % ls -l =com\fITAB\fC
  1461. % ls -l =compress _
  1462. .De
  1463. If the completion is ambiguous, the editor will beep.  If you find
  1464. this annoying, you can set the \fINOLISTBEEP\fP option.  Completion
  1465. can even be done in the middle of words.  To use this, you will have
  1466. to set the \fICOMPLETEINWORD\fP option:
  1467. .Ds
  1468. % setopt completeinword
  1469. % ls /usr/p\kxt\l'|\nxu\(ul'on\fITAB\fC
  1470. % ls /usr/prince\kxt\l'|\nxu\(ul'on/
  1471. % setopt alwaystoend
  1472. % ls /usr/p\kxt\l'|\nxu\(ul'on\fITAB\fC
  1473. % ls /usr/princeton/_
  1474. .De
  1475. You can list possible completions by pressing \fI^D\fP:
  1476. .Ds
  1477. % ls /vmu\fITAB \(embeep\(em\fC
  1478. % ls /vmunix_
  1479. % ls /vmunix\fI^D\fC
  1480. vmunix                    vmunix.old                
  1481. vmunix.new.kernelmap.old  vmunix.org
  1482. .De
  1483. Or, you could just set the \fIAUTOLIST\fP option:
  1484. .Ds
  1485. % setopt autolist
  1486. % ls /vmu\fITAB \(embeep\(em\fC
  1487. vmunix                    vmunix.old                
  1488. vmunix.new.kernelmap.old  vmunix.org
  1489. % ls /vmunix_
  1490. .De
  1491. If you like to see the types of the files in these lists, like in
  1492. \fCls\ -F\fP, you can set the \fILISTTYPES\fP option.  Together with
  1493. \fIAUTOLIST\fP you can use \fILISTAMBIGUOUS\fP.  This will only list
  1494. the possibilities if there is no unambiguous part to add:
  1495. .Ds
  1496. % setopt listambiguous
  1497. % ls /vmu\fITAB \(embeep\(em\fC
  1498. % ls /vmunix_\fITAB \(embeep\(em\fC
  1499. vmunix                    vmunix.old                
  1500. vmunix.new.kernelmap.old  vmunix.org
  1501. .De
  1502. If you don't want several of these listings to scroll the screen so
  1503. much, the \fIALWAYSLASTPROMPT\fP option is useful.  If set, you can
  1504. continue to edit the line you were editing, with the completion
  1505. listing appearing beneath it.
  1506. .PP
  1507. Another interesting option is \fIMENUCOMPLETE\fP.  This affects the
  1508. way \fITAB\fP works.  Let's look at the \fC/vmunix\fP example again:
  1509. .Ds
  1510. % setopt menucomplete
  1511. % ls /vmu\fITAB\fC
  1512. % ls /vmunix\fITAB\fC
  1513. % ls /vmunix.new.kernelmap.old\fITAB\fC
  1514. % ls /vmunix.old_
  1515. .De
  1516. Each time you press \fITAB\fP, it displays the next possible completion.
  1517. In this way, you can cycle through the possible completions until
  1518. you find the one you want.
  1519. .PP
  1520. The \fIAUTOMENU\fP option makes a nice compromise between this method
  1521. of completion and the regular method.  If you set this option,
  1522. pressing \fITAB\fP once completes the unambiguous part normally,
  1523. pressing the \fITAB\fP key repeatedly after an ambiguous completion
  1524. will cycle through the possible completions.
  1525. .PP
  1526. Another option you could set is \fIRECEXACT\fP, which causes
  1527. exact matches to be accepted, even if there are other possible
  1528. completions:
  1529. .Ds
  1530. % setopt recexact
  1531. % ls /vmu\fITAB \(embeep\(em\fC
  1532. vmunix                    vmunix.old                
  1533. vmunix.new.kernelmap.old  vmunix.org
  1534. % ls /vmunix_\fITAB\fC
  1535. % ls /vmunix _
  1536. .De
  1537. To facilitate the typing of pathnames, a slash will be added whenever
  1538. a directory is completed.  Some computers don't like the spurious
  1539. slashes at the end of directory names.  In that case, the
  1540. \fIAUTOREMOVESLASH\fP option comes to rescue.  It will remove these
  1541. slashes when you type a space or return after them.
  1542. .PP
  1543. The \fIfignore\fP variable lists suffixes of files to ignore
  1544. during completion.
  1545. .Ds
  1546. % ls foo\fITAB \(embeep\(em\fC
  1547. foofile.c  foofile.o
  1548. % fignore=( .o \e~ .bak .junk )
  1549. % ls foo\fITAB\fP
  1550. % ls foofile.c _
  1551. .De
  1552. Since \fCfoofile.o\fP has a suffix that is in the \fCfignore\fP list,
  1553. it was not considered a possible completion of \fCfoo\fP.
  1554. .PP
  1555. Username completion is also supported:
  1556. .Ds
  1557. % ls ~pfal\fITAB\fC
  1558. % ls ~pfalstad/_
  1559. .De
  1560. and parameter name completion:
  1561. .Ds
  1562. % echo $ORG\fITAB\fC
  1563. % echo $ORGANIZATION _
  1564. % echo ${ORG\fITAB\fC
  1565. % echo ${ORGANIZATION _
  1566. .De
  1567. Note that in the last example a space is added after the completion as
  1568. usual.  But if you want to add a colon or closing brace, you probably
  1569. don't want this extra space.  Setting the \fIAUTOPARAMKEYS\fP option
  1570. will automatically remove this space if you type a colon or closing
  1571. brace after such a completion.
  1572. .PP
  1573. There is also option completion:
  1574. .Ds
  1575. % setopt nocl\fITAB\fC
  1576. % setopt noclobber _
  1577. .De
  1578. and binding completion:
  1579. .Ds
  1580. % bindkey '^X^X' pu\fITAB\fC
  1581. % bindkey '^X^X' push-line _
  1582. .De
  1583. The \fCcompctl\fP command is used to control completion of the
  1584. arguments of specific commands.  For example, to specify that certain
  1585. commands take other commands as arguments, you use \fCcompctl -c\fP:
  1586. .Ds
  1587. % compctl -c man nohup
  1588. % man upt\fITAB\fC
  1589. % man uptime _
  1590. .De
  1591. To specify that a command should complete filenames, you should use
  1592. \fCcompctl -f\fP.  This is the default.  It can be combined with \fC-c\fP,
  1593. as well.
  1594. .Ds
  1595. % compctl -cf echo
  1596. % echo upt\fITAB\fC
  1597. % echo uptime _
  1598.  
  1599. % echo fo\fITAB\fC
  1600. % echo foo.c
  1601. .De
  1602. Similarly, use \fC-o\fP to specify options, \fC-v\fP to specify
  1603. variables, and \fC-b\fP to specify bindings.
  1604. .Ds
  1605. % compctl -o setopt unsetopt
  1606. % compctl -v typeset vared unset export
  1607. % compctl -b bindkey
  1608. .De
  1609. You can also use \fC-k\fP to specify a custom list of keywords to use
  1610. in completion.  After the \fC-k\fP comes either the name of an array
  1611. or a literal array to take completions from.
  1612. .Ds
  1613. % ftphosts=(ftp.uu.net wuarchive.wustl.edu)
  1614. % compctl -k ftphosts ftp
  1615. % ftp wu\fITAB\fC
  1616. % ftp wuarchive.wustl.edu _
  1617.  
  1618. % compctl -k '(cpirazzi subbarao sukthnkr)' mail finger
  1619. % finger cp\fITAB\fC
  1620. % finger cpirazzi _
  1621. .De
  1622. To better specify the files to complete for a command, use the
  1623. \fC-g\fP option which takes any glob pattern as an argument.  Be sure
  1624. to quote the glob patterns as otherwise they will be expanded when the
  1625. \fCcompctl\fP command is run.
  1626. .Ds
  1627. % ls
  1628. letter.tex  letter.dvi  letter.aux  letter.log  letter.toc
  1629. % compctl -g '*.tex' latex
  1630. % compctl -g '*.dvi' xdvi dvips
  1631. % latex l\fITAB\fC
  1632. % latex letter.tex _
  1633. % xdvi l\fITAB\fC
  1634. % xdvi letter.dvi _
  1635. .De
  1636. Glob patterns can include qualifiers within parentheses.  To rmdir
  1637. only directories and cd to directories and symbolic links pointing to
  1638. them:
  1639. .Ds
  1640. % compctl -g '*(-/)' cd
  1641. % compctl -g '*(/)' rmdir
  1642. .De
  1643. RCS users like to run commands on files which are not in the current
  1644. directory, but in the RCS subdirectory where they all get \fC,v\fP
  1645. suffixes.  They might like to use
  1646. .Ds
  1647. % compctl -g 'RCS/*(:t:s/\e,v//)' co rlog rcs
  1648. % ls RCS
  1649. builtin.c,v  lex.c,v      zle_main.c,v
  1650. % rlog bu\fITAB\fC
  1651. % rlog builtin.c _
  1652. .De
  1653. The \fC:t\fP modifier keeps only the last part of the pathname and the
  1654. \fC:s/\e,v//\fP will replace any \fC,v\fP by nothing.
  1655. .PP
  1656. The \fC-s\fP flag is similar to \fC-g\fP, but it uses all expansions,
  1657. instead of just globbing, like brace expansion, parameter substitution
  1658. and command substitution.
  1659. .Ds
  1660. % compctl -s '$(setopt)' unsetopt
  1661. .De
  1662. will only complete options which are actually set to be arguments to
  1663. \fCunsetopt\fP.
  1664. .PP
  1665. Sometimes a command takes another command as its argument.  You can
  1666. tell \fBzsh\fP to complete commands as the first argument to such a
  1667. command and then use the completion method of the second command.  The
  1668. \fC-l\fP flag with a null-string argument is used for this.
  1669. .Ds
  1670. % compctl -l '' nohup exec
  1671. % nohup comp\fITAB\fC
  1672. % nohup compress _
  1673. % nohup compress fil\fITAB\fC
  1674. % nohup compress filename _
  1675. .De
  1676. Sometimes you would like to run really complicated commands to find
  1677. out what the possible completions are.  To do this, you can specify a
  1678. shell function to be called that will assign the possible completions
  1679. to a variable called reply.  Note that this variable must be an array.
  1680. Here's another (much slower) way to get the completions for \fCco\fP
  1681. and friends:
  1682. .Ds
  1683. % function getrcs {
  1684. > reply=()
  1685. > for i in RCS/*
  1686. >   do
  1687. >   reply=($reply[*] $(basename $i ,v))
  1688. >   done
  1689. > }
  1690. % compctl -K getrcs co rlog rcs
  1691. .De
  1692. Some command arguments use a prefix that is not a part of the things
  1693. to complete.  The kill builtin command takes a signal name after a
  1694. \fC-\fP.  To make such a prefix be ignored in the completion process,
  1695. you can use the \fC-P\fP flag.
  1696. .Ds
  1697. % compctl -P - -k signals kill
  1698. % kill -H\fITAB\fP
  1699. % kill -HUP _
  1700. .De
  1701. TeX is usually run on files ending in \fC.tex\fP, but also sometimes
  1702. on other files.  It is somewhat annoying to specify that the arguments
  1703. of TeX should end in \fC.tex\fP and then not be able to complete these
  1704. other files.  Therefore you can specify things like \*QComplete to
  1705. files ending in \fC.tex\fP if available, otherwise complete to any
  1706. filename.\*U.  This is done with \fIxor\fPed completion:
  1707. .Ds
  1708. % compctl -g '*.tex' + -f tex
  1709. .De
  1710. The \fC+\fP tells the editor to only take the next thing into account
  1711. if the current one doesn't generate any matches.  If you have not
  1712. changed the default completion, the above example is in fact
  1713. equivalent to
  1714. .Ds
  1715. % compctl -g '*.tex' + tex
  1716. .De
  1717. as a lone \fC+\fP at the end is equivalent to specifying the default
  1718. completion after the \fC+\fP.  This form of completion is also
  1719. frequently used if you want to run some command only on a certain type
  1720. of files, but not necessarily in the current directory.  In this case
  1721. you will want to complete both files of this type and directories.
  1722. Depending on your preferences you can use either of
  1723. .Ds
  1724. % compctl -g '*.ps' + -g '*(-/)' ghostview
  1725. % compctl -g '*.ps *(-/)' ghostview
  1726. .De
  1727. where the first one will only complete directories (and symbolic links
  1728. pointing to directories) if no postscript file matches the already
  1729. typed part of the argument.
  1730. .Sh "Extended completion"
  1731. .PP
  1732. If you play with completion, you will soon notice that you would like
  1733. to specify what to complete, depending on what flags you give to the
  1734. command and where you are on the command line.  For example, a command
  1735. could take any filename argument after a \fC-f\fP flag, a username
  1736. after a \fC-u\fP flag and an executable after a \fC-x\fP flag.  This
  1737. section will introduce you to the ways to specify these things.  To
  1738. many people it seems rather difficult at first, but taking the trouble
  1739. to understand it can save you lots of typing in the end.  Even I keep
  1740. being surprised when \fBzsh\fP manages to complete a small or even
  1741. empty prefix to the right file in a large directory.
  1742. .PP
  1743. To tell \fBzsh\fP about these kinds of completion, you use \*Qextended
  1744. completion\*U by specifying the \fC-x\fP flag to compctl.  The
  1745. \fC-x\fP flag takes a list of patterns/flags pairs.  The patterns
  1746. specify when to complete and the flags specify what.  The flags are
  1747. simply those mentioned above, like \fC-f\fP or \fC-g \fIglob
  1748. pattern\fR.
  1749. .PP
  1750. As an example, the \fCr[\fIstring1\fC,\fIstring2\fC]\fR pattern
  1751. matches if the cursor is after something that starts with
  1752. \fIstring1\fP and before something that starts with \fIstring2\fP.
  1753. The \fIstring2\fP is often something that you do not want to match
  1754. anything at all.
  1755. .Ds
  1756. % ls
  1757. foo1   bar1   foo.Z  bar.Z
  1758. % compctl -g '^*.Z' -x 'r[-d,---]' -g '*.Z' -- compress
  1759. % compress f\fITAB\fP
  1760. % compress foo1 _
  1761. % compress -d f\fITAB\fP
  1762. % compress -d foo.Z _
  1763. .De
  1764. In the above example, if the cursor is after the \fC-d\fP the pattern
  1765. will match and therefore \fBzsh\fP uses the \fC-g *.Z\fP flag that will only
  1766. complete files ending in \fC.Z\fP.  Otherwise, if no pattern matches,
  1767. it will use the flags before the \fC-x\fP and in this case complete
  1768. every file that does not end in \fC.Z\fP.
  1769. .PP
  1770. The \fCs[\fIstring\fC]\fR pattern matches if the current word starts
  1771. with \fIstring\fP.  The \fIstring\fP itself is not considered to be
  1772. part of the completion.
  1773. .Ds
  1774. % compctl -x 's[-]' -k signals -- kill
  1775. % kill -H\fITAB\fP
  1776. % kill -HUP _
  1777. .De
  1778. The \fCtar\fP command takes a tar file as an argument after the
  1779. \fC-f\fP option.  The \fCc[\fIoffset\fC,\fIstring\fC]\fR pattern
  1780. matches if the word in position \fIoffset\fP relative to the current
  1781. word is \fIstring\fP.  More in particular, if \fIoffset\fP is -1, it
  1782. matches if the previous word is \fIstring\fP.  This suggests
  1783. .Ds
  1784. % compctl -f -x 'c[-1,-f]' -g '*.tar' -- tar
  1785. .De
  1786. But this is not enough.  The \fC-f\fP option could be the last of a
  1787. longer string of options.  \fCC[\fR...\fC,\fR...\fC]\fR is just like
  1788. \fCc[\fR...\fC,\fR...\fC]\fR, except that it uses glob-like pattern
  1789. matching for \fIstring\fP.  So
  1790. .Ds
  1791. % compctl -f -x 'C[-1,-*f]' -g '*.tar' -- tar
  1792. .De
  1793. will complete tar files after any option string ending in an \fCf\fP.
  1794. But we'd like even more.  Old versions of tar used all options as the
  1795. first argument, but without the minus sign.  This might be
  1796. inconsistent with option usage in all other commands, but it is still
  1797. supported by newer versions of \fCtar\fP.  So we would also like to
  1798. complete tar files if the first argument ends in an \fCf\fP and we're
  1799. right behind it.
  1800. .PP
  1801. We can `and' patterns by putting them next to each other with a space
  1802. between them.  We can `or' these sets by putting comma's between them.
  1803. We will also need some new patterns.  \fCp[\fInum\fC]\fR will match if
  1804. the current argument (the one to be completed) is the \fInum\fPth
  1805. argument.  \fCW[\fIindex\fC,\fIpattern\fC]\fR will match if the
  1806. argument in place \fIindex\fP matches the \fIpattern\fP.  This gives
  1807. us
  1808. .Ds
  1809. % compctl -f -x 'C[-1,-*f] , W[1,*f] p[2]' -g '*.tar' -- tar
  1810. .De
  1811. In words: If the previous argument is an option string that ends in an
  1812. \fCf\fP, or the first argument ended in an \fCf\fP and it is now the
  1813. second argument, then complete only filenames ending in \fC.tar\fP.
  1814. .PP
  1815. All the above examples used only one set of patterns with one
  1816. completion flag.  You can use several of these pattern/flag pairs
  1817. separated by a \fC-\fP.  The first matching pattern will be used.
  1818. Suppose you have a version of \fCtar\fP that supports compressed files
  1819. by using a \fC-Z\fP option.  Leaving the old tar syntax aside for a
  1820. moment, we would like to complete files ending in \fC.tar.Z\fP if a
  1821. \fC-Z\fP option has been used and files ending in \fC.tar\fP
  1822. otherwise, all this only after a \fC-f\fP flag.  Again, the \fC-Z\fP
  1823. can be alone or it can be part of a longer option string, perhaps the
  1824. same as that of the \fC-f\fP flag.  Here's how to do it; note the
  1825. backslash and the secondary prompt which are not part of the
  1826. \fCcompctl\fP command.
  1827. .Ds
  1828. % compctl -f -x 'C[-1,-*Z*f] , R[-*Z*,---] C[-1,-*f]' -g '*.tar.Z' - \e
  1829. > 'C[-1,-*f]' -g '*.tar' -- tar
  1830. .De
  1831. The first pattern set tells us to match if either the previous
  1832. argument was an option string including a \fCZ\fP and ending in an
  1833. \fCf\fP or there was an option string with a \fCZ\fP somewhere and the
  1834. previous word was any option string ending in an \fCf\fP.  If this is
  1835. the case, we need a compressed tar file.  Only if this is not the case
  1836. the second pattern set will be considered.  By the way,
  1837. \fCR[\fIpattern1\fC,\fIpattern2\fC]\fR is just like
  1838. \fCr[\fR...\fC,\fR...\fC]\fR except that it uses pattern matching with
  1839. shell metacharacters instead of just strings.
  1840. .PP
  1841. You will have noticed the \fC--\fP before the command name.  This ends
  1842. the list of pattern/flag pairs of \fC-x\fP.  It is usually used just
  1843. before the command name, but you can also use an extended completion
  1844. as one part of a list of xored completions, in which case the \fC--\fP
  1845. appears just before one of the \fC+\fP signs.
  1846. .PP
  1847. Note the difference between using extended completion as part of a
  1848. list of xored completions as in
  1849. .Ds
  1850. % ls
  1851. foo  bar
  1852. % compctl -x 'r[-d,---]' -g '*.Z' -- + -g '^*.Z' compress
  1853. % compress -d f\fITAB\fP
  1854. % compress -d foo _
  1855. .De
  1856. and specifying something before the \fC-x\fP as in
  1857. .Ds
  1858. % compctl -g '^*.Z' -x 'r[-d,---]' -g '*.Z' -- compress
  1859. % compress -d f\fITAB\fP
  1860. % compress -d f_
  1861. .De
  1862. In the first case, the alternative glob pattern (\fC^*.Z\fP) will be
  1863. used if the first part does not generate any possible completions,
  1864. while in the second case the alternative glob pattern will only be
  1865. used if the \fCr[\fR...\fC]\fR pattern doesn't match.
  1866. .Sh "Bindings"
  1867. .PP
  1868. Each of the editor commands we have seen was actually a function bound
  1869. by default to a certain key.  The real names of the commands are:
  1870. .Ds
  1871. \fCexpand-or-complete   \fITAB\fR
  1872. \fCpush-line            \fIESC-Q\fR
  1873. \fCrun-help             \fIESC-H\fR
  1874. \fCaccept-and-hold      \fIESC-A\fR
  1875. \fCquote-line           \fIESC-'\fR
  1876. .De
  1877. These bindings are arbitrary; you could change them if you want.
  1878. For example, to bind \fCaccept-line\fP to \fI^Z\fP:
  1879. .Ds
  1880. % bindkey '^Z' accept-line
  1881. .De
  1882. Another idea would be to bind the delete key to \fCdelete-char\fP;
  1883. this might be convenient if you use \fI^H\fP for backspace.
  1884. .Ds
  1885. % bindkey '^?' delete-char
  1886. .De
  1887. Or, you could bind \fI^X\fP\fI^H\fP to \fCrun-help\fP:
  1888. .Ds
  1889. % bindkey '^X^H' run-help
  1890. .De
  1891. Other examples:
  1892. .Ds
  1893. % bindkey '^X^Z' universal-argument
  1894. % bindkey ' ' magic-space
  1895. % bindkey -s '^T' 'uptime
  1896. > '
  1897. % bindkey '^Q' push-line-or-edit
  1898. .De
  1899. \fCuniversal-argument\fP multiplies the next command by 4.
  1900. Thus \fI^X\fP\fI^Z\fP\fI^W\fP might delete the last four words on the line.
  1901. If you bind space to \fCmagic-space\fP, then csh-style history
  1902. expansion is done on the line whenever you press the space bar.
  1903. .PP
  1904. Something that often happens is that I am typing a multiline command
  1905. and discover an error in one of the previous lines.  In this case,
  1906. \fCpush-line-or-edit\fP will put the entire multiline construct into
  1907. the editor buffer.  If there is only a single line, it is equivalent
  1908. to \fCpush-line\fP.
  1909. .PP
  1910. The \fC-s\fP flag to \fCbindkey\fP specifies that you are binding the key
  1911. to a string, not a command.  Thus \fCbindkey -s '^T' 'uptime\en'\fP
  1912. lets you VMS lovers get the load average whenever you press \fI^T\fP.
  1913. .PP
  1914. If you have a NeXT keyboard, the one with the \fC|\fP and \fC\e\fP keys
  1915. very inconveniently placed, the following
  1916. bindings may come in handy:
  1917. .Ds
  1918. % bindkey -s '\ee/' '\e\e'
  1919. % bindkey -s '\ee=' '|'
  1920. .De
  1921. Now you can type \fIALT-/\fP to get a backslash, and \fIALT-=\fP to
  1922. get a vertical bar.  This only works inside \fBzsh\fP, of course;
  1923. \fCbindkey\fP has no effect on the key mappings inside \fCtalk\fP
  1924. or \fCmail\fP, etc.
  1925. .PP
  1926. Some people like to bind \fC^S\fP and \fC^Q\fP to editor commands.
  1927. Just binding these has no effect, as the terminal will catch them and
  1928. use them for flow control.  You could unset them as stop and start
  1929. characters, but most people like to use these for external commands.
  1930. The solution is to set the \fINOFLOWCONTROL\fP option.  This will
  1931. allow you to bind the start and stop characters to editor commands,
  1932. while retaining their normal use for external commands.
  1933. .Sh "Parameter Substitution"
  1934. .PP
  1935. In \fBzsh\fP, parameters are set like this:
  1936. .Ds
  1937. % foo=bar
  1938. % echo $foo
  1939. bar
  1940. .De
  1941. Spaces before or after the \fC=\fP are frowned upon:
  1942. .Ds
  1943. % foo = bar
  1944. zsh: command not found: foo
  1945. .De
  1946. Also, \fCset\fP doesn't work for setting parameters:
  1947. .Ds
  1948. % set foo=bar
  1949. % set foo = bar
  1950. % echo $foo
  1951.  
  1952. %
  1953. .De
  1954. Note that no error message was printed.  This is because both
  1955. of these commands were perfectly valid; the \fCset\fP builtin
  1956. assigns its arguments to the \fIpositional parameters\fP
  1957. (\fC$1\fP, \fC$2\fP, etc.).
  1958. .Ds
  1959. % set foo=bar
  1960. % echo $1
  1961. foo=bar
  1962. % set foo = bar
  1963. % echo $3 $2
  1964. bar =
  1965. .De
  1966. If you're really intent on using the csh syntax, define a
  1967. function like this:
  1968. .Ds
  1969. % set () {
  1970. >    eval "$1$2$3"
  1971. > }
  1972. % set foo = bar
  1973. % set fuu=brrr
  1974. % echo $foo $fuu
  1975. bar brrr
  1976. .De
  1977. But then, of course you can't use the form of \fCset\fP with
  1978. options, like \fCset -F\fP (which turns off filename generation).
  1979. Also, the \fCset\fP command by itself won't list all the parameters
  1980. like it should.
  1981. To get around that you need a \fCcase\fP statement:
  1982. .Ds
  1983. % set () {
  1984. >    case $1 in
  1985. >    -*|+*|'') builtin set $* ;;
  1986. >    *) eval "$1$2$3" ;;
  1987. >    esac
  1988. > }
  1989. .De
  1990. For the most part, this should make csh users happy.
  1991. .PP
  1992. The following sh-style operators are supported in \fBzsh\fP:
  1993. .Ds
  1994. % unset null
  1995. % echo ${foo-xxx}
  1996. bar
  1997. % echo ${null-xxx}
  1998. xxx
  1999. % unset null
  2000. % echo ${null=xxx}
  2001. xxx
  2002. % echo $null
  2003. xxx
  2004. % echo ${foo=xxx}
  2005. bar
  2006. % echo $foo
  2007. bar
  2008. % unset null
  2009. % echo ${null+set}
  2010.  
  2011. % echo ${foo+set}
  2012. set
  2013. .De
  2014. Also, csh-style \fC:\fP modifiers may be appended to a parameter
  2015. substitution.
  2016. .Ds
  2017. % echo $PWD
  2018. /home/learning/pf/zsh/zsh2.00/src
  2019. % echo $PWD:h
  2020. /home/learning/pf/zsh/zsh2.00
  2021. % echo $PWD:h:h
  2022. /home/learning/pf/zsh
  2023. % echo $PWD:t
  2024. src
  2025. % name=foo.c
  2026. % echo $name
  2027. foo.c
  2028. % echo $name:r
  2029. foo
  2030. % echo $name:e
  2031. c
  2032. .De
  2033. The equivalent constructs in ksh (which are also supported in \fBzsh\fP)
  2034. are a bit more general and easier to remember.
  2035. When the shell expands \fC${foo#\fR\fIpat\fR\fC}\fR,
  2036. it checks to see if \fIpat\fP matches a substring at the beginning
  2037. of the value
  2038. of \fCfoo\fP.  If so, it removes that portion of \fCfoo\fP, using the shortest
  2039. possible match.
  2040. With \fC${foo##\fR\fIpat\fR\fC}\fR, the longest possible match is removed.
  2041. \fC${foo%\fR\fIpat\fR\fC}\fR and \fC${foo%%\fR\fIpat\fR\fC}\fR remove the match
  2042. from the end.
  2043. Here are the ksh equivalents of the \fC:\fP modifiers:
  2044. .Ds
  2045. % echo ${PWD%/*}
  2046. /home/learning/pf/zsh/zsh2.00
  2047. % echo ${PWD%/*/*}
  2048. /home/learning/pf/zsh
  2049. % echo ${PWD##*/}
  2050. src
  2051. % echo ${name%.*}
  2052. foo
  2053. % echo ${name#*.}
  2054. c
  2055. .De
  2056. \fBzsh\fP also has upper/lowercase modifiers:
  2057. .Ds
  2058. % xx=Test
  2059. % echo $xx:u
  2060. TEST
  2061. % echo $xx:l
  2062. test
  2063. .De
  2064. and a substitution modifier:
  2065. .Ds
  2066. % echo $name:s/foo/bar/
  2067. bar.c
  2068. % ls
  2069. foo.c    foo.h    foo.o    foo.pro
  2070. % for i in foo.*; mv $i $i:s/foo/bar/
  2071. % ls
  2072. bar.c    bar.h    bar.o    bar.pro
  2073. .De
  2074. There is yet another syntax to modify substituted parameters.  You can
  2075. add certain modifiers in parentheses after the opening brace like:
  2076. .Ds
  2077. ${(\fImodifiers\fC)\fIparameter\fC}
  2078. .De
  2079. For example, \fCo\fP sorts the words resulting from the expansion:
  2080. .Ds
  2081. % echo ${path}
  2082. /usr/bin /usr/bin/X11 /etc
  2083. % echo ${(o)path}
  2084. /etc /usr/bin /usr/bin/X11
  2085. .De
  2086. One possible source of confusion is the fact that in \fBzsh\fP,
  2087. the result of parameter substitution is \fInot\fP split into
  2088. words.  Thus, this will not work:
  2089. .Ds
  2090. % srcs='glob.c exec.c init.c'
  2091. % ls $srcs
  2092. glob.c exec.c init.c not found
  2093. .De
  2094. This is considered a feature, not a bug.
  2095. If splitting were done by default, as it is in most other shells,
  2096. functions like this would not work properly:
  2097. .Ds
  2098. $ ll () { ls -F $* }
  2099. $ ll 'fuu bar'
  2100. fuu not found
  2101. bar not found
  2102.  
  2103. % ll 'fuu bar'
  2104. fuu bar not found
  2105. .De
  2106. Of course, a hackish workaround is available in sh (and \fBzsh\fP):
  2107. .Ds
  2108. % setopt shwordsplit
  2109. % ll () { ls -F "$@" }
  2110. % ll 'fuu bar'
  2111. fuu bar not found
  2112. .De
  2113. If you like the sh behaviour, \fBzsh\fP can accomodate you:
  2114. .Ds
  2115. % ls ${=srcs}
  2116. exec.c  glob.c  init.c
  2117. % setopt shwordsplit
  2118. % ls $srcs
  2119. exec.c  glob.c  init.c
  2120. .De
  2121. Another way to get the \fC$srcs\fP trick to work is to use an array:
  2122. .Ds
  2123. % unset srcs
  2124. % srcs=( glob.c exec.c init.c )  
  2125. % ls $srcs
  2126. exec.c  glob.c  init.c
  2127. .De
  2128. or an alias:
  2129. .Ds
  2130. % alias -g SRCS='exec.c glob.c init.c'
  2131. % ls SRCS
  2132. exec.c  glob.c  init.c
  2133. .De
  2134. Another option that modifies parameter expansion is
  2135. \fIRCEXPANDPARAM\fP:
  2136. .Ds
  2137. % echo foo/$srcs
  2138. foo/glob.c exec.c init.c
  2139. % setopt rcexpandparam
  2140. % echo foo/$srcs
  2141. foo/glob.c foo/exec.c foo/init.c
  2142. % echo foo/${^srcs}
  2143. foo/glob.c foo/exec.c foo/init.c
  2144. % echo foo/$^srcs
  2145. foo/glob.c foo/exec.c foo/init.c
  2146. .De
  2147. .Sh "Shell Parameters"
  2148. .PP
  2149. The shell has many predefined parameters that may be
  2150. accessed.  Here are some examples:
  2151. .Ds
  2152. % sleep 10 &
  2153. [1] 3820
  2154. % echo $!
  2155. 3820
  2156. % set a b c
  2157. % echo $#
  2158. 3
  2159. % echo $ARGC
  2160. 3
  2161. % ( exit 20 ) ; echo $?
  2162. 20
  2163. % false; echo $status
  2164. 1
  2165. .De
  2166. (\fC$?\fP and \fC$status\fP are equivalent.)
  2167. .Ds
  2168. % echo $HOST $HOSTTYPE
  2169. dendrite sun4
  2170. % echo $UID $GID
  2171. 701 60
  2172. % cd /tmp
  2173. % cd /home
  2174. % echo $PWD $OLDPWD
  2175. /home /tmp
  2176. % ls $OLDPWD/.getwd 
  2177. /tmp/.getwd
  2178. .De
  2179. \fC~+\fP and \fC~-\fP are short for \fC$PWD\fP and \fC$OLDPWD\fP, respectively.
  2180. .Ds
  2181. % ls ~-/.getwd
  2182. /tmp/.getwd
  2183. % ls -d ~+/learning
  2184. /home/learning
  2185. % echo $RANDOM
  2186. 4880
  2187. % echo $RANDOM
  2188. 11785
  2189. % echo $RANDOM
  2190. 2062
  2191. % echo $TTY
  2192. /dev/ttyp4
  2193. % echo $VERSION
  2194. zsh v2.00.03
  2195. % echo $USERNAME
  2196. pf
  2197. .De
  2198. .PP
  2199. The \fCcdpath\fP variable sets the search path for the \fCcd\fP command.
  2200. If you do not specify \fC.\fP somewhere in the path, it is assumed to
  2201. be the first component.
  2202. .Ds
  2203. % cdpath=( /usr ~ ~/zsh )
  2204. % ls /usr
  2205. 5bin         dict         lang         net          sccs         sys
  2206. 5include     etc          lector       nserve       services     tmp
  2207. 5lib         export       lib          oed          share        ucb
  2208. adm          games        local        old          skel         ucbinclude
  2209. bin          geac         lost+found   openwin      spool        ucblib
  2210. boot         hosts        macsyma_417  pat          src          xpg2bin
  2211. demo         include      man          princeton    stand        xpg2include
  2212. diag         kvm          mdec         pub          swap         xpg2lib
  2213. % cd spool
  2214. /usr/spool
  2215. % cd bin
  2216. /usr/bin
  2217. % cd func
  2218. ~/func
  2219. % cd 
  2220. % cd pub
  2221. % pwd
  2222. /u/pfalstad/pub
  2223. % ls -d /usr/pub
  2224. /usr/pub
  2225. .De
  2226. \fBPATH\fP and \fBpath\fP both set the search path for commands.
  2227. These two variables are equivalent, except that one is a string
  2228. and one is an array.  If the user modifies \fBPATH\fP, the shell
  2229. changes \fBpath\fP as well, and vice versa.
  2230. .Ds
  2231. % PATH=/bin:/usr/bin:/tmp:.
  2232. % echo $path
  2233. /bin /usr/bin /tmp .
  2234. % path=( /usr/bin . /usr/local/bin /usr/ucb )
  2235. % echo $PATH
  2236. /usr/bin:.:/usr/local/bin:/usr/ucb
  2237. .De
  2238. The same is true of \fBCDPATH\fP and \fBcdpath\fP:
  2239. .Ds
  2240. % echo $CDPATH
  2241. /usr:/u/pfalstad:/u/pfalstad/zsh
  2242. % CDPATH=/u/subbarao:/usr/src:/tmp
  2243. % echo $cdpath
  2244. /u/subbarao /usr/src /tmp
  2245. .De
  2246. In general, predefined parameters with names in all lowercase are
  2247. arrays; assignments to them take the form:
  2248. .Ds
  2249. \fIname\fR\fC=(\fR \fIelem\fR ...\ \fC)\fR
  2250. .De
  2251. Predefined parameters with names in all uppercase are strings.  If
  2252. there is both an array and a string version of the same parameter, the
  2253. string version is a colon-separated list, like \fBPATH\fP.
  2254. .PP
  2255. \fBHISTFILE\fP is the name of the history file, where the history
  2256. is saved when a shell exits.
  2257. .Ds
  2258. % zsh
  2259. phoenix% HISTFILE=/tmp/history
  2260. phoenix% SAVEHIST=20
  2261. phoenix% echo foo
  2262. foo
  2263. phoenix% date
  2264. Fri May 24 05:39:35 EDT 1991
  2265. phoenix% uptime
  2266.   5:39am  up 4 days, 20:02,  40 users,  load average: 2.30, 2.20, 2.00
  2267. phoenix% exit
  2268. % cat /tmp/history
  2269. HISTFILE=/tmp/history
  2270. SAVEHIST=20
  2271. echo foo
  2272. date
  2273. uptime
  2274. exit
  2275. % HISTSIZE=3
  2276. % history
  2277.    28  rm /tmp/history
  2278.    29  HISTSIZE=3
  2279.    30  history
  2280. .De
  2281. If you have several incantations of \fBzsh\fP running at the same
  2282. time, like when using the X window system, it might be preferable to
  2283. append the history of each shell to a file when a shell exits instead
  2284. of overwriting the old contents of the file.  You can get this
  2285. behaviour by setting the \fIAPPENDHISTORY\fP option.
  2286. .PP
  2287. In \fBzsh\fP, if you say
  2288. .Ds
  2289. % >file
  2290. .De
  2291. the command \fCcat\fP is normally assumed:
  2292. .Ds
  2293. % >file
  2294. foo!    
  2295. ^D
  2296. % cat file
  2297. foo!
  2298. .De
  2299. Thus, you can view a file simply by typing:
  2300. .Ds
  2301. % <file
  2302. foo!
  2303. .De
  2304. However, this is not csh or sh compatible.  To correct this,
  2305. change the value of the parameter \fBNULLCMD\fP,
  2306. which is \fCcat\fP by default.
  2307. .Ds
  2308. % NULLCMD=:
  2309. % >file
  2310. % ls -l file
  2311. -rw-r--r--  1 pfalstad        0 May 24 05:41 file
  2312. .De
  2313. If \fCNULLCMD\fP is unset, the shell reports an error if no
  2314. command is specified (like csh).
  2315. .Ds
  2316. % unset NULLCMD
  2317. % >file
  2318. zsh: redirection with no command
  2319. .De
  2320. Actually, \fBREADNULLCMD\fP is used whenever you have a null command
  2321. reading input from a single file.  Thus, you can set \fBREADNULLCMD\fP
  2322. to \fCmore\fP or \fCless\fP rather than \fCcat\fP.  Also, if you
  2323. set \fBNULLCMD\fP to \fC:\fP for sh compatibility, you can still read
  2324. files with \fC< file\fP if you leave \fBREADNULLCMD\fP set to \fCmore\fP.
  2325. .Sh "Prompting"
  2326. .PP
  2327. The default prompt for \fBzsh\fP is:
  2328. .Ds
  2329. phoenix% echo $PROMPT
  2330. %m%# 
  2331. .De
  2332. The \fC%m\fP stands for the short form of the current hostname,
  2333. and the \fC%#\fP stands for a \fC%\fP or a \fC#\fP, depending on whether
  2334. the shell is running as root or not.
  2335. \fBzsh\fP supports many other control sequences
  2336. in the \fBPROMPT\fP variable.
  2337. .Ds
  2338. % PROMPT='%/> '
  2339. /u/pfalstad/etc/TeX/zsh>
  2340.  
  2341. % PROMPT='%~> '   
  2342. ~/etc/TeX/zsh> 
  2343.  
  2344. % PROMPT='%h %~> '
  2345. 6 ~/etc/TeX/zsh> 
  2346. .De
  2347. \fC%h\fP represents the number of current history event.
  2348. .Ds
  2349. % PROMPT='%h %~ %M> '
  2350. 10 ~/etc/TeX/zsh apple-gunkies.gnu.ai.mit.edu> 
  2351.  
  2352. % PROMPT='%h %~ %m> '
  2353. 11 ~/etc/TeX/zsh apple-gunkies> 
  2354.  
  2355. % PROMPT='%h %t> '
  2356. 12 6:11am> 
  2357.  
  2358. % PROMPT='%n %w tty%l>'
  2359. pfalstad Fri 24 ttyp0>
  2360. .De
  2361. \fBPROMPT2\fP is used in multiline commands, like for-loops.  The
  2362. \fC%_\fP escape sequence was made especially for this prompt.  It is
  2363. replaced by the kind of command that is being entered.
  2364. .Ds
  2365. % PROMPT2='%_> '
  2366. % for i in foo bar
  2367. for>
  2368.  
  2369. % echo 'hi
  2370. quote>
  2371. .De
  2372. Also available is the \fBRPROMPT\fP parameter.
  2373. If this is set, the shell puts a prompt on the \fIright\fP side
  2374. of the screen.
  2375. .Ds
  2376. % RPROMPT='%t'
  2377. %                                                      6:14am
  2378.  
  2379. % RPROMPT='%~'
  2380. %                                               ~/etc/TeX/zsh
  2381.  
  2382. % PROMPT='%l %T %m[%h] ' RPROMPT=' %~'
  2383. p0 6:15 phoenix[5]                              ~/etc/TeX/zsh
  2384. .De
  2385. These special escape sequences can also be used with the
  2386. \fC-P\fP option to \fCprint\fP:
  2387. .Ds
  2388. % print -P %h tty%l
  2389. 15 ttyp1
  2390. .De
  2391. .PP
  2392. The \fBPOSTEDIT\fP parameter is printed whenever the editor exits.
  2393. This can be useful for termcap tricks.  To highlight the prompt
  2394. and command line while leaving command output unhighlighted, try this:
  2395. .Ds
  2396. % POSTEDIT=`echotc se`
  2397. % PROMPT='%S%% '
  2398. .De
  2399. .Sh "Login/logout watching"
  2400. .PP
  2401. You can specify login or logout events to monitor
  2402. by setting the \fBwatch\fP variable.
  2403. Normally, this is done by specifying a list of usernames.
  2404. .Ds
  2405. % watch=( pfalstad subbarao sukthnkr egsirer )
  2406. .De
  2407. The \fClog\fP command reports all people logged in
  2408. that you are watching for.
  2409. .Ds
  2410. % log
  2411. pfalstad has logged on p0 from mickey.
  2412. pfalstad has logged on p5 from mickey.
  2413. % \fR...\fC
  2414. subbarao has logged on p8 from phoenix.
  2415. % \fR...\fC
  2416. subbarao has logged off p8 from phoenix.
  2417. % \fR...\fC
  2418. sukthnkr has logged on p8 from dew.
  2419. % \fR...\fC
  2420. sukthnkr has logged off p8 from dew.
  2421. .De
  2422. If you specify hostnames with an \fC@\fP prepended,
  2423. the shell will watch for all users logging in from
  2424. the specified host.
  2425. .Ds
  2426. % watch=( @mickey @phoenix )
  2427. % log
  2428. djthongs has logged on q2 from phoenix.
  2429. pfalstad has logged on p0 from mickey.
  2430. pfalstad has logged on p5 from mickey.
  2431. .De
  2432. If you give a tty name with a \fC%\fP prepended, the shell
  2433. will watch for all users logging in on that tty.
  2434. .Ds
  2435. % watch=( %ttyp0 %console )
  2436. % log
  2437. root has logged on console from .
  2438. pfalstad has logged on p0 from mickey.
  2439. .De
  2440. The format of the reports may also be changed.
  2441. .Ds
  2442. % watch=( pfalstad gettes eps djthongs jcorr bdavis )
  2443. % log
  2444. jcorr has logged on tf from 128.112.176.3:0.
  2445. jcorr has logged on r0 from 128.112.176.3:0.
  2446. gettes has logged on p4 from yo:0.0.
  2447. djthongs has logged on pe from grumpy:0.0.
  2448. djthongs has logged on q2 from phoenix.
  2449. bdavis has logged on qd from BRUNO.
  2450. eps has logged on p3 from csx30:0.0.
  2451. pfalstad has logged on p0 from mickey.
  2452. pfalstad has logged on p5 from mickey.
  2453. % WATCHFMT='%n on tty%l from %M'
  2454. % log
  2455. jcorr on ttytf from 128.112.176.3:0.
  2456. jcorr on ttyr0 from 128.112.176.3:0.
  2457. gettes on ttyp4 from yo:0.0
  2458. djthongs on ttype from grumpy:0.0
  2459. djthongs on ttyq2 from phoenix.Princeto
  2460. bdavis on ttyqd from BRUNO.pppl.gov
  2461. eps on ttyp3 from csx30:0.0
  2462. pfalstad on ttyp0 from mickey.Princeton
  2463. pfalstad on ttyp5 from mickey.Princeton
  2464. % WATCHFMT='%n fm %m'
  2465. % log
  2466. jcorr fm 128.112.176.3:0
  2467. jcorr fm 128.112.176.3:0
  2468. gettes fm yo:0.0
  2469. djthongs fm grumpy:0.0
  2470. djthongs fm phoenix
  2471. bdavis fm BRUNO
  2472. eps fm csx30:0.0
  2473. pfalstad fm mickey
  2474. pfalstad fm mickey
  2475. % WATCHFMT='%n %a at %t %w.'
  2476. % log
  2477. jcorr logged on at 3:15pm Mon 20.
  2478. jcorr logged on at 3:16pm Wed 22.
  2479. gettes logged on at 6:54pm Wed 22.
  2480. djthongs logged on at 7:19am Thu 23.
  2481. djthongs logged on at 7:20am Thu 23.
  2482. bdavis logged on at 12:40pm Thu 23.
  2483. eps logged on at 4:19pm Thu 23.
  2484. pfalstad logged on at 3:39am Fri 24.
  2485. pfalstad logged on at 3:42am Fri 24.
  2486. .De
  2487. If you have a \fC.friends\fP file in your home directory,
  2488. a convenient way to make \fBzsh\fP watch for all your friends
  2489. is to do this:
  2490. .Ds
  2491. % watch=( $(< ~/.friends) )
  2492. % echo $watch
  2493. subbarao maruchck root sukthnkr \fR...
  2494. .De
  2495. If watch is set to \fCall\fP, then all users logging in or out
  2496. will be reported.
  2497. .Sh "Options"
  2498. .PP
  2499. Some options have already been mentioned; here are a few more:
  2500. .PP
  2501. Using the \fIAUTOCD\fP option, you can simply type the name
  2502. of a directory, and it will become the current directory.
  2503. .Ds
  2504. % cd /
  2505. % setopt autocd
  2506. % bin
  2507. % pwd
  2508. /bin
  2509. % ../etc
  2510. % pwd
  2511. /etc
  2512. .De
  2513. With \fICDABLEVARS\fP, if the argument to \fCcd\fP is the name of a
  2514. parameter whose value is a valid directory, it will become
  2515. the current directory.
  2516. .Ds
  2517. % setopt cdablevars
  2518. % foo=/tmp
  2519. % cd foo
  2520. /tmp
  2521. .De
  2522. \fICORRECT\fP turns on spelling correction for commands,
  2523. and the \fICORRECTALL\fP option turns on spelling correction
  2524. for all arguments.
  2525. .Ds
  2526. % setopt correct
  2527. % sl
  2528. zsh: correct `sl' to `ls' [nyae]? y
  2529. % setopt correctall
  2530. % ls x.v11r4
  2531. zsh: correct `x.v11r4' to `X.V11R4' [nyae]? n
  2532. /usr/princton/src/x.v11r4 not found
  2533. % ls /etc/paswd
  2534. zsh: correct to `/etc/paswd' to `/etc/passwd' [nyae]? y
  2535. /etc/passwd
  2536. .De
  2537. If you press \fCy\fP
  2538. when the shell asks you if you want to correct a word, it will
  2539. be corrected.  If you press \fCn\fP, it will be left alone.
  2540. Pressing \fCa\fP aborts the command, and pressing \fCe\fP brings the line
  2541. up for editing again, in case you agree the word is spelled wrong
  2542. but you don't like the correction.
  2543. .PP
  2544. Normally, a quoted expression may contain a newline:
  2545. .Ds
  2546. % echo '
  2547. > foo
  2548. > '
  2549.  
  2550. foo
  2551.  
  2552. %
  2553. .De
  2554. With \fICSHJUNKIEQUOTES\fP set, this is illegal, as it is
  2555. in csh.
  2556. .Ds
  2557. % setopt cshjunkiequotes
  2558. % ls 'foo
  2559. zsh: unmatched '
  2560. .De
  2561. \fIGLOBDOTS\fP lets files beginning with a \fC.\fP be matched without
  2562. explicitly specifying the dot.
  2563. .Ds
  2564. % ls -d *x*
  2565. Mailboxes
  2566. % setopt globdots
  2567. % ls -d *x*
  2568. \&.exrc         .pnewsexpert  .xserverrc
  2569. \&.mushexpert   .xinitrc      Mailboxes
  2570. .De
  2571. \fIHISTIGNOREDUPS\fP prevents the current line from being
  2572. saved in the history if it is the same as the previous one;
  2573. \fIHISTIGNORESPACE\fP prevents the current line from being
  2574. saved if it begins with a space.
  2575. .Ds
  2576. % PROMPT='%h> '
  2577. 39> setopt histignoredups
  2578. 40> echo foo
  2579. foo
  2580. 41> echo foo
  2581. foo
  2582. 41> echo foo
  2583. foo
  2584. 41> echo bar
  2585. bar
  2586. 42> setopt histignorespace
  2587. 43>  echo foo
  2588. foo
  2589. 43>  echo fubar
  2590. fubar
  2591. 43>  echo fubar
  2592. fubar
  2593. .De
  2594. \fIIGNOREBRACES\fP turns off csh-style brace expansion.
  2595. .Ds
  2596. % echo x{y{z,a},{b,c}d}e
  2597. xyze xyae xbde xcde
  2598. % setopt ignorebraces
  2599. % echo x{y{z,a},{b,c}d}e
  2600. x{y{z,a},{b,c}d}e
  2601. .De
  2602. \fIIGNOREEOF\fP forces the user to type \fCexit\fP or \fClogout\fP,
  2603. instead of just pressing \fI^D\fP.
  2604. .Ds
  2605. % setopt ignoreeof
  2606. % ^D
  2607. zsh: use 'exit' to exit.
  2608. .De
  2609. \fIINTERACTIVECOMMENTS\fP turns on interactive comments;
  2610. comments begin with a \fC#\fP.
  2611. .Ds
  2612. % setopt interactivecomments
  2613. % date # this is a comment
  2614. Fri May 24 06:54:14 EDT 1991
  2615. .De
  2616. \fINOBEEP\fP makes sure the shell never beeps.
  2617. .PP
  2618. \fINOCLOBBER\fP prevents you from accidentally
  2619. overwriting an existing file.
  2620. .Ds
  2621. % setopt noclobber
  2622. % cat /dev/null >~/.zshrc
  2623. zsh: file exists: /u/pfalstad/.zshrc
  2624. .De
  2625. If you really do want to clobber a file, you can use the
  2626. \fC>!\fP operator.
  2627. To make things easier in this case, the \fC>\fP is stored in
  2628. the history list as a \fC>!\fP:
  2629. .Ds
  2630. % cat /dev/null >! ~/.zshrc
  2631. % cat /etc/motd > ~/.zshrc
  2632. zsh: file exists: /u/pfalstad/.zshrc
  2633. % !!
  2634. cat /etc/motd >! ~/.zshrc
  2635. % \fR...
  2636. .De
  2637. \fIRCQUOTES\fP lets you use a more elegant method for including
  2638. single quotes in a singly quoted string:
  2639. .Ds
  2640. % echo '"don'\e''t do that."'
  2641. "don't do that."
  2642. % echo '"don''t do that."'
  2643. "dont do that."
  2644. % setopt rcquotes
  2645. % echo '"don''t do that."'
  2646. "don't do that."
  2647. .De
  2648. Finally,
  2649. \fISUNKEYBOARDHACK\fP wins the award for the strangest option.
  2650. If a line ends with \fC`\fP, and there are an odd number of them
  2651. on the line, the shell will ignore the trailing \fC`\fP.  This
  2652. is provided for keyboards whose RETURN key is too small,
  2653. and too close to the \fC`\fP key.
  2654. .Ds
  2655. % setopt sunkeyboardhack
  2656. % date`
  2657. Fri May 24 06:55:38 EDT 1991
  2658. .De
  2659. .Sh "Closing Comments"
  2660. .PP
  2661. I (Bas de Bakker) would be happy to receive mail if anyone has any
  2662. tricks or ideas to add to this document, or if there are some points
  2663. that could be made clearer or covered more thoroughly.  Please notify
  2664. me of any errors in this document.
  2665. .bp
  2666. .PX
  2667.