home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume35 / zsh / part03 < prev    next >
Text File  |  1993-02-20  |  57KB  |  2,439 lines

  1. Newsgroups: comp.sources.misc
  2. From: zsh-list@cs.uow.edu.au (The Zsh Mailing List)
  3. Subject: v35i053:  zsh - The Z Shell, version 2.3.1, Part03/22
  4. Message-ID: <1993Feb20.212032.28166@sparky.imd.sterling.com>
  5. X-Md4-Signature: cbf11e1334e0785c3220a1644257cb50
  6. Date: Sat, 20 Feb 1993 21:20:32 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: zsh-list@cs.uow.edu.au (The Zsh Mailing List)
  10. Posting-number: Volume 35, Issue 53
  11. Archive-name: zsh/part03
  12. Environment: UNIX
  13. Supersedes: zsh2.2: Volume 29, Issue 97-113
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  20. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  21. # Contents:  doc/intro.txt.01 help/autoload src/Makefile.sample
  22. # Wrapped by mattson@odin on Sat Feb  6 14:41:51 1993
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. echo If this archive is complete, you will see the following message:
  25. echo '          "shar: End of archive 3 (of 22)."'
  26. if test -f 'doc/intro.txt.01' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'doc/intro.txt.01'\"
  28. else
  29.   echo shar: Extracting \"'doc/intro.txt.01'\" \(49646 characters\)
  30.   sed "s/^X//" >'doc/intro.txt.01' <<'END_OF_FILE'
  31. X
  32. X
  33. X
  34. X
  35. X
  36. X
  37. X               An Introduction to the Z Shell
  38. X
  39. X
  40. X                        Paul Falstad
  41. X                       pf@ttisms.com
  42. X
  43. X
  44. X
  45. X
  46. X
  47. X
  48. Xzsh is a shell designed for interactive use, although it  is
  49. Xalso  a  powerful  scripting  language.   Many of the useful
  50. Xfeatures of bash, ksh, and tcsh were incorporated into  zsh;
  51. Xmany  original  features  were added.  This document details
  52. Xsome of the  unique  features  of  zsh.   It  assumes  basic
  53. Xknowledge of the standard UNIX shells; the intent is to show
  54. Xa reader already familiar with one of the other major shells
  55. Xwhat  makes zsh more useful or more powerful.  This document
  56. Xis not at all comprehensive; read the  manual  entry  for  a
  57. Xdescription  of  the  shell  that  is  complete and concise,
  58. Xalthough somewhat overwhelming and devoid of examples.
  59. X
  60. XFilename Generation
  61. X
  62. XOtherwise known as globbing, filename  generation  is  quite
  63. Xextensive in zsh.  Of course, it has all the basics:
  64. X
  65. X% ls
  66. XMakefile   file.pro   foo.o      main.o     q.c        run234     stuff
  67. Xbar.o      foo        link       morestuff  run123     run240     sub
  68. Xfile.h     foo.c      main.h     pipe       run2       run303
  69. X% ls *.c
  70. Xfoo.c  q.c
  71. X% ls *.[co]
  72. Xbar.o   foo.c   foo.o   main.o  q.c
  73. X% ls foo.?
  74. Xfoo.c  foo.o
  75. X% ls *.[^c]
  76. Xbar.o   file.h  foo.o   main.h  main.o
  77. X% ls *.[^oh]
  78. Xfoo.c  q.c
  79. X
  80. X
  81. XAlso, if the EXTENDEDGLOB option is set, some  new  features
  82. Xare  activated.   For  example,  the ^ character negates the
  83. Xpattern following it:
  84. X
  85. X
  86. X
  87. X
  88. X
  89. X
  90. X
  91. X
  92. X
  93. X
  94. X
  95. X
  96. X
  97. X
  98. X
  99. X                           - 2 -
  100. X
  101. X% setopt extendedglob
  102. X% ls -d ^*.c
  103. XMakefile   file.pro   link       morestuff  run2       run303
  104. Xbar.o      foo        main.h     pipe       run234     stuff
  105. Xfile.h     foo.o      main.o     run123     run240     sub
  106. X% ls -d ^*.*
  107. XMakefile   link       pipe       run2       run240     stuff
  108. Xfoo        morestuff  run123     run234     run303     sub
  109. X% ls -d ^Makefile
  110. Xbar.o      foo        link       morestuff  run123     run240     sub
  111. Xfile.h     foo.c      main.h     pipe       run2       run303
  112. Xfile.pro   foo.o      main.o     q.c        run234     stuff
  113. X% ls -d *.^c
  114. X.rhosts   bar.o     file.h    file.pro  foo.o     main.h    main.o
  115. X
  116. X
  117. XAn expression of the form <x-y> matches a range of integers:
  118. X
  119. X% ls run<200-300>
  120. Xrun234  run240
  121. X% ls run<300-400>
  122. Xrun303
  123. X% ls run<-200>
  124. Xrun123  run2
  125. X% ls run<300->
  126. Xrun303
  127. X% ls run<>
  128. Xrun123  run2    run234  run240  run303
  129. X
  130. X
  131. XGrouping is possible:
  132. X
  133. X% ls (foo|bar).*
  134. Xbar.o  foo.c  foo.o
  135. X% ls *.(c|o|pro)
  136. Xbar.o     file.pro  foo.c     foo.o     main.o    q.c
  137. X
  138. X
  139. XAlso, the string **/ forces a  recursive  search  of  sub-
  140. Xdirectories:
  141. X
  142. X
  143. X
  144. X
  145. X
  146. X
  147. X
  148. X
  149. X
  150. X
  151. X
  152. X
  153. X
  154. X
  155. X
  156. X
  157. X
  158. X
  159. X
  160. X
  161. X
  162. X
  163. X
  164. X
  165. X                           - 3 -
  166. X
  167. X% ls -R
  168. XMakefile   file.pro   foo.o      main.o     q.c        run234     stuff
  169. Xbar.o      foo        link       morestuff  run123     run240     sub
  170. Xfile.h     foo.c      main.h     pipe       run2       run303
  171. X
  172. Xmorestuff:
  173. X
  174. Xstuff:
  175. Xfile  xxx   yyy
  176. X
  177. Xstuff/xxx:
  178. Xfoobar
  179. X
  180. Xstuff/yyy:
  181. Xfrobar
  182. X% ls **/*bar
  183. Xstuff/xxx/foobar  stuff/yyy/frobar
  184. X% ls **/f*
  185. Xfile.h            foo               foo.o             stuff/xxx/foobar
  186. Xfile.pro          foo.c             stuff/file        stuff/yyy/frobar
  187. X% ls *bar*
  188. Xbar.o
  189. X% ls **/*bar*
  190. Xbar.o             stuff/xxx/foobar  stuff/yyy/frobar
  191. X% ls stuff/**/*bar*
  192. Xstuff/xxx/foobar  stuff/yyy/frobar
  193. X
  194. X
  195. X
  196. XIt is possible to exclude certain files  from  the  patterns
  197. Xusing  the  ~  character.   A  pattern of the form *.c~bar.c
  198. Xlists all files matching *.c, except for the file bar.c.
  199. X
  200. X% ls *.c
  201. Xfoo.c    foob.c    bar.c
  202. X% ls *.c~bar.c
  203. Xfoo.c    foob.c
  204. X% ls *.c~f*
  205. Xbar.c
  206. X
  207. X
  208. X
  209. XOne can add a number of qualifiers to  the  end  of  any  of
  210. Xthese  patterns,  to restrict matches to certain file types.
  211. XA qualified pattern is of the form
  212. X
  213. X        pattern(...)
  214. X
  215. X
  216. Xwith single-letter qualifiers inside the parentheses.
  217. X
  218. X
  219. X
  220. X
  221. X
  222. X
  223. X
  224. X
  225. X
  226. X
  227. X
  228. X
  229. X
  230. X
  231. X                           - 4 -
  232. X
  233. X% alias l='ls -dF'
  234. X% l *
  235. XMakefile    foo*        main.h      q.c         run240
  236. Xbar.o       foo.c       main.o      run123      run303
  237. Xfile.h      foo.o       morestuff/  run2        stuff/
  238. Xfile.pro    link@       pipe        run234      sub
  239. X% l *(/)
  240. Xmorestuff/  stuff/
  241. X% l *(@)
  242. Xlink@
  243. X% l *(*)
  244. Xfoo*        link@       morestuff/  stuff/
  245. X% l *(x)
  246. Xfoo*        link@       morestuff/  stuff/
  247. X% l *(X)
  248. Xfoo*        link@       morestuff/  stuff/
  249. X% l *(R)
  250. Xbar.o       foo*        link@       morestuff/  run123      run240
  251. Xfile.h      foo.c       main.h      pipe        run2        run303
  252. Xfile.pro    foo.o       main.o      q.c         run234      stuff/
  253. X
  254. X
  255. XNote that  *(x)  and  *(*)  both  match  executables.   *(X)
  256. Xmatches  files  executable  by  others,  as opposed to *(x),
  257. Xwhich matches files executable by the owner.  *(R) and  *(r)
  258. Xmatch  readable files; *(W) and *(w), which checks for writ-
  259. Xable files.  *(W) is especially important, since  it  checks
  260. Xfor world-writable files:
  261. X
  262. X% l *(w)
  263. Xbar.o       foo*        link@       morestuff/  run123      run240
  264. Xfile.h      foo.c       main.h      pipe        run2        run303
  265. Xfile.pro    foo.o       main.o      q.c         run234      stuff/
  266. X% l *(W)
  267. Xlink@   run240
  268. X% l -l link run240
  269. Xlrwxrwxrwx  1 pfalstad       10 May 23 18:12 link -> /bin/false*
  270. X-rw-rw-rw-  1 pfalstad        0 May 23 18:12 run240
  271. X
  272. X
  273. XYou can filter out the symbolic links with the ^ character:
  274. X
  275. X% l *(W^@)
  276. Xrun240
  277. X% l *(x)
  278. Xfoo*        link@       morestuff/  stuff/
  279. X% l *(x^@/)
  280. Xfoo*
  281. X
  282. X
  283. XTo find all plain files, you can use .:
  284. X
  285. X
  286. X
  287. X
  288. X
  289. X
  290. X
  291. X
  292. X
  293. X
  294. X
  295. X
  296. X
  297. X                           - 5 -
  298. X
  299. X% l *(.)
  300. XMakefile  file.h    foo*      foo.o     main.o    run123    run234    run303
  301. Xbar.o     file.pro  foo.c     main.h    q.c       run2      run240    sub
  302. X% l *(^.)
  303. Xlink@       morestuff/  pipe        stuff/
  304. X% l s*(.)
  305. Xstuff/   sub
  306. X% l *(p)
  307. Xpipe
  308. X% l -l *(p)
  309. Xprw-r--r--  1 pfalstad        0 May 23 18:12 pipe
  310. X
  311. X
  312. X*(U) matches all files owned by  you.   To  search  for  all
  313. Xfiles not owned by you, use *(^U):
  314. X
  315. X% l -l *(^U)
  316. X-rw-------  1 subbarao       29 May 23 18:13 sub
  317. X
  318. X
  319. XThis searches for setuid files:
  320. X
  321. X% l -l *(s)
  322. X-rwsr-xr-x  1 pfalstad       16 May 23 18:12 foo*
  323. X
  324. X
  325. XThis checks for a certain user's files:
  326. X
  327. X% ypmatch subbarao passwd
  328. Xsubbarao:*:3338:35:Kartik Subbarao:/u/subbarao:/usr/princeton/bin/zsh
  329. X% l -l *(u3338)
  330. X-rw-------  1 subbarao       29 May 23 18:13 sub
  331. X
  332. X
  333. X
  334. XStartup Files
  335. X
  336. XThere are five startup files that  zsh  will  read  commands
  337. Xfrom:
  338. X
  339. X$ZDOTDIR/.zshenv
  340. X$ZDOTDIR/.zprofile
  341. X$ZDOTDIR/.zshrc
  342. X$ZDOTDIR/.zlogin
  343. X$ZDOTDIR/.zlogout
  344. X
  345. X
  346. XIf ZDOTDIR is not set, then the value of HOME is used;  this
  347. Xis the usual case.
  348. X
  349. X.zshenv is sourced on all invocations of the  shell,  unless
  350. Xthe -f option is set.  It should contain commands to set the
  351. Xcommand search path, plus other important environment  vari-
  352. Xables.   .zshenv  should  not  contain commands that produce
  353. Xoutput or assume the shell is attached to a tty.
  354. X
  355. X.zshrc is sourced in interactive shells.  It should  contain
  356. Xcommands  to  set  up aliases, functions, options, key bind-
  357. Xings, etc.
  358. X
  359. X
  360. X
  361. X
  362. X
  363. X                           - 6 -
  364. X.zlogin is sourced in login shells.  It should contain  com-
  365. Xmands  that  should be executed only in login shells.  .zlo-
  366. Xgout is sourced when login shells exit.  .zprofile is  simi-
  367. Xlar  to  .zlogin,  except  that it is sourced before .zshrc.
  368. X.zprofile is meant as an  alternative  to  .zlogin  for  ksh
  369. Xfans; the two are not intended to be used together, although
  370. Xthis could certainly be done if desired.  .zlogin is not the
  371. Xplace  for  alias definitions, options, environment variable
  372. Xsettings, etc.; as a general rule, it should not change  the
  373. Xshell  environment at all.  Rather, it should be used to set
  374. Xthe terminal type and run  a  series  of  external  commands
  375. X(fortune, msgs, etc).
  376. X
  377. XShell Functions
  378. X
  379. Xzsh also allows you to create your own commands by  defining
  380. Xshell functions.  For example:
  381. X
  382. X% yp () {
  383. X>       ypmatch $1 passwd.byname
  384. X> }
  385. X% yp pfalstad
  386. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  387. X
  388. X
  389. XThis function looks up a user in the NIS password map.   The
  390. X$1  expands to the first argument to yp.  The function could
  391. Xhave been equivalently defined in one of the following ways:
  392. X
  393. X% function yp {
  394. X>       ypmatch $1 passwd.byname
  395. X> }
  396. X% function yp () {
  397. X>       ypmatch $1 passwd.byname
  398. X> }
  399. X% function yp () ypmatch $1 passwd.byname
  400. X
  401. X
  402. XNote that aliases are expanded when the function  definition
  403. Xis parsed, not when the function is executed.  For example:
  404. X
  405. X% alias ypmatch=echo
  406. X% yp pfalstad
  407. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  408. X
  409. X
  410. XSince the alias was defined after the function  was  parsed,
  411. Xit  has  no effect on the function's execution.  However, if
  412. Xwe define the function again with the alias in place:
  413. X
  414. X% function yp () { ypmatch $1 passwd.byname }
  415. X% yp pfalstad
  416. Xpfalstad passwd.byname
  417. X
  418. X
  419. Xit is parsed with the new alias definition in place.  There-
  420. Xfore, in general you must define aliases before functions.
  421. X
  422. XWe can make the function take multiple arguments:
  423. X
  424. X
  425. X
  426. X
  427. X
  428. X
  429. X                           - 7 -
  430. X
  431. X% unalias ypmatch
  432. X% yp () {
  433. X>       for i
  434. X>       do ypmatch $i passwd.byname
  435. X>       done
  436. X> }
  437. X% yp pfalstad subbarao sukthnkr
  438. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  439. Xsubbarao:*:3338:35:Kartik Subbarao:/u/subbarao:/usr/princeton/bin/zsh
  440. Xsukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  441. X
  442. X
  443. XThe for i loops through each of  the  function's  arguments,
  444. Xsetting  i  equal to each of them in turn.  We can also make
  445. Xthe function do  something  sensible  if  no  arguments  are
  446. Xgiven:
  447. X
  448. X% yp () {
  449. X>       if (( $# == 0 ))
  450. X>       then echo usage: yp name ...; fi
  451. X>       for i; do ypmatch $i passwd.byname; done
  452. X> }
  453. X% yp
  454. Xusage: yp name ...
  455. X% yp pfalstad sukthnkr
  456. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  457. Xsukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  458. X
  459. X
  460. X$# is the number of arguments supplied to the function.   If
  461. Xit is equal to zero, we print a usage message; otherwise, we
  462. Xloop through the arguments, and ypmatch all of them.
  463. X
  464. XHere's a function that selects a random line from a file:
  465. X
  466. X% randline () {
  467. X>       integer z=$(wc -l <$1)
  468. X>       sed -n $[RANDOM % z + 1]p $1
  469. X> }
  470. X% randline /etc/motd
  471. XPHOENIX WILL BE DOWN briefly Friday morning, 5/24/91 from 8 AM to
  472. X% randline /etc/motd
  473. XSunOS Release 4.1.1 (PHOENIX) #19: Tue May 14 19:03:15 EDT 1991
  474. X% randline /etc/motd
  475. X| Please use the "msgs" command to read announcements.  Refer to the   |
  476. X% echo $z
  477. X
  478. X%
  479. X
  480. X
  481. Xrandline has a local variable, z, that holds the  number  of
  482. Xlines  in  the  file.  $[RANDOM % z + 1] expands to a random
  483. Xnumber between 1 and z.  An expression of  the  form  $[...]
  484. Xexpands to the value of the arithmetic expression within the
  485. Xbrackets, and the RANDOM variable returns  a  random  number
  486. Xeach  time  it is referenced.  % is the modulus operator, as
  487. Xin C.  Therefore, sed -n $[RANDOM%z+1]p picks a random  line
  488. Xfrom its input, from 1 to z.
  489. X
  490. X
  491. X
  492. X
  493. X
  494. X
  495. X                           - 8 -
  496. XFunction definitions can be viewed with the functions  buil-
  497. Xtin:
  498. X
  499. X% functions randline
  500. Xrandline () {
  501. X        integer z=$(wc -l <$1)
  502. X        sed -n $[RANDOM % z + 1]p $1
  503. X
  504. X}
  505. X% functions
  506. Xyp () {
  507. X        if let $# == 0
  508. X
  509. X        then
  510. X                echo usage: yp name ...
  511. X
  512. X        fi
  513. X        for i
  514. X        do
  515. X                ypmatch $i passwd.byname
  516. X
  517. X                done
  518. X
  519. X}
  520. Xrandline () {
  521. X        integer z=$(wc -l <$1)
  522. X        sed -n $[RANDOM % z + 1]p $1
  523. X
  524. X}
  525. X
  526. X
  527. XHere's another one:
  528. X
  529. X% cx () { chmod +x $* }
  530. X% ls -l foo bar
  531. X-rw-r--r--  1 pfalstad       29 May 24 04:38 bar
  532. X-rw-r--r--  1 pfalstad       29 May 24 04:38 foo
  533. X% cx foo bar
  534. X% ls -l foo bar
  535. X-rwxr-xr-x  1 pfalstad       29 May 24 04:38 bar
  536. X-rwxr-xr-x  1 pfalstad       29 May 24 04:38 foo
  537. X
  538. X
  539. XNote that this could also have been implemented as an alias:
  540. X
  541. X% chmod 644 foo bar
  542. X% alias cx='chmod +x'
  543. X% cx foo bar
  544. X% ls -l foo bar
  545. X-rwxr-xr-x  1 pfalstad       29 May 24 04:38 bar
  546. X-rwxr-xr-x  1 pfalstad       29 May 24 04:38 foo
  547. X
  548. X
  549. X
  550. XInstead of defining a lot of functions in your  .zshrc,  all
  551. Xof  which  you  may  not  use, it is often better to use the
  552. Xautoload builtin.  The idea is, you create a directory where
  553. Xfunction  definitions  are stored, declare the names in your
  554. X.zshrc, and tell the shell where to look for them.  Whenever
  555. Xyou  reference a function, the shell will automatically load
  556. X
  557. X
  558. X
  559. X
  560. X
  561. X                           - 9 -
  562. Xit into memory.
  563. X
  564. X% mkdir /tmp/funs
  565. X% cat >/tmp/funs/yp
  566. Xypmatch $1 passwd.byname
  567. X^D
  568. X% cat >/tmp/funs/cx
  569. Xchmod +x $*
  570. X^D
  571. X% FPATH=/tmp/funs
  572. X% autoload cx yp
  573. X% functions cx yp
  574. Xundefined cx ()
  575. Xundefined yp ()
  576. X% chmod 755 /tmp/funs/{cx,yp}
  577. X% yp egsirer
  578. Xegsirer:*:3214:35:Emin Gun Sirer:/u/egsirer:/bin/sh
  579. X% functions yp
  580. Xyp () {
  581. X        ypmatch $1 passwd.byname
  582. X}
  583. X
  584. X
  585. XThis idea has other benefits.  By adding a #! header to  the
  586. Xfiles, you can make them double as shell scripts.  (Although
  587. Xit is faster to use them as functions, since a separate pro-
  588. Xcess is not created.)
  589. X
  590. X% ed /tmp/funs/yp
  591. X25
  592. Xi
  593. X#! /usr/local/bin/zsh
  594. Xw
  595. X42
  596. Xq
  597. X% </tmp/funs/yp
  598. X#! /usr/local/bin/zsh
  599. Xypmatch $1 passwd.byname
  600. X% /tmp/funs/yp sukthnkr
  601. Xsukthnkr:*:1267:35:Rahul Sukthankar:/u/sukthnkr:/usr/princeton/bin/tcsh
  602. X
  603. X
  604. XNow other people, who may not use zsh, or who don't want  to
  605. Xcopy  all  of  your .zshrc, may use these functions as shell
  606. Xscripts.
  607. X
  608. XDirectories
  609. X
  610. XOne nice feature of zsh is the way  it  prints  directories.
  611. XFor example, if we set the prompt like this:
  612. X
  613. Xphoenix% PROMPT='%~> '
  614. X~> cd src
  615. X~/src>
  616. X
  617. X
  618. Xthe shell will print the current directory  in  the  prompt,
  619. Xusing  the  ~  character.  However, zsh is smarter than most
  620. Xother shells in this respect:
  621. X
  622. X
  623. X
  624. X
  625. X
  626. X
  627. X                           - 10 -
  628. X
  629. X~/src> cd ~subbarao
  630. X~subbarao> cd ~maruchck
  631. X~maruchck> cd lib
  632. X~maruchck/lib> cd fun
  633. X~maruchck/lib/fun> foo=/usr/princeton/common/src
  634. X~maruchck/lib/fun> cd ~foo
  635. X~foo> cd ..
  636. X/usr/princeton/common> cd src
  637. X~foo> cd news/nntp
  638. X~foo/news/nntp> cd inews
  639. X~foo/news/nntp/inews>
  640. X
  641. X
  642. XNote that zsh prints other users' directories  in  the  form
  643. X~user.  Also note that you can set a parameter and use it as
  644. Xa directory name; zsh will act as if foo is a user with  the
  645. Xlogin  directory  /usr/princeton/common/src.   This  is con-
  646. Xvenient, especially if you're sick of  seeing  prompts  like
  647. Xthis:
  648. X
  649. Xphoenix:/usr/princeton/common/src/X.V11R4/contrib/clients/xv/docs>
  650. X
  651. X
  652. XIf you get stuck in this position, you can give the  current
  653. Xdirectory a short name, like this:
  654. X
  655. X/usr/princeton/common/src/news/nntp/inews> inews=$PWD
  656. X/usr/princeton/common/src/news/nntp/inews> echo ~inews
  657. X/usr/princeton/common/src/news/nntp/inews
  658. X~inews>
  659. X
  660. X
  661. XWhen you reference a directory in the form ~inews, the shell
  662. Xassumes  that you want the directory displayed in this form;
  663. Xthus simply typing echo  ~inews  or  cd  ~inews  causes  the
  664. Xprompt to be shortened.  You can define a shell function for
  665. Xthis purpose:
  666. X
  667. X~inews> namedir () { $1=$PWD ;  : ~$1 }
  668. X~inews> cd /usr/princeton/bin
  669. X/usr/princeton/bin> namedir pbin
  670. X~pbin> cd /var/spool/mail
  671. X/var/spool/mail> namedir spool
  672. X~spool> cd .msgs
  673. X~spool/.msgs>
  674. X
  675. X
  676. XYou may want to add this one-line function to your .zshrc.
  677. X
  678. Xzsh can also put the current directory in your title bar, if
  679. Xyou  are  using  a  windowing system.  One way to do this is
  680. Xwith the chpwd function, which is automatically executed  by
  681. Xthe  shell  whenever you change directory.  If you are using
  682. Xxterm, this will work:
  683. X
  684. Xchpwd () { print -Pn '^[]2;%~^G' }
  685. X
  686. X
  687. XThe -P option tells print to  treat  its  arguments  like  a
  688. X
  689. X
  690. X
  691. X
  692. X
  693. X                           - 11 -
  694. Xprompt  string; otherwise the %~ would not be expanded.  The
  695. X-n option suppresses the terminating newline, as with echo.
  696. X
  697. XIf you are using an IRIS wsh, do this:
  698. X
  699. Xchpwd () { print -Pn '^[P1.y%~^[' }
  700. X
  701. X
  702. XThe print -D command has other uses.  For example, to  print
  703. Xthe  current directory to standard output in short form, you
  704. Xcan do this:
  705. X
  706. X% print -D $PWD
  707. X~subbarao/src
  708. X
  709. X
  710. Xand to print each component of the path in short form:
  711. X
  712. X% print -D $path
  713. X/bin /usr/bin ~locbin ~locbin/X11 ~/bin
  714. X
  715. X
  716. X
  717. XDirectory Stacks
  718. X
  719. XIf you use csh, you may know about  directory  stacks.   The
  720. Xpushd  command  puts the current directory on the stack, and
  721. Xchanges to a new directory; the popd command pops  a  direc-
  722. Xtory off the stack and changes to it.
  723. X
  724. Xphoenix% cd
  725. Xphoenix% PROMPT='Z %~> '
  726. XZ ~> pushd /tmp
  727. X/tmp ~
  728. XZ /tmp> pushd /usr/etc
  729. X/usr/etc /tmp ~
  730. XZ /usr/etc> pushd /usr/bin
  731. X/usr/bin /usr/etc /tmp ~
  732. XZ /usr/bin> popd
  733. X/usr/etc /tmp ~
  734. XZ /usr/etc> popd
  735. X/tmp ~
  736. XZ /tmp> pushd /etc
  737. X/etc /tmp ~
  738. XZ /etc> popd
  739. X/tmp ~
  740. X
  741. X
  742. Xzsh's directory stack commands work similarly.  One  differ-
  743. Xence  is the way pushd is handled if no arguments are given.
  744. XAs in csh, this exchanges the top two elements of the direc-
  745. Xtory stack:
  746. X
  747. XZ /tmp> dirs
  748. X/tmp ~
  749. XZ /tmp> pushd
  750. X~ /tmp
  751. X
  752. X
  753. Xunless the stack only has one entry:
  754. X
  755. X
  756. X
  757. X
  758. X
  759. X                           - 12 -
  760. X
  761. XZ ~> popd
  762. X/tmp
  763. XZ /tmp> dirs
  764. X/tmp
  765. XZ /tmp> pushd
  766. X~ /tmp
  767. X
  768. X
  769. Xor unless the PUSHDTOHOME option is set:
  770. X
  771. XZ ~> setopt pushdtohome
  772. XZ ~> pushd
  773. X~ ~ /tmp
  774. X
  775. X
  776. X
  777. XAs an alternative to using directory stacks in this  manner,
  778. Xwe  can  get something like a directory history by setting a
  779. Xfew more options and parameters:
  780. X
  781. X~> DIRSTACKSIZE=8
  782. X~> setopt autopushd pushdminus pushdsilent pushdtohome
  783. X~> alias dh='dirs -v'
  784. X~> cd /tmp
  785. X/tmp> cd /usr
  786. X/usr> cd bin
  787. X/usr/bin> cd ../pub
  788. X/usr/pub> dh
  789. X0       /usr/pub
  790. X1       /usr/bin
  791. X2       /usr
  792. X3       /tmp
  793. X4       ~
  794. X/usr/pub> cd -3
  795. X/tmp> dh
  796. X0       /tmp
  797. X1       /usr/pub
  798. X2       /usr/bin
  799. X3       /usr
  800. X4       ~
  801. X/tmp> ls =2/df
  802. X/usr/bin/df
  803. X/tmp> cd -4
  804. X~>
  805. X
  806. X
  807. XNote that =2 expanded to the second directory in the history
  808. Xlist,  and  that  cd  -3 recalled the third directory in the
  809. Xlist.
  810. X
  811. XYou may be wondering what all those options  do.   AUTOPUSHD
  812. Xmade  cd act like pushd.  (alias cd=pushd is not sufficient,
  813. Xfor various reasons.) PUSHDMINUS swapped the meaning  of  cd
  814. X+1 and cd -1; we want them to mean the opposite of what they
  815. Xmean in csh, because it makes more sense in this scheme, and
  816. Xit's easier to type:
  817. X
  818. X
  819. X
  820. X
  821. X
  822. X
  823. X
  824. X
  825. X                           - 13 -
  826. X
  827. X~> dh
  828. X0       ~
  829. X1       /tmp
  830. X2       /usr/pub
  831. X3       /usr/bin
  832. X4       /usr
  833. X~> unsetopt pushdminus
  834. X~> cd +1
  835. X/tmp> dh
  836. X0       /tmp
  837. X1       ~
  838. X2       /usr/pub
  839. X3       /usr/bin
  840. X4       /usr
  841. X/tmp> cd +2
  842. X/usr/pub>
  843. X
  844. X
  845. XPUSHDSILENT keeps the  shell  from  printing  the  directory
  846. Xstack  each  time  we  do a cd, and PUSHDTOHOME we mentioned
  847. Xearlier:
  848. X
  849. X/usr/pub> unsetopt pushdsilent
  850. X/usr/pub> cd /etc
  851. X/etc /usr/pub /tmp ~ /usr/bin /usr
  852. X/etc> cd
  853. X~ /etc /usr/pub /tmp ~ /usr/bin /usr
  854. X~> unsetopt pushdtohome
  855. X~> cd
  856. X/etc ~ /usr/pub /tmp ~ /usr/bin /usr
  857. X/etc>
  858. X
  859. X
  860. XDIRSTACKSIZE keeps the  directory  stack  from  getting  too
  861. Xlarge, much like HISTSIZE:
  862. X
  863. X/etc> setopt pushdsilent
  864. X/etc> cd /
  865. X/> cd /
  866. X/> cd /
  867. X/> cd /
  868. X/> cd /
  869. X/> cd /
  870. X/> cd /
  871. X/> cd /
  872. X/> dh
  873. X0       /
  874. X1       /
  875. X2       /
  876. X3       /
  877. X4       /
  878. X5       /
  879. X6       /
  880. X7       /
  881. X
  882. X
  883. X
  884. X
  885. X
  886. X
  887. X
  888. X
  889. X
  890. X
  891. X                           - 14 -
  892. XCommand/Process Substitution
  893. X
  894. XCommand substitution in zsh can  take  two  forms.   In  the
  895. Xtraditional  form,  a command enclosed in backquotes (`...`)
  896. Xis replaced on the command line with its  output.   This  is
  897. Xthe  form used by the older shells.  Newer shells (like zsh)
  898. Xalso provide another form, $(...).  This form is much easier
  899. Xto nest.
  900. X
  901. X% ls -l `echo /vmunix`
  902. X-rwxr-xr-x  1 root      1209702 May 14 19:04 /vmunix
  903. X% ls -l $(echo /vmunix)
  904. X-rwxr-xr-x  1 root      1209702 May 14 19:04 /vmunix
  905. X% who | grep mad
  906. Xsubbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  907. Xpfalstad ttyu1   May 23 16:25   (mad55sx14.Prince)
  908. Xsubbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  909. Xpfalstad ttyv3   May 23 16:25   (mad55sx14.Prince)
  910. X% who | grep mad | awk '{print $2}'
  911. Xttyt7
  912. Xttyu1
  913. Xttyu6
  914. Xttyv3
  915. X% cd /dev; ls -l $(who |
  916. X> grep $(echo mad) |
  917. X> awk '{ print $2 }')
  918. Xcrwx-w----  1 subbarao  20,  71 May 23 18:35 ttyt7
  919. Xcrw--w----  1 pfalstad  20,  81 May 23 18:42 ttyu1
  920. Xcrwx-w----  1 subbarao  20,  86 May 23 18:38 ttyu6
  921. Xcrw--w----  1 pfalstad  20,  99 May 23 18:41 ttyv3
  922. X
  923. X
  924. XMany common  uses  of  command  substitution,  however,  are
  925. Xsuperseded by other mechanisms of zsh:
  926. X
  927. X% ls -l `tty`
  928. Xcrw-rw-rw-  1 root      20,  28 May 23 18:35 /dev/ttyqc
  929. X% ls -l $TTY
  930. Xcrw-rw-rw-  1 root      20,  28 May 23 18:35 /dev/ttyqc
  931. X% ls -l `which rn`
  932. X-rwxr-xr-x  1 root       172032 Mar  6 18:40 /usr/princeton/bin/rn
  933. X% ls -l =rn
  934. X-rwxr-xr-x  1 root       172032 Mar  6 18:40 /usr/princeton/bin/rn
  935. X
  936. X
  937. XA command name with a = prepended is replaced with its  full
  938. Xpathname.   This  can  be very convenient.  If it's not con-
  939. Xvenient for you, you can turn it off:
  940. X
  941. X% ls
  942. X=foo    =bar
  943. X% ls =foo =bar
  944. Xzsh: foo not found
  945. X% setopt noequals
  946. X% ls =foo =bar
  947. X=foo    =bar
  948. X
  949. X
  950. X
  951. XAnother nice feature is process substitution:
  952. X
  953. X
  954. X
  955. X
  956. X
  957. X                           - 15 -
  958. X
  959. X% who | fgrep -f =(print -l root lemke shgchan subbarao)
  960. Xroot     console May 19 10:41
  961. Xlemke    ttyq0   May 22 10:05   (narnia:0.0)
  962. Xlemke    ttyr7   May 22 10:05   (narnia:0.0)
  963. Xlemke    ttyrd   May 22 10:05   (narnia:0.0)
  964. Xshgchan  ttys1   May 23 16:52   (gaudi.Princeton.)
  965. Xsubbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  966. Xsubbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  967. Xshgchan  ttyvb   May 23 16:51   (gaudi.Princeton.)
  968. X
  969. X
  970. XA command of the form =(...) is replaced with the name of  a
  971. Xfile containing its output.  (A command substitution, on the
  972. Xother hand, is replaced with the output itself.) print -l is
  973. Xlike  echo,  excepts  that  it  prints its arguments one per
  974. Xline, the way fgrep expects them:
  975. X
  976. X% print -l foo bar
  977. Xfoo
  978. Xbar
  979. X
  980. X
  981. XWe could also have written:
  982. X
  983. X% who | fgrep -f =(echo 'root
  984. X> lemke
  985. X> shgchan
  986. X> subbarao')
  987. X
  988. X
  989. XUsing process substitution, you can edit  the  output  of  a
  990. Xcommand:
  991. X
  992. X% ed =(who | fgrep -f ~/.friends)
  993. X355
  994. Xg/lemke/d
  995. Xw /tmp/filbar
  996. X226
  997. Xq
  998. X% cat /tmp/filbar
  999. Xroot     console May 19 10:41
  1000. Xshgchan  ttys1   May 23 16:52   (gaudi.Princeton.)
  1001. Xsubbarao ttyt7   May 23 15:02   (mad55sx15.Prince)
  1002. Xsubbarao ttyu6   May 23 15:04   (mad55sx15.Prince)
  1003. Xshgchan  ttyvb   May 23 16:51   (gaudi.Princeton.)
  1004. X
  1005. X
  1006. Xor easily read archived mail:
  1007. X
  1008. X
  1009. X
  1010. X
  1011. X
  1012. X
  1013. X
  1014. X
  1015. X
  1016. X
  1017. X
  1018. X
  1019. X
  1020. X
  1021. X
  1022. X
  1023. X                           - 16 -
  1024. X
  1025. X% mail -f =(zcat ~/mail/oldzshmail.Z)
  1026. X"/tmp/zsha06024": 84 messages, 0 new, 43 unread
  1027. X>  1  U  TO: pfalstad, zsh (10)
  1028. X   2  U  nytim!tim@uunet.uu.net, Re: Zsh on Sparc1 /SunOS 4.0.3
  1029. X   3  U  JAM%TPN@utrcgw.utc.com, zsh fix (15)
  1030. X   4  U  djm@eng.umd.edu, way to find out if running zsh? (25)
  1031. X   5  U  djm@eng.umd.edu, Re: way to find out if running zsh? (17)
  1032. X   6   r djm@eng.umd.edu, Meta . (18)
  1033. X   7  U  jack@cs.glasgow.ac.uk, Re: problem building zsh (147)
  1034. X   8  U  nytim!tim@uunet.uu.net, Re: Zsh on Sparc1 /SunOS 4.0.3
  1035. X   9     ursa!jmd, Another fix... (61)
  1036. X  10  U  pplacewa@bbn.com, Re: v18i084: Zsh 2.00 - A small complaint (36)
  1037. X  11  U  lubkin@cs.rochester.edu, POSIX job control (34)
  1038. X  12  U  yale!bronson!tan@uunet.UU.NET
  1039. X  13  U  brett@rpi.edu, zsh (36)
  1040. X  14  S  subbarao, zsh sucks!!!! (286)
  1041. X  15  U  snibru!d241s008!d241s013!ala@relay.EU.net, zsh (165)
  1042. X  16  U  nytim!tim@uunet.UU.NET, Re: Zsh on Sparc1 /SunOS 4.0.3
  1043. X  17  U  subbarao, zsh is a junk shell (43)
  1044. X  18  U  amaranth@vela.acs.oakland.edu, zsh (33)
  1045. X43u/84 1: x
  1046. X% ls -l /tmp/zsha06024
  1047. X/tmp/zsha06024 not found
  1048. X
  1049. X
  1050. XNote that the shell creates a temporary file, and deletes it
  1051. Xwhen the command is finished.
  1052. X
  1053. X% diff =(ls) =(ls -F)
  1054. X3c3
  1055. X< fortune
  1056. X---
  1057. X> fortune*
  1058. X10c10
  1059. X< strfile
  1060. X---
  1061. X> strfile*
  1062. X
  1063. X
  1064. XIf you read zsh's man page, you may notice  that  <(...)  is
  1065. Xanother  form  of  process  substitution which is similar to
  1066. X=(...).  There is an important difference between  the  two.
  1067. XIn  the  <(...)  case, the shell creates a named pipe (FIFO)
  1068. Xinstead of a file.  This is better, since it does  not  fill
  1069. Xup  the  file system; but it does not work in all cases.  In
  1070. Xfact, if we had replaced =(...) with <(...) in the  examples
  1071. Xabove,  all  of  them  would have stopped working except for
  1072. Xfgrep -f <(...).  You can not edit a pipe, or open it  as  a
  1073. Xmail  folder;  fgrep, however, has no problem with reading a
  1074. Xlist of words from a pipe.  You may wonder why  diff  <(foo)
  1075. Xbar  doesn't  work,  since  foo  | diff - bar works; this is
  1076. Xbecause diff creates a temporary file if it notices that one
  1077. Xof its arguments is -, and then copies its standard input to
  1078. Xthe temporary file.
  1079. X
  1080. XAliasing
  1081. X
  1082. XOften-used commands can be abbreviated with an alias:
  1083. X
  1084. X
  1085. X
  1086. X
  1087. X
  1088. X
  1089. X                           - 17 -
  1090. X
  1091. X% alias uc=uncompress
  1092. X% ls
  1093. Xhanoi.Z
  1094. X% uc hanoi
  1095. X% ls
  1096. Xhanoi
  1097. X
  1098. X
  1099. Xor commands with certain desired options:
  1100. X
  1101. X% alias fm='finger -m'
  1102. X% fm root
  1103. XLogin name: root                        In real life: Operator
  1104. XDirectory: /                            Shell: /bin/csh
  1105. XOn since May 19 10:41:15 on console     3 days 5 hours Idle Time
  1106. XNo unread mail
  1107. XNo Plan.
  1108. X
  1109. X% alias lock='lock -p -60000'
  1110. X% lock
  1111. Xlock: /dev/ttyr4 on phoenix. timeout in 60000 minutes
  1112. Xtime now is Fri May 24 04:23:18 EDT 1991
  1113. XKey:
  1114. X
  1115. X% alias l='ls -AF'
  1116. X% l /
  1117. X.bash_history              kadb*
  1118. X.bashrc                    lib@
  1119. X.cshrc                     licensed/
  1120. X.exrc                      lost+found/
  1121. X.login                     macsyma
  1122. X
  1123. X
  1124. XAliases can also be used to replace old commands:
  1125. X
  1126. X% alias grep=egrep ps=sps make=gmake
  1127. X% alias whoami='echo root'
  1128. X% whoami
  1129. Xroot
  1130. X
  1131. X
  1132. Xor to define new ones:
  1133. X
  1134. X
  1135. X
  1136. X
  1137. X
  1138. X
  1139. X
  1140. X
  1141. X
  1142. X
  1143. X
  1144. X
  1145. X
  1146. X
  1147. X
  1148. X
  1149. X
  1150. X
  1151. X
  1152. X
  1153. X
  1154. X
  1155. X                           - 18 -
  1156. X
  1157. X% cd /
  1158. X% alias sz='ls -l | sort -n +3 | tail -10'
  1159. X% sz
  1160. Xdrwxr-sr-x  7 bin          3072 May 23 11:59 etc
  1161. Xdrwxrwxrwx 26 root         5120 May 24 04:20 tmp
  1162. Xdrwxr-xr-x  2 root         8192 Dec 26 19:34 lost+found
  1163. Xdrwxr-sr-x  2 bin         14848 May 23 18:48 dev
  1164. X-r--r--r--  1 root       140520 Dec 26 20:08 boot
  1165. X-rwxr-xr-x  1 root       311172 Dec 26 20:08 kadb
  1166. X-rwxr-xr-x  1 root      1209695 Apr 16 15:33 vmunix.old
  1167. X-rwxr-xr-x  1 root      1209702 May 14 19:04 vmunix
  1168. X-rwxr-xr-x  1 root      1209758 May 21 12:23 vmunix.new.kernelmap.old
  1169. X-rwxr-xr-x  1 root      1711848 Dec 26 20:08 vmunix.org
  1170. X% cd
  1171. X% alias rable='ls -AFtrd *(R)' nrable='ls -AFtrd *(^R)'
  1172. X% rable
  1173. XREADME      func/       bin/        pub/        News/       src/
  1174. Xnicecolors  etc/        scr/        tmp/        iris/       zsh*
  1175. X% nrable
  1176. XMailboxes/  mail/       notes
  1177. X
  1178. X
  1179. X(The pattern *(R) matches all readable files in the  current
  1180. Xdirectory, and *(^R) matches all unreadable files.)
  1181. X
  1182. XMost  other  shells  have  aliases  of  this  kind  (command
  1183. Xaliases).   However,  zsh also has global aliases, which are
  1184. Xsubstituted anywhere on a line.  Global aliases can be  used
  1185. Xto abbreviate frequently-typed usernames, hostnames, etc.
  1186. X
  1187. X% alias -g me=pfalstad gun=egsirer mjm=maruchck
  1188. X% who | grep me
  1189. Xpfalstad ttyp0   May 24 03:39   (mickey.Princeton)
  1190. Xpfalstad ttyp5   May 24 03:42   (mickey.Princeton)
  1191. X% fm gun
  1192. XLogin name: egsirer                     In real life: Emin Gun Sirer
  1193. XDirectory: /u/egsirer                   Shell: /bin/sh
  1194. XLast login Thu May 23 19:05 on ttyq3 from bow.Princeton.ED
  1195. XNew mail received Fri May 24 02:30:28 1991;
  1196. X  unread since Fri May 24 02:30:27 1991
  1197. X% alias -g phx=phoenix.princeton.edu warc=wuarchive.wustl.edu
  1198. X% ftp warc
  1199. XConnected to wuarchive.wustl.edu.
  1200. X
  1201. X
  1202. XHere are some more interesting uses.
  1203. X
  1204. X% alias -g M='| more' GF='| fgrep -f ~/.friends'
  1205. X% who M   # pipes the output of who through more
  1206. X% who GF  # see if your friends are on
  1207. X% w GF    # see what your friends are doing
  1208. X
  1209. X
  1210. XAnother example makes use of zsh's process substitution.  If
  1211. Xyou run NIS, and you miss being able to do this:
  1212. X
  1213. X% grep pfalstad /etc/passwd
  1214. X
  1215. X
  1216. X
  1217. X
  1218. X
  1219. X
  1220. X
  1221. X                           - 19 -
  1222. Xyou can define an alias that will  seem  more  natural  than
  1223. Xypmatch pfalstad passwd:
  1224. X
  1225. X% alias -g PASS='<(ypcat passwd)'
  1226. X% grep pfalstad PASS
  1227. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1228. X
  1229. X
  1230. XIf you're really crazy, you can even call it /etc/passwd:
  1231. X
  1232. X% alias -g /etc/passwd='<(ypcat passwd)'
  1233. X% grep pfalstad /etc/passwd
  1234. Xpfalstad:*:3564:35:Paul John Falstad:/u/pfalstad:/usr/princeton/bin/zsh
  1235. X
  1236. X
  1237. XThe last example shows one of the perils of global  aliases;
  1238. Xthey  have a lot of potential to cause confusion.  For exam-
  1239. Xple, if you defined a global alias called | (which is possi-
  1240. Xble), zsh would begin to act very strangely; every pipe sym-
  1241. Xbol would be replaced with the text of your alias.  To  some
  1242. Xextent,  global  aliases are like macros in C; discretion is
  1243. Xadvised in using them and in choosing names for them.  Using
  1244. Xnames  in all caps is not a bad idea, especially for aliases
  1245. Xwhich introduce shell metasyntax (like M and GF above).
  1246. X
  1247. XNote that zsh aliases are not like csh aliases.  The  syntax
  1248. Xfor  defining  them is different, and they do not have argu-
  1249. Xments.  All your favorite csh aliases will probably not work
  1250. Xunder zsh.  For example, if you try:
  1251. X
  1252. Xalias rm mv '\!* /tmp/wastebasket'
  1253. X
  1254. X
  1255. Xno aliases will be defined,  but  zsh  will  not  report  an
  1256. Xerror.   In  csh,  this  line defines an alias that makes rm
  1257. Xsafe---files that are rm'd will  be  moved  to  a  temporary
  1258. Xdirectory  instead of instantly destroyed.  In zsh's syntax,
  1259. Xhowever, this line asks the  shell  to  print  any  existing
  1260. Xalias definitions for rm, mv, or !* /tmp/wastebasket.  Since
  1261. Xthere are none, most likely, the shell will not  print  any-
  1262. Xthing,  although alias will return a nonzero exit code.  The
  1263. Xproper syntax is this:
  1264. X
  1265. Xalias rm='mv \!* /tmp/wastebasket'
  1266. X
  1267. X
  1268. XHowever, this won't work either:
  1269. X
  1270. X% rm foo.dvi
  1271. Xzsh: no matches found: !*
  1272. X
  1273. X
  1274. XWhile this makes rm safe, it is certainly not what the  user
  1275. Xintended.  In zsh, you must use a shell function for this:
  1276. X
  1277. X% unalias rm
  1278. X% rm () { mv $* /tmp/wastebasket }
  1279. X% rm foo.dvi
  1280. X% ls /tmp/wastebasket
  1281. Xfoo.dvi
  1282. X
  1283. X
  1284. X
  1285. X
  1286. X
  1287. X                           - 20 -
  1288. XWhile this is much cleaner and easier to read  (I  hope  you
  1289. Xwill  agree), it is not csh-compatible.  Therefore, a script
  1290. Xto convert csh aliases and variables has been provided.  You
  1291. Xshould  only  need  to  use it once, to convert all your csh
  1292. Xaliases and parameters to zsh format:
  1293. X
  1294. X% csh
  1295. Xcsh> alias
  1296. Xl       ls -AF
  1297. Xmore    less
  1298. Xon      last -2 !:1 ; who | grep !:1
  1299. Xcsh> exit
  1300. X% c2z >neat_zsh_aliases
  1301. X% cat neat_zsh_aliases
  1302. Xalias l='ls -AF'
  1303. Xalias more='less'
  1304. Xon () { last -2 $1 ; who | grep $1 }
  1305. X...
  1306. X
  1307. X
  1308. XThe first two aliases were converted to regular zsh aliases,
  1309. Xwhile  the  third,  since it needed to handle arguments, was
  1310. Xconverted to a function.  c2z can convert  most  aliases  to
  1311. Xzsh  format  without any problems.  However, if you're using
  1312. Xsome really arcane csh tricks, or if you have an alias  with
  1313. Xa  name  like do (which is reserved in zsh), you may have to
  1314. Xfix some of the aliases by hand.
  1315. X
  1316. XThe c2z script checks your csh setup, and produces a list of
  1317. Xzsh commands which replicate your aliases and parameter set-
  1318. Xtings as closely as possible.  You could include its  output
  1319. Xin your startup file, .zshrc.
  1320. X
  1321. XHistory
  1322. X
  1323. XThere are several ways to manipulate history  in  zsh.   One
  1324. Xway is to use csh-style ! history:
  1325. X
  1326. X% /usr/local/bin/!:0 !-2*:s/foo/bar/ >>!$
  1327. X
  1328. X
  1329. XIf you don't want to use this, you can turn it off by typing
  1330. Xsetopt nobanghist.
  1331. X
  1332. XAnother way is to use the fc command.  For example,  if  you
  1333. Xtype an erroneous command:
  1334. X
  1335. X% for i in `cat /etc/clients`
  1336. X do
  1337. X rpu $i
  1338. X done
  1339. Xzsh: command not found: rpu
  1340. Xzsh: command not found: rpu
  1341. Xzsh: command not found: rpu
  1342. X
  1343. X
  1344. Xtyping fc will execute an editor on this  command,  allowing
  1345. Xyou  to  fix it.  (The default editor is vi, by the way, not
  1346. Xed).
  1347. X
  1348. X
  1349. X
  1350. X
  1351. X
  1352. X
  1353. X                           - 21 -
  1354. X
  1355. X% fc
  1356. X49
  1357. X/rpu/s//rup/p
  1358. X rup $i
  1359. Xw
  1360. X49
  1361. Xq
  1362. Xfor i in `cat /etc/clients`
  1363. X do
  1364. X rup $i
  1365. X done
  1366. X        beam    up  2 days, 10:17,    load average: 0.86, 0.80, 0.50
  1367. X         bow    up  4 days,  8:41,    load average: 0.91, 0.80, 0.50
  1368. X        burn    up          17:18,    load average: 0.91, 0.80, 0.50
  1369. X       burst    up  9 days,  1:49,    load average: 0.95, 0.80, 0.50
  1370. X         tan    up          11:14,    load average: 0.91, 0.80, 0.50
  1371. X       bathe    up  3 days, 17:49,    load average: 1.84, 1.79, 1.50
  1372. X        bird    up  1 day,   9:13,    load average: 1.95, 1.82, 1.51
  1373. X      bonnet    up  2 days, 21:18,    load average: 0.93, 0.80, 0.50
  1374. X
  1375. X
  1376. XA variant of the fc command is r, which redoes the last com-
  1377. Xmand, with optional changes:
  1378. X
  1379. X% echo foo
  1380. Xfoo
  1381. X% r
  1382. Xecho foo
  1383. Xfoo
  1384. X
  1385. X% echo foo
  1386. Xfoo
  1387. X% r foo=bar
  1388. Xecho bar
  1389. Xbar
  1390. X
  1391. X
  1392. X
  1393. XCommand Line Editing
  1394. X
  1395. Xzsh's command line editor, ZLE, is quite  powerful.   It  is
  1396. Xdesigned  to  emulate  either  emacs  or  vi; the default is
  1397. Xemacs.  To set the bindings for vi mode, type bindkey -v.
  1398. X
  1399. XIn addition to basic editing, the shell allows you to recall
  1400. Xprevious  lines in the history.  In emacs mode, this is done
  1401. Xwith ^P (control-P):
  1402. X
  1403. X% ls ~
  1404. X-           README      file        mail        pub         tmp
  1405. XMailboxes   bin         func        nicecolors  scr         zsh
  1406. XNews        etc         iris        notes       src
  1407. X% echo foobar
  1408. Xfoobar
  1409. X% ^P
  1410. X% echo foobar^P
  1411. X% ls ~_
  1412. X
  1413. X
  1414. X
  1415. X
  1416. X
  1417. X
  1418. X
  1419. X                           - 22 -
  1420. XPressing ^P once brings up the previous line (echo  foobar);
  1421. Xpressing  it  again  brings  up the line before that (ls ~).
  1422. XThe cursor is left at the end of the line, allowing  you  to
  1423. Xedit  the  line  if  desired  before  executing it.  In many
  1424. Xcases, ZLE eliminates the need for the fc command, since  it
  1425. Xis powerful enough to handle even multiline commands:
  1426. X
  1427. X% for i in a b c d e
  1428. X> do
  1429. X> echo $i
  1430. X> done
  1431. Xa
  1432. Xb
  1433. Xc
  1434. Xd
  1435. Xe
  1436. X% ^P
  1437. X% for i in a b c d e
  1438. X do
  1439. X echo $i
  1440. X done_
  1441. X
  1442. X
  1443. XNow you can just move up to the part you want to change...
  1444. X
  1445. X% for i in _ b c d e
  1446. X do
  1447. X echo $i
  1448. X done
  1449. X
  1450. X
  1451. Xchange it, and execute the new command.
  1452. X
  1453. X% for i in f g h i j
  1454. X do
  1455. X echo $i
  1456. X done
  1457. Xf
  1458. Xg
  1459. Xh
  1460. Xi
  1461. Xj
  1462. X
  1463. X
  1464. XAlso, you can search the history for a certain command using
  1465. XESC-P:
  1466. X
  1467. X% set ESC-P
  1468. X% setopt autolist ESC-P
  1469. X% setopt nocorrect_
  1470. X
  1471. X
  1472. XAnother way is to do an incremental search, emacs-style:
  1473. X
  1474. X
  1475. X
  1476. X
  1477. X
  1478. X
  1479. X
  1480. X
  1481. X
  1482. X
  1483. X
  1484. X
  1485. X                           - 23 -
  1486. X
  1487. X% ^R
  1488. X% _
  1489. Xi-search:
  1490. X
  1491. X% l_ /usr/bin
  1492. Xi-search: l
  1493. X
  1494. X% date > foofile_c
  1495. Xi-search: le
  1496. X
  1497. X
  1498. XAnother useful feature of the editor is command and filename
  1499. Xcompletion.
  1500. X
  1501. X% compTAB
  1502. X% compress _
  1503. X
  1504. X% ls /nicTAB
  1505. X% ls /nicecolors _
  1506. X
  1507. X% ls /usr/prTAB
  1508. X% ls /usr/princeton/_
  1509. X
  1510. X% ls -l =comTAB
  1511. X% ls -l =compress _
  1512. X
  1513. X
  1514. XIf the completion is ambiguous, the editor will  beep.   You
  1515. Xcan list possible completions by pressing ^D:
  1516. X
  1517. X% ls /vmuTAB -beep-
  1518. X% ls /vmunix_
  1519. X% ls /vmunix^D
  1520. Xvmunix                    vmunix.old
  1521. Xvmunix.new.kernelmap.old  vmunix.org
  1522. X
  1523. X
  1524. XOr, you could just set the AUTOLIST option:
  1525. X
  1526. X% setopt autolist
  1527. X% ls /vmuTAB -beep-
  1528. Xvmunix                    vmunix.old
  1529. Xvmunix.new.kernelmap.old  vmunix.org
  1530. X% ls /vmunix_
  1531. X
  1532. X
  1533. XAnother option you could set is RECEXACT, which causes exact
  1534. Xmatches  to  be  accepted,  even if there are other possible
  1535. Xcompletions:
  1536. X
  1537. X% setopt recexact
  1538. X% ls /vmuTAB -beep-
  1539. Xvmunix                    vmunix.old
  1540. Xvmunix.new.kernelmap.old  vmunix.org
  1541. X% ls /vmunix_TAB
  1542. X% ls /vmunix _
  1543. X
  1544. X
  1545. XThe fignore variable  lists  suffixes  of  files  to  ignore
  1546. X
  1547. X
  1548. X
  1549. X
  1550. X
  1551. X                           - 24 -
  1552. Xduring completion.
  1553. X
  1554. X% ls fooTAB -beep-
  1555. Xfoofile.c  foofile.o
  1556. X% fignore=( .o \~ .bak .junk )
  1557. X% ls fooTAB
  1558. X% ls foofile.c _
  1559. X
  1560. X
  1561. XSince foofile.o has a suffix that is in the fignore list, it
  1562. Xwas not considered a possible completion of foo.
  1563. X
  1564. XUsername completion is also supported:
  1565. X
  1566. X% ls ~pfalTAB
  1567. X% ls ~pfalstad/_
  1568. X
  1569. X
  1570. Xand parameter name completion:
  1571. X
  1572. X% echo $ORGTAB
  1573. X% echo $ORGANIZATION _
  1574. X
  1575. X
  1576. Xand hostname completion, if you give the  shell  a  list  of
  1577. Xhosts to complete:
  1578. X
  1579. X% hosts=( phoenix.princeton.edu uunet.uu.net nic.ddn.mil
  1580. X> diskfarm.princeton.edu gnu.ai.mit.edu
  1581. X> eniac.seas.upenn.edu )
  1582. X% telnet diskTAB
  1583. X% telnet diskfarm.princeton.edu _
  1584. X
  1585. X% ftp uuTAB
  1586. X% ftp uunet.uu.net _
  1587. X
  1588. X% mail subbarao@phTAB
  1589. X% mail subbarao@phoenix.princeton.edu _
  1590. X
  1591. X
  1592. Xand option completion:
  1593. X
  1594. X% setopt noclTAB
  1595. X% setopt noclobber _
  1596. X
  1597. X
  1598. Xand binding completion:
  1599. X
  1600. X% bindkey '^X^X' puTAB
  1601. X% bindkey '^X^X' push-line _
  1602. X
  1603. X
  1604. X
  1605. XThe compctl command is used to control how completion works.
  1606. XFor example, to specify that certain commands show take com-
  1607. Xmands as arguments, you use compctl -c:
  1608. X
  1609. X% compctl -c man nohup
  1610. X% man uptTAB
  1611. X% man uptime _
  1612. X
  1613. X
  1614. X
  1615. X
  1616. X
  1617. X                           - 25 -
  1618. XTo specify that a command  should  complete  filenames,  you
  1619. Xshould use compctl -f.  This is the default.  It can be com-
  1620. Xbined with -c, as well.
  1621. X
  1622. X% compctl -cf echo
  1623. X% echo uptTAB
  1624. X% echo uptime _
  1625. X
  1626. X% echo foTAB
  1627. X% echo foo.c
  1628. X
  1629. X
  1630. XSimilarly, use  -h  to  specify  hostnames,  -o  to  specify
  1631. Xoptions,  -v  to  specify variables, and -b to specify bind-
  1632. Xings.
  1633. X
  1634. X% compctl -h rlogin
  1635. X% compctl -hfc rsh
  1636. X% compctl -b bindkey
  1637. X
  1638. X
  1639. XYou can also use -k to specify a custom list of keywords  to
  1640. Xuse in completion.
  1641. X
  1642. X% ftphosts=(ftp.uu.net wuarchive.wustl.edu)
  1643. X% compctl -k ftphosts ftp
  1644. X% ftp wuTAB
  1645. X% ftp wuarchive.wustl.edu _
  1646. X
  1647. X% friends=(cpirazzi subbarao sukthnkr)
  1648. X% compctl -k friends mail finger su
  1649. X% finger cpTAB
  1650. X% finger cpirazzi _
  1651. X
  1652. X
  1653. X
  1654. XIn addition to completion, TAB performs expansion if  possi-
  1655. Xble.
  1656. X
  1657. X% ls *.cTAB
  1658. X% ls foofile.c fortune.c rnd.c strfile.c unstr.c_
  1659. X
  1660. X
  1661. XFor example, suppose you have a bunch of weird files  in  an
  1662. Ximportant directory:
  1663. X
  1664. X% ls
  1665. X  * * *       ; & % $??foo  dspfok        foo.c
  1666. X  !"foo"!       ` \ `         foo           rrr
  1667. X
  1668. X
  1669. XYou want to remove them, but you don't want to damage foo.c.
  1670. XHere is one way to do this:
  1671. X
  1672. X% rm *TAB
  1673. X% rm \ \ \*\ \*\ \*\ \ \  \!\"foo\"\! \;\ \&\ %\ \$'
  1674. X'foo \`\ \\\ \` dspfok foo foo.c rrr_
  1675. X
  1676. X
  1677. XWhen you expand *, zsh inserts the names of  all  the  files
  1678. X
  1679. X
  1680. X
  1681. X
  1682. X
  1683. X                           - 26 -
  1684. Xinto  the  editing  buffer, with proper shell quoting.  Now,
  1685. Xjust move back and remove foo.c from the buffer:
  1686. X
  1687. X% rm \ \ \*\ \*\ \*\ \ \  \!\"foo\"\! \;\ \&\ %\ \$'
  1688. X'foo \`\ \\\ \` dspfok foo _rr
  1689. X
  1690. X
  1691. Xand press return.  Everything except foo.c will  be  deleted
  1692. Xfrom the directory.
  1693. X
  1694. XHere's another trick; let's say you have typed this  command
  1695. Xin:
  1696. X
  1697. X% gcc -o x.out foob.c -g -Wpointer-arith -Wtrigraphs_
  1698. X
  1699. X
  1700. Xand you forget which library you want.  You need  to  escape
  1701. Xout  for  a  minute and check by typing ls /usr/lib, or some
  1702. Xother such command; but you don't want to retype  the  whole
  1703. Xcommand  again,  and  you can't press return now because the
  1704. Xcurrent command is incomplete.  In zsh, you can put the line
  1705. Xon  the  buffer stack, using ESC-Q, and type some other com-
  1706. Xmands.  The next time a prompt is printed, the gcc line will
  1707. Xbe  popped  off  the  stack  and  put  in the editing buffer
  1708. Xautomatically; you can then enter the  proper  library  name
  1709. Xand  press  return  (or, ESC-Q again and look for some other
  1710. Xlibraries whose names you forgot).
  1711. X
  1712. XA similar situation: what if you forget the  option  to  gcc
  1713. Xthat  finds  bugs using AI techniques?  You could either use
  1714. XESC-Q again, and type man gcc, or  you  could  press  ESC-H,
  1715. Xwhich  essentially  does the same thing; it puts the current
  1716. Xline on the buffer stack, and executes the command  run-help
  1717. Xgcc, where run-help is an alias for man.
  1718. X
  1719. XAnother interesting command is  ESC-A.   This  executes  the
  1720. Xcurrent  line,  but  retains  it  in  the buffer, so that it
  1721. Xappears again when the next prompt is  printed.   Also,  the
  1722. Xcursor  stays in the same place.  This is useful for execut-
  1723. Xing a series of similar commands:
  1724. X
  1725. X% cc grok.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1726. X% cc fubar.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1727. X% cc fooble.c -g -lc -lgl -lsun -lmalloc -Bstatic -o b.out
  1728. X
  1729. X
  1730. X
  1731. XThe ESC-' command is useful for managing the shell's quoting
  1732. Xconventions.  Let's say you want to print this string:
  1733. X
  1734. Xdon't do that; type 'rm -rf \*', with a \ before the *.
  1735. X
  1736. X
  1737. XAll that is necessary is to type it into the editing buffer:
  1738. X
  1739. X% don't do that; type 'rm -rf \*', with a \ before the *.
  1740. X
  1741. X
  1742. Xpress ESC-' (escape-quote):
  1743. X
  1744. X
  1745. X
  1746. X
  1747. X
  1748. X
  1749. X                           - 27 -
  1750. X
  1751. X% 'don'\''t do that; type '\''rm -rf \*'\'', with a \ before the *.'
  1752. X
  1753. X
  1754. Xthen move to the beginning and add the echo command.
  1755. X
  1756. X% echo 'don'\''t do that; type '\''rm -rf \*'\'', with a \ before the *.'
  1757. Xdon't do that; type 'rm -rf \*', with a \ before the *.
  1758. X
  1759. X
  1760. XLet's say you want to create an alias to do this  echo  com-
  1761. Xmand.   This  can  be done by recalling the line with ^P and
  1762. Xpressing ESC-' again:
  1763. X
  1764. X% 'echo '\''don'\''\'\'''\''t do that; type '\''\'\'''\''rm -rf
  1765. X\*'\''\'\'''\'', with a \ before the *.'\'''
  1766. X
  1767. X
  1768. Xand then move to the beginning and add the command to create
  1769. Xan alias.
  1770. X
  1771. X% alias zoof='echo '\''don'\''\'\'''\''t do that; type '\''\'\'''\''rm
  1772. X-rf \*'\''\'\'''\'', with a \ before the *.'\'''
  1773. X% zoof
  1774. Xdon't do that; type 'rm -rf \*', with a \ before the *.
  1775. X
  1776. X
  1777. X
  1778. XAnother interesting option is  MENUCOMPLETE.   This  affects
  1779. Xthe way TAB works.  Let's look at the /vmunix example again:
  1780. X
  1781. X% setopt menucomplete
  1782. X% ls /vmuTAB
  1783. X% ls /vmunixTAB
  1784. X% ls /vmunix.new.kernelmap.oldTAB
  1785. X% ls /vmunix.old_
  1786. X
  1787. X
  1788. XEach time you press TAB, it displays the next possible  com-
  1789. Xpletion.   In  this  way, you can cycle through the possible
  1790. Xcompletions until you find the one you want.
  1791. X
  1792. XThe AUTOMENU option makes a  nice  compromise  between  this
  1793. Xmethod  of  completion  and  the regular method.  If you set
  1794. Xthis option, pressing the TAB key repeatedly after an  ambi-
  1795. Xguous  completion  will  cycle  through the possible comple-
  1796. Xtions.
  1797. X
  1798. XBindings
  1799. X
  1800. XEach of the above editor commands was  actually  a  function
  1801. Xbound  by  default  to a certain key.  The real names of the
  1802. Xcommands are:
  1803. X
  1804. Xexpand-or-complete   TAB
  1805. Xpush-line            ESC-Q
  1806. Xrun-help             ESC-H
  1807. Xaccept-and-hold      ESC-A
  1808. Xquote-line           ESC-'
  1809. X
  1810. X
  1811. X
  1812. X
  1813. X
  1814. X
  1815. X                           - 28 -
  1816. XThese bindings are arbitrary; you could change them  if  you
  1817. Xwant.  For example, to bind accept-line to ^Z:
  1818. X
  1819. X% bindkey '^Z' accept-line
  1820. X
  1821. X
  1822. XAnother idea would be to bind the delete key to delete-char;
  1823. Xthis might be convenient if you use ^H for backspace.
  1824. X
  1825. X% bindkey '^?' delete-char
  1826. X
  1827. X
  1828. XOr, you could bind ^X^H to run-help:
  1829. X
  1830. X% bindkey '^X^H' run-help
  1831. X
  1832. X
  1833. XOther examples:
  1834. X
  1835. X% bindkey '^X^Z' universal-argument
  1836. X% bindkey ' ' magic-space
  1837. X% bindkey -s '^T' 'uptime
  1838. X> '
  1839. X
  1840. X
  1841. Xuniversal-argument multiplies the next command by  4.   Thus
  1842. X^X^Z^W might delete the last four words on the line.  If you
  1843. Xbind space to magic-space, then csh-style history  expansion
  1844. Xis done on the line whenever you press the space bar.
  1845. X
  1846. XThe -s flag to bindkey specifies that you  are  binding  the
  1847. Xkey  to  a  string,  not  a  command.   Thus bindkey -s '^T'
  1848. X'uptime\n' lets you VMS lovers get the load average whenever
  1849. Xyou press ^T.
  1850. X
  1851. XIf you have a NeXT keyboard, the one with the | and  \  keys
  1852. Xvery  inconveniently placed, the following bindings may come
  1853. Xin handy:
  1854. X
  1855. X% bindkey -s '\e/' '\\'
  1856. X% bindkey -s '\e=' '|'
  1857. X
  1858. X
  1859. XNow you can type ALT-/ to get a backslash, and ALT-= to  get
  1860. Xa  vertical  bar.   This  only  works inside zsh, of course;
  1861. Xbindkey has no effect on the key  mappings  inside  talk  or
  1862. Xmail, etc.
  1863. X
  1864. XAnother use of the editor is to edit the value of variables.
  1865. XFor  example,  an easy way to change your path is to use the
  1866. Xvared command:
  1867. X
  1868. X% vared PATH
  1869. X> /u/pfalstad/scr:/u/pfalstad/bin/sun4:/u/maruchck/scr:/u/subbarao/bin:/u/maruc
  1870. Xhck/bin:/u/subbarao/scripts:/usr/princeton/bin:/usr/ucb:/usr/bin:/bin:/usr/host
  1871. Xs:/usr/princeton/bin/X11:/./usr/lang:/./usr/etc:/./etc
  1872. X
  1873. X
  1874. XYou can now edit the path.  When you press return, the  con-
  1875. Xtents of the edit buffer will be assigned to PATH.
  1876. X
  1877. X
  1878. X
  1879. X
  1880. X
  1881. X                           - 29 -
  1882. XParameter Substitution
  1883. X
  1884. XIn zsh, parameters are set like this:
  1885. X
  1886. X% foo=bar
  1887. X% echo $foo
  1888. Xbar
  1889. X
  1890. X
  1891. XSpaces before or after the = are frowned upon:
  1892. X
  1893. X% foo = bar
  1894. Xzsh: command not found: foo
  1895. X
  1896. X
  1897. XAlso, set doesn't work for setting parameters:
  1898. X
  1899. X% set foo=bar
  1900. X% set foo = bar
  1901. X% echo $foo
  1902. X
  1903. X%
  1904. X
  1905. X
  1906. XNote that no error message was  printed.   This  is  because
  1907. Xboth of these commands were perfectly valid; the set builtin
  1908. Xassigns its arguments to the positional parameters ($1,  $2,
  1909. Xetc.).
  1910. X
  1911. X% set foo=bar
  1912. X% echo $1
  1913. Xfoo=bar
  1914. X% set foo = bar
  1915. X% echo $3 $2
  1916. Xbar =
  1917. X
  1918. X
  1919. XIf you're really intent on using the csh  syntax,  define  a
  1920. Xfunction like this:
  1921. X
  1922. X% set () {
  1923. X>    eval "$1$2$3"
  1924. X> }
  1925. X% set foo = bar
  1926. X% set fuu=brrr
  1927. X% echo $foo $fuu
  1928. Xbar brrr
  1929. X
  1930. X
  1931. XBut then, of course you can't  use  the  form  of  set  with
  1932. Xoptions,  like set -F (which turns off filename generation).
  1933. XAlso, the set command by itself won't list all  the  parame-
  1934. Xters  like  it  should.   To get around that you need a case
  1935. Xstatement:
  1936. X
  1937. X
  1938. X
  1939. X
  1940. X
  1941. X
  1942. X
  1943. X
  1944. X
  1945. X
  1946. X
  1947. X                           - 30 -
  1948. X
  1949. X% set () {
  1950. X>    case $1 in
  1951. X>    -*|+*|'') builtin set $* ;;
  1952. X>    *) eval "$1$2$3" ;;
  1953. X>    esac
  1954. X> }
  1955. X
  1956. X
  1957. XFor the most part, this should make csh users happy.
  1958. X
  1959. XThe following sh-style operators are supported in zsh:
  1960. X
  1961. X% unset null
  1962. X% echo ${foo-xxx}
  1963. Xbar
  1964. X% echo ${null-xxx}
  1965. Xxxx
  1966. X% unset null
  1967. X% echo ${null=xxx}
  1968. Xxxx
  1969. X% echo $null
  1970. Xxxx
  1971. X% echo ${foo=xxx}
  1972. Xbar
  1973. X% echo $foo
  1974. Xbar
  1975. X% unset null
  1976. X% echo ${null+set}
  1977. X
  1978. X% echo ${foo+set}
  1979. Xset
  1980. X
  1981. X
  1982. XAlso, csh-style : modifiers may be appended to  a  parameter
  1983. Xsubstitution.
  1984. X
  1985. X% echo $PWD
  1986. X/home/learning/pf/zsh/zsh2.00/src
  1987. X% echo $PWD:h
  1988. X/home/learning/pf/zsh/zsh2.00
  1989. X% echo $PWD:h:h
  1990. X/home/learning/pf/zsh
  1991. X% echo $PWD:t
  1992. Xsrc
  1993. X% name=foo.c
  1994. X% echo $name
  1995. Xfoo.c
  1996. X% echo $name:r
  1997. Xfoo
  1998. X% echo $name:e
  1999. Xc
  2000. X
  2001. X
  2002. XThe equivalent constructs in ksh (which are  also  supported
  2003. Xin zsh) are a bit more general and easier to remember.  When
  2004. Xthe shell expands  ${foo#pat},  it  checks  to  see  if  pat
  2005. Xmatches  a  substring  at the beginning of the value of foo.
  2006. XIf so, it removes that portion of foo,  using  the  shortest
  2007. Xpossible  match.   With  ${foo##pat},  the  longest possible
  2008. X
  2009. X
  2010. X
  2011. X
  2012. X
  2013. X                           - 31 -
  2014. Xmatch is removed.  ${foo%pat}  and  ${foo%%pat}  remove  the
  2015. Xmatch  from  the end.  Here are the ksh equivalents of the :
  2016. Xmodifiers:
  2017. X
  2018. X% echo ${PWD%/*}
  2019. X/home/learning/pf/zsh/zsh2.00
  2020. X% echo ${PWD%/*/*}
  2021. X/home/learning/pf/zsh
  2022. X% echo ${PWD##*/}
  2023. Xsrc
  2024. X% echo ${name%.*}
  2025. Xfoo
  2026. X% echo ${name#*.}
  2027. Xc
  2028. X
  2029. X
  2030. Xzsh also has upper/lowercase modifiers:
  2031. X
  2032. X% xx=Test
  2033. X% echo $xx:u
  2034. XTEST
  2035. X% echo $xx:l
  2036. Xtest
  2037. X
  2038. X
  2039. Xand a substitution modifier:
  2040. X
  2041. X% echo $name:s/foo/bar/
  2042. Xbar.c
  2043. X% ls
  2044. Xfoo.c    foo.h    foo.o    foo.pro
  2045. X% for i in foo.*; mv $i $i:s/foo/bar/
  2046. X% ls
  2047. Xbar.c    bar.h    bar.o    bar.pro
  2048. X
  2049. X
  2050. XOne possible source of confusion is the fact  that  in  zsh,
  2051. Xthe  result  of  parameter  substitution  is  not split into
  2052. Xwords.  Thus, this will not work:
  2053. X
  2054. X% srcs='glob.c exec.c init.c'
  2055. X% ls $srcs
  2056. Xglob.c exec.c init.c not found
  2057. X
  2058. X
  2059. XThis is considered a feature, not a bug.  If splitting  were
  2060. Xdone  by  default,  as it is in most other shells, functions
  2061. Xlike this would not work properly:
  2062. X
  2063. X$ ll () { ls -F $* }
  2064. X$ ll 'fuu bar'
  2065. Xfuu not found
  2066. Xbar not found
  2067. X
  2068. X% ll 'fuu bar'
  2069. Xfuu bar not found
  2070. X
  2071. X
  2072. XOf course, a hackish workaround  is  available  in  sh  (and
  2073. Xzsh):
  2074. X
  2075. X
  2076. X
  2077. X
  2078. X
  2079. X                           - 32 -
  2080. X
  2081. X% setopt shwordsplit
  2082. X% ll () { ls -F "$@" }
  2083. X% ll 'fuu bar'
  2084. Xfuu bar not found
  2085. X
  2086. X
  2087. XIf you like the sh behaviour, zsh can accomodate you:
  2088. X
  2089. X% ls ${=srcs}
  2090. Xexec.c  glob.c  init.c
  2091. X% setopt shwordsplit
  2092. X% ls $srcs
  2093. Xexec.c  glob.c  init.c
  2094. X
  2095. X
  2096. XAnother way to get the $srcs trick to  work  is  to  use  an
  2097. Xarray:
  2098. X
  2099. X% unset srcs
  2100. X% srcs=( glob.c exec.c init.c )
  2101. X% ls $srcs
  2102. Xexec.c  glob.c  init.c
  2103. X
  2104. X
  2105. Xor an alias:
  2106. X
  2107. X% alias -g SRCS='exec.c glob.c init.c'
  2108. X% ls SRCS
  2109. Xexec.c  glob.c  init.c
  2110. X
  2111. X
  2112. XAnother option that modifies parameter  expansion  is  RCEX-
  2113. XPANDPARAM:
  2114. X
  2115. X% echo foo/$srcs
  2116. Xfoo/glob.c exec.c init.c
  2117. X% setopt rcexpandparam
  2118. X% echo foo/$srcs
  2119. Xfoo/glob.c foo/exec.c foo/init.c
  2120. X% echo foo/${^srcs}
  2121. Xfoo/glob.c foo/exec.c foo/init.c
  2122. X% echo foo/$^srcs
  2123. Xfoo/glob.c foo/exec.c foo/init.c
  2124. X
  2125. X
  2126. X
  2127. XShell Parameters
  2128. X
  2129. XThe  shell  has  many  predefined  parameters  that  may  be
  2130. Xaccessed.  Here are some examples:
  2131. X
  2132. X
  2133. X
  2134. X
  2135. X
  2136. X
  2137. X
  2138. X
  2139. X
  2140. X
  2141. X
  2142. X
  2143. X
  2144. X
  2145. X                           - 33 -
  2146. X
  2147. X% sleep 10 &
  2148. X[1] 3820
  2149. X% echo $!
  2150. X3820
  2151. X% set a b c
  2152. X% echo $#
  2153. X3
  2154. X% echo $ARGC
  2155. X3
  2156. X% ( exit 20 ) ; echo $?
  2157. X20
  2158. X% false; echo $status
  2159. X1
  2160. X
  2161. X
  2162. X($? and $status are equivalent.)
  2163. X
  2164. X% echo $HOST $HOSTTYPE
  2165. Xdendrite sun4
  2166. X% echo $UID $GID
  2167. X701 60
  2168. X% cd /tmp
  2169. X% cd /home
  2170. X% echo $PWD $OLDPWD
  2171. X/home /tmp
  2172. X% ls $OLDPWD/.getwd
  2173. X/tmp/.getwd
  2174. X
  2175. X
  2176. X~+ and ~- are short for $PWD and $OLDPWD, respectively.
  2177. X
  2178. X% ls ~-/.getwd
  2179. X/tmp/.getwd
  2180. X% ls -d ~+/learning
  2181. X/home/learning
  2182. X% echo $RANDOM
  2183. X4880
  2184. X% echo $RANDOM
  2185. X11785
  2186. X% echo $RANDOM
  2187. X2062
  2188. X% echo $TTY
  2189. X/dev/ttyp4
  2190. X% echo $VERSION
  2191. Xzsh v2.00.03
  2192. X% echo $USERNAME
  2193. Xpf
  2194. X
  2195. X
  2196. X
  2197. XThe cdpath variable sets the search path for the cd command.
  2198. XIf you do not specify . somewhere in the path, it is assumed
  2199. Xto be the first component.
  2200. X
  2201. X
  2202. X
  2203. X
  2204. X
  2205. X
  2206. X
  2207. X
  2208. X
  2209. X
  2210. X
  2211. X                           - 34 -
  2212. X
  2213. X% cdpath=( /usr ~ ~/zsh )
  2214. X% ls /usr
  2215. X5bin         dict         lang         net          sccs         sys
  2216. X5include     etc          lector       nserve       services     tmp
  2217. X5lib         export       lib          oed          share        ucb
  2218. Xadm          games        local        old          skel         ucbinclude
  2219. Xbin          geac         lost+found   openwin      spool        ucblib
  2220. Xboot         hosts        macsyma_417  pat          src          xpg2bin
  2221. Xdemo         include      man          princeton    stand        xpg2include
  2222. Xdiag         kvm          mdec         pub          swap         xpg2lib
  2223. X% cd spool
  2224. X/usr/spool
  2225. X% cd bin
  2226. X/usr/bin
  2227. X% cd func
  2228. X~/func
  2229. X% cd
  2230. X% cd pub
  2231. X% pwd
  2232. X/u/pfalstad/pub
  2233. X% ls -d /usr/pub
  2234. X/usr/pub
  2235. X
  2236. X
  2237. XPATH and path both set the search path for commands.   These
  2238. Xtwo  variables  are  equivalent, except that one is a string
  2239. Xand one is an array.  If the user modifies PATH,  the  shell
  2240. Xchanges path as well, and vice versa.
  2241. X
  2242. X% PATH=/bin:/usr/bin:/tmp:.
  2243. X% echo $path
  2244. X/bin /usr/bin /tmp .
  2245. X% path=( /usr/bin . /usr/local/bin /usr/ucb )
  2246. X% echo $PATH
  2247. X/usr/bin:.:/usr/local/bin:/usr/ucb
  2248. X
  2249. X
  2250. XThe same is true of CDPATH and cdpath:
  2251. X
  2252. X% echo $CDPATH
  2253. X/usr:/u/pfalstad:/u/pfalstad/zsh
  2254. X% CDPATH=/u/subbarao:/usr/src:/tmp
  2255. X% echo $cdpath
  2256. X/u/subbarao /usr/src /tmp
  2257. X
  2258. X
  2259. XIn general, parameters  with  names  in  all  lowercase  are
  2260. Xarrays; assignments to them take the form:
  2261. X
  2262. Xname=( elem ... )
  2263. X
  2264. X
  2265. XParameters with names in  all  uppercase  are  strings.   If
  2266. Xthere  is  both  an  array  and a string version of the same
  2267. Xparameter, the string version  is  a  colon-separated  list,
  2268. Xlike PATH.
  2269. X
  2270. XHISTFILE is the name of the history file, where the  history
  2271. Xis saved when a shell exits.
  2272. X
  2273. X
  2274. X
  2275. X
  2276. X
  2277. X                           - 35 -
  2278. X
  2279. X% zsh
  2280. Xphoenix% HISTFILE=/tmp/history
  2281. Xphoenix% SAVEHIST=20
  2282. Xphoenix% echo foo
  2283. Xfoo
  2284. Xphoenix% date
  2285. XFri May 24 05:39:35 EDT 1991
  2286. Xphoenix% uptime
  2287. X  5:39am  up 4 days, 20:02,  40 users,  load average: 2.30, 2.20, 2.00
  2288. Xphoenix% exit
  2289. X% cat /tmp/history
  2290. XHISTFILE=/tmp/history
  2291. XSAVEHIST=20
  2292. Xecho foo
  2293. Xdate
  2294. Xuptime
  2295. Xexit
  2296. X% HISTSIZE=3
  2297. X% history
  2298. X   28  rm /tmp/history
  2299. X   29  HISTSIZE=3
  2300. X   30  history
  2301. X
  2302. X
  2303. X
  2304. XIn zsh, if you say
  2305. X
  2306. X% >file
  2307. X
  2308. X
  2309. Xthe command cat is normally assumed:
  2310. X
  2311. X% >file
  2312. Xfoo!
  2313. X^D
  2314. X% cat file
  2315. Xfoo!
  2316. X
  2317. X
  2318. END_OF_FILE
  2319.   if test 49646 -ne `wc -c <'doc/intro.txt.01'`; then
  2320.     echo shar: \"'doc/intro.txt.01'\" unpacked with wrong size!
  2321.   fi
  2322.   # end of 'doc/intro.txt.01'
  2323. fi
  2324. if test -f 'help/autoload' -a "${1}" != "-c" ; then 
  2325.   echo shar: Will not clobber existing file \"'help/autoload'\"
  2326. else
  2327.   echo shar: Extracting \"'help/autoload'\" \(274 characters\)
  2328.   sed "s/^X//" >'help/autoload' <<'END_OF_FILE'
  2329. X     autoload [ name ... ]
  2330. X          For each of the names (which are names  of  functions),
  2331. X          create a function marked undefined.  The fpath variable
  2332. X          will be searched to find the actual function definition
  2333. X          when the function is first referenced.
  2334. END_OF_FILE
  2335.   if test 274 -ne `wc -c <'help/autoload'`; then
  2336.     echo shar: \"'help/autoload'\" unpacked with wrong size!
  2337.   fi
  2338.   # end of 'help/autoload'
  2339. fi
  2340. if test -f 'src/Makefile.sample' -a "${1}" != "-c" ; then 
  2341.   echo shar: Will not clobber existing file \"'src/Makefile.sample'\"
  2342. else
  2343.   echo shar: Extracting \"'src/Makefile.sample'\" \(1851 characters\)
  2344.   sed "s/^X//" >'src/Makefile.sample' <<'END_OF_FILE'
  2345. X#! /bin/make -f
  2346. X# Makefile for zsh
  2347. X# generated by buildzsh
  2348. X
  2349. XAUX=buildzsh
  2350. X
  2351. XSRC=builtin.c cond.c exec.c glob.c hist.c init.c jobs.c lex.c loop.c \
  2352. Xmath.c mem.c params.c parse.c subst.c table.c text.c utils.c watch.c \
  2353. Xzle_bindings.c zle_hist.c zle_main.c zle_misc.c zle_move.c zle_refresh.c \
  2354. Xzle_tricky.c zle_utils.c zle_vi.c zle_word.c
  2355. X
  2356. XHEADER=funcs.h zle.h zsh.h ztype.h
  2357. X
  2358. XPROTO=builtin.pro cond.pro exec.pro glob.pro hist.pro init.pro jobs.pro \
  2359. Xlex.pro loop.pro math.pro mem.pro params.pro parse.pro subst.pro table.pro \
  2360. Xtext.pro utils.pro watch.pro zle_bindings.pro zle_hist.pro zle_main.pro \
  2361. Xzle_misc.pro zle_move.pro zle_refresh.pro zle_tricky.pro zle_utils.pro \
  2362. Xzle_vi.pro zle_word.pro
  2363. X
  2364. XOBJS=builtin.o cond.o exec.o glob.o hist.o init.o jobs.o lex.o loop.o \
  2365. Xmath.o mem.o params.o parse.o subst.o table.o text.o utils.o watch.o \
  2366. Xzle_bindings.o zle_hist.o zle_main.o zle_misc.o zle_move.o zle_refresh.o \
  2367. Xzle_tricky.o zle_utils.o zle_vi.o zle_word.o
  2368. X
  2369. XBINDIR=/usr/local/bin
  2370. XMANDIR=/usr/local/man/man1
  2371. X
  2372. X# Debugging flags
  2373. XDFLAGS = # -DQDEBUG
  2374. X
  2375. X# For gcc 2.3.3
  2376. X# CC=gcc -fpcc-struct-return
  2377. X# CFLAGS= -O2 -g -Wall -Wno-implicit -Wno-parentheses -Wno-comment $(DFLAGS)
  2378. X
  2379. XCC=cc
  2380. XCFLAGS= -O
  2381. XLIBS= -ltermcap
  2382. X
  2383. XZSHPATH=zsh
  2384. X
  2385. X.SUFFIXES: .c .o .pro
  2386. X
  2387. X.c.o:
  2388. X    $(CC) $(CFLAGS) $(DFLAGS) -c $<
  2389. X
  2390. X.c.pro:
  2391. X    sed -n '/\/\*\*\/$$/{N;s/^\([^(]*\).*\/\*\*\/.\(.*\)/\1 DCLPROTO((\2))/p;}' $< | sed -e 's/;/,/g' -e 's/,))$$/));/' -e 's/(({))$$/((void));/' >$@
  2392. X
  2393. Xall: $(PROTO) $(ZSHPATH)
  2394. X
  2395. X$(ZSHPATH): $(OBJS)
  2396. X    $(CC) -o $(ZSHPATH) $(OBJS) $(LIBS) $(LFLAGS)
  2397. X    
  2398. Xtags: /tmp
  2399. X    ctags *.[cy]
  2400. X
  2401. X# I hate this next line
  2402. X$(OBJS): config.h zsh.h zle.h signals.h ztype.h funcs.h
  2403. X
  2404. Xparams.o: version.h
  2405. X
  2406. Xclean:
  2407. X    rm -f *.o *.pro zsh core 
  2408. X
  2409. Xcleanall:
  2410. X    rm -f *.o *.pro zsh core Makefile signals.h config.h
  2411. X
  2412. Xinstall: zsh
  2413. X    install -s -m 755 zsh $(BINDIR)
  2414. X    install -m 444 ../man/man1/zsh.1 $(MANDIR)
  2415. END_OF_FILE
  2416.   if test 1851 -ne `wc -c <'src/Makefile.sample'`; then
  2417.     echo shar: \"'src/Makefile.sample'\" unpacked with wrong size!
  2418.   fi
  2419.   # end of 'src/Makefile.sample'
  2420. fi
  2421. echo shar: End of archive 3 \(of 22\).
  2422. cp /dev/null ark3isdone
  2423. MISSING=""
  2424. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ; do
  2425.     if test ! -f ark${I}isdone ; then
  2426.     MISSING="${MISSING} ${I}"
  2427.     fi
  2428. done
  2429. if test "${MISSING}" = "" ; then
  2430.     echo You have unpacked all 22 archives.
  2431.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2432. else
  2433.     echo You still must unpack the following archives:
  2434.     echo "        " ${MISSING}
  2435. fi
  2436. exit 0
  2437.  
  2438. exit 0 # Just in case...
  2439.