.zshrc 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811
  1. # Filename: /etc/zsh/zshrc
  2. # Purpose: config file for zsh (z shell)
  3. # Authors: grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
  4. # Bug-Reports: see http://grml.org/bugs/
  5. # License: This file is licensed under the GPL v2.
  6. ################################################################################
  7. # This file is sourced only for interactive shells. It
  8. # should contain commands to set up aliases, functions,
  9. # options, key bindings, etc.
  10. #
  11. # Global Order: zshenv, zprofile, zshrc, zlogin
  12. ################################################################################
  13. # USAGE
  14. # If you are using this file as your ~/.zshrc file, please use ~/.zshrc.pre
  15. # and ~/.zshrc.local for your own customisations. The former file is read
  16. # before ~/.zshrc, the latter is read after it. Also, consider reading the
  17. # refcard and the reference manual for this setup, both available from:
  18. # <http://grml.org/zsh/>
  19. # Contributing:
  20. # If you want to help to improve grml's zsh setup, clone the grml-etc-core
  21. # repository from git.grml.org:
  22. # git clone git://git.grml.org/grml-etc-core.git
  23. #
  24. # Make your changes, commit them; use 'git format-patch' to create a series
  25. # of patches and send those to the following address via 'git send-email':
  26. # grml-etc-core@grml.org
  27. #
  28. # Doing so makes sure the right people get your patches for review and
  29. # possibly inclusion.
  30. # zsh-refcard-tag documentation:
  31. # You may notice strange looking comments in this file.
  32. # These are there for a purpose. grml's zsh-refcard can now be
  33. # automatically generated from the contents of the actual configuration
  34. # file. However, we need a little extra information on which comments
  35. # and what lines of code to take into account (and for what purpose).
  36. #
  37. # Here is what they mean:
  38. #
  39. # List of tags (comment types) used:
  40. # #a# Next line contains an important alias, that should
  41. # be included in the grml-zsh-refcard.
  42. # (placement tag: @@INSERT-aliases@@)
  43. # #f# Next line contains the beginning of an important function.
  44. # (placement tag: @@INSERT-functions@@)
  45. # #v# Next line contains an important variable.
  46. # (placement tag: @@INSERT-variables@@)
  47. # #k# Next line contains an important keybinding.
  48. # (placement tag: @@INSERT-keybindings@@)
  49. # #d# Hashed directories list generation:
  50. # start denotes the start of a list of 'hash -d'
  51. # definitions.
  52. # end denotes its end.
  53. # (placement tag: @@INSERT-hasheddirs@@)
  54. # #A# Abbreviation expansion list generation:
  55. # start denotes the beginning of abbreviations.
  56. # end denotes their end.
  57. # Lines within this section that end in '#d .*' provide
  58. # extra documentation to be included in the refcard.
  59. # (placement tag: @@INSERT-abbrev@@)
  60. # #m# This tag allows you to manually generate refcard entries
  61. # for code lines that are hard/impossible to parse.
  62. # Example:
  63. # #m# k ESC-h Call the run-help function
  64. # That would add a refcard entry in the keybindings table
  65. # for 'ESC-h' with the given comment.
  66. # So the syntax is: #m# <section> <argument> <comment>
  67. # #o# This tag lets you insert entries to the 'other' hash.
  68. # Generally, this should not be used. It is there for
  69. # things that cannot be done easily in another way.
  70. # (placement tag: @@INSERT-other-foobar@@)
  71. #
  72. # All of these tags (except for m and o) take two arguments, the first
  73. # within the tag, the other after the tag:
  74. #
  75. # #<tag><section># <comment>
  76. #
  77. # Where <section> is really just a number, which are defined by the
  78. # @secmap array on top of 'genrefcard.pl'. The reason for numbers
  79. # instead of names is, that for the reader, the tag should not differ
  80. # much from a regular comment. For zsh, it is a regular comment indeed.
  81. # The numbers have got the following meanings:
  82. # 0 -> "default"
  83. # 1 -> "system"
  84. # 2 -> "user"
  85. # 3 -> "debian"
  86. # 4 -> "search"
  87. # 5 -> "shortcuts"
  88. # 6 -> "services"
  89. #
  90. # So, the following will add an entry to the 'functions' table in the
  91. # 'system' section, with a (hopefully) descriptive comment:
  92. # #f1# Edit an alias via zle
  93. # edalias() {
  94. #
  95. # It will then show up in the @@INSERT-aliases-system@@ replacement tag
  96. # that can be found in 'grml-zsh-refcard.tex.in'.
  97. # If the section number is omitted, the 'default' section is assumed.
  98. # Furthermore, in 'grml-zsh-refcard.tex.in' @@INSERT-aliases@@ is
  99. # exactly the same as @@INSERT-aliases-default@@. If you want a list of
  100. # *all* aliases, for example, use @@INSERT-aliases-all@@.
  101. # zsh profiling
  102. # just execute 'ZSH_PROFILE_RC=1 zsh' and run 'zprof' to get the details
  103. if [[ $ZSH_PROFILE_RC -gt 0 ]] ; then
  104. zmodload zsh/zprof
  105. fi
  106. # load .zshrc.pre to give the user the chance to overwrite the defaults
  107. [[ -r ${ZDOTDIR:-${HOME}}/.zshrc.pre ]] && source ${ZDOTDIR:-${HOME}}/.zshrc.pre
  108. # check for version/system
  109. # check for versions (compatibility reasons)
  110. function is4 () {
  111. [[ $ZSH_VERSION == <4->* ]] && return 0
  112. return 1
  113. }
  114. function is41 () {
  115. [[ $ZSH_VERSION == 4.<1->* || $ZSH_VERSION == <5->* ]] && return 0
  116. return 1
  117. }
  118. function is42 () {
  119. [[ $ZSH_VERSION == 4.<2->* || $ZSH_VERSION == <5->* ]] && return 0
  120. return 1
  121. }
  122. function is425 () {
  123. [[ $ZSH_VERSION == 4.2.<5->* || $ZSH_VERSION == 4.<3->* || $ZSH_VERSION == <5->* ]] && return 0
  124. return 1
  125. }
  126. function is43 () {
  127. [[ $ZSH_VERSION == 4.<3->* || $ZSH_VERSION == <5->* ]] && return 0
  128. return 1
  129. }
  130. function is433 () {
  131. [[ $ZSH_VERSION == 4.3.<3->* || $ZSH_VERSION == 4.<4->* \
  132. || $ZSH_VERSION == <5->* ]] && return 0
  133. return 1
  134. }
  135. function is437 () {
  136. [[ $ZSH_VERSION == 4.3.<7->* || $ZSH_VERSION == 4.<4->* \
  137. || $ZSH_VERSION == <5->* ]] && return 0
  138. return 1
  139. }
  140. function is439 () {
  141. [[ $ZSH_VERSION == 4.3.<9->* || $ZSH_VERSION == 4.<4->* \
  142. || $ZSH_VERSION == <5->* ]] && return 0
  143. return 1
  144. }
  145. #f1# Checks whether or not you're running grml
  146. function isgrml () {
  147. [[ -f /etc/grml_version ]] && return 0
  148. return 1
  149. }
  150. #f1# Checks whether or not you're running a grml cd
  151. function isgrmlcd () {
  152. [[ -f /etc/grml_cd ]] && return 0
  153. return 1
  154. }
  155. if isgrml ; then
  156. #f1# Checks whether or not you're running grml-small
  157. function isgrmlsmall () {
  158. if [[ ${${${(f)"$(</etc/grml_version)"}%% *}##*-} == 'small' ]]; then
  159. return 0
  160. fi
  161. return 1
  162. }
  163. else
  164. function isgrmlsmall () { return 1 }
  165. fi
  166. GRML_OSTYPE=$(uname -s)
  167. function islinux () {
  168. [[ $GRML_OSTYPE == "Linux" ]]
  169. }
  170. function isdarwin () {
  171. [[ $GRML_OSTYPE == "Darwin" ]]
  172. }
  173. function isfreebsd () {
  174. [[ $GRML_OSTYPE == "FreeBSD" ]]
  175. }
  176. function isopenbsd () {
  177. [[ $GRML_OSTYPE == "OpenBSD" ]]
  178. }
  179. function issolaris () {
  180. [[ $GRML_OSTYPE == "SunOS" ]]
  181. }
  182. #f1# are we running within an utf environment?
  183. function isutfenv () {
  184. case "$LANG $CHARSET $LANGUAGE" in
  185. *utf*) return 0 ;;
  186. *UTF*) return 0 ;;
  187. *) return 1 ;;
  188. esac
  189. }
  190. # check for user, if not running as root set $SUDO to sudo
  191. (( EUID != 0 )) && SUDO='sudo' || SUDO=''
  192. # change directory to home on first invocation of zsh
  193. # important for rungetty -> autologin
  194. # Thanks go to Bart Schaefer!
  195. isgrml && function checkhome () {
  196. if [[ -z "$ALREADY_DID_CD_HOME" ]] ; then
  197. export ALREADY_DID_CD_HOME=$HOME
  198. cd
  199. fi
  200. }
  201. # check for zsh v3.1.7+
  202. if ! [[ ${ZSH_VERSION} == 3.1.<7->* \
  203. || ${ZSH_VERSION} == 3.<2->.<->* \
  204. || ${ZSH_VERSION} == <4->.<->* ]] ; then
  205. printf '-!-\n'
  206. printf '-!- In this configuration we try to make use of features, that only\n'
  207. printf '-!- require version 3.1.7 of the shell; That way this setup can be\n'
  208. printf '-!- used with a wide range of zsh versions, while using fairly\n'
  209. printf '-!- advanced features in all supported versions.\n'
  210. printf '-!-\n'
  211. printf '-!- However, you are running zsh version %s.\n' "$ZSH_VERSION"
  212. printf '-!-\n'
  213. printf '-!- While this *may* work, it might as well fail.\n'
  214. printf '-!- Please consider updating to at least version 3.1.7 of zsh.\n'
  215. printf '-!-\n'
  216. printf '-!- DO NOT EXPECT THIS TO WORK FLAWLESSLY!\n'
  217. printf '-!- If it does today, you'\''ve been lucky.\n'
  218. printf '-!-\n'
  219. printf '-!- Ye been warned!\n'
  220. printf '-!-\n'
  221. function zstyle () { : }
  222. fi
  223. # autoload wrapper - use this one instead of autoload directly
  224. # We need to define this function as early as this, because autoloading
  225. # 'is-at-least()' needs it.
  226. function zrcautoload () {
  227. emulate -L zsh
  228. setopt extended_glob
  229. local fdir ffile
  230. local -i ffound
  231. ffile=$1
  232. (( ffound = 0 ))
  233. for fdir in ${fpath} ; do
  234. [[ -e ${fdir}/${ffile} ]] && (( ffound = 1 ))
  235. done
  236. (( ffound == 0 )) && return 1
  237. if [[ $ZSH_VERSION == 3.1.<6-> || $ZSH_VERSION == <4->* ]] ; then
  238. autoload -U ${ffile} || return 1
  239. else
  240. autoload ${ffile} || return 1
  241. fi
  242. return 0
  243. }
  244. # The following is the ‘add-zsh-hook’ function from zsh upstream. It is
  245. # included here to make the setup work with older versions of zsh (prior to
  246. # 4.3.7) in which this function had a bug that triggers annoying errors during
  247. # shell startup. This is exactly upstreams code from f0068edb4888a4d8fe94def,
  248. # with just a few adjustments in coding style to make the function look more
  249. # compact. This definition can be removed as soon as we raise the minimum
  250. # version requirement to 4.3.7 or newer.
  251. function add-zsh-hook () {
  252. # Add to HOOK the given FUNCTION.
  253. # HOOK is one of chpwd, precmd, preexec, periodic, zshaddhistory,
  254. # zshexit, zsh_directory_name (the _functions subscript is not required).
  255. #
  256. # With -d, remove the function from the hook instead; delete the hook
  257. # variable if it is empty.
  258. #
  259. # -D behaves like -d, but pattern characters are active in the function
  260. # name, so any matching function will be deleted from the hook.
  261. #
  262. # Without -d, the FUNCTION is marked for autoload; -U is passed down to
  263. # autoload if that is given, as are -z and -k. (This is harmless if the
  264. # function is actually defined inline.)
  265. emulate -L zsh
  266. local -a hooktypes
  267. hooktypes=(
  268. chpwd precmd preexec periodic zshaddhistory zshexit
  269. zsh_directory_name
  270. )
  271. local usage="Usage: $0 hook function\nValid hooks are:\n $hooktypes"
  272. local opt
  273. local -a autoopts
  274. integer del list help
  275. while getopts "dDhLUzk" opt; do
  276. case $opt in
  277. (d) del=1 ;;
  278. (D) del=2 ;;
  279. (h) help=1 ;;
  280. (L) list=1 ;;
  281. ([Uzk]) autoopts+=(-$opt) ;;
  282. (*) return 1 ;;
  283. esac
  284. done
  285. shift $(( OPTIND - 1 ))
  286. if (( list )); then
  287. typeset -mp "(${1:-${(@j:|:)hooktypes}})_functions"
  288. return $?
  289. elif (( help || $# != 2 || ${hooktypes[(I)$1]} == 0 )); then
  290. print -u$(( 2 - help )) $usage
  291. return $(( 1 - help ))
  292. fi
  293. local hook="${1}_functions"
  294. local fn="$2"
  295. if (( del )); then
  296. # delete, if hook is set
  297. if (( ${(P)+hook} )); then
  298. if (( del == 2 )); then
  299. set -A $hook ${(P)hook:#${~fn}}
  300. else
  301. set -A $hook ${(P)hook:#$fn}
  302. fi
  303. # unset if no remaining entries --- this can give better
  304. # performance in some cases
  305. if (( ! ${(P)#hook} )); then
  306. unset $hook
  307. fi
  308. fi
  309. else
  310. if (( ${(P)+hook} )); then
  311. if (( ${${(P)hook}[(I)$fn]} == 0 )); then
  312. set -A $hook ${(P)hook} $fn
  313. fi
  314. else
  315. set -A $hook $fn
  316. fi
  317. autoload $autoopts -- $fn
  318. fi
  319. }
  320. # Load is-at-least() for more precise version checks Note that this test will
  321. # *always* fail, if the is-at-least function could not be marked for
  322. # autoloading.
  323. zrcautoload is-at-least || function is-at-least () { return 1 }
  324. # set some important options (as early as possible)
  325. # append history list to the history file; this is the default but we make sure
  326. # because it's required for share_history.
  327. setopt append_history
  328. # import new commands from the history file also in other zsh-session
  329. is4 && setopt share_history
  330. # save each command's beginning timestamp and the duration to the history file
  331. setopt extended_history
  332. # If a new command line being added to the history list duplicates an older
  333. # one, the older command is removed from the list
  334. is4 && setopt histignorealldups
  335. # remove command lines from the history list when the first character on the
  336. # line is a space
  337. setopt histignorespace
  338. # if a command is issued that can't be executed as a normal command, and the
  339. # command is the name of a directory, perform the cd command to that directory.
  340. setopt auto_cd
  341. # in order to use #, ~ and ^ for filename generation grep word
  342. # *~(*.gz|*.bz|*.bz2|*.zip|*.Z) -> searches for word not in compressed files
  343. # don't forget to quote '^', '~' and '#'!
  344. setopt extended_glob
  345. # display PID when suspending processes as well
  346. setopt longlistjobs
  347. # report the status of backgrounds jobs immediately
  348. setopt notify
  349. # whenever a command completion is attempted, make sure the entire command path
  350. # is hashed first.
  351. setopt hash_list_all
  352. # not just at the end
  353. setopt completeinword
  354. # Don't send SIGHUP to background processes when the shell exits.
  355. setopt nohup
  356. # make cd push the old directory onto the directory stack.
  357. setopt auto_pushd
  358. # avoid "beep"ing
  359. setopt nobeep
  360. # don't push the same dir twice.
  361. setopt pushd_ignore_dups
  362. # * shouldn't match dotfiles. ever.
  363. setopt noglobdots
  364. # use zsh style word splitting
  365. setopt noshwordsplit
  366. # don't error out when unset parameters are used
  367. setopt unset
  368. # setting some default values
  369. NOCOR=${NOCOR:-0}
  370. NOMENU=${NOMENU:-0}
  371. NOPRECMD=${NOPRECMD:-0}
  372. COMMAND_NOT_FOUND=${COMMAND_NOT_FOUND:-0}
  373. GRML_ZSH_CNF_HANDLER=${GRML_ZSH_CNF_HANDLER:-/usr/share/command-not-found/command-not-found}
  374. GRML_DISPLAY_BATTERY=${GRML_DISPLAY_BATTERY:-${BATTERY:-0}}
  375. GRMLSMALL_SPECIFIC=${GRMLSMALL_SPECIFIC:-1}
  376. ZSH_NO_DEFAULT_LOCALE=${ZSH_NO_DEFAULT_LOCALE:-0}
  377. typeset -ga ls_options
  378. typeset -ga grep_options
  379. # Colors on GNU ls(1)
  380. if ls --color=auto / >/dev/null 2>&1; then
  381. ls_options+=( --color=auto )
  382. # Colors on FreeBSD and OSX ls(1)
  383. elif ls -G / >/dev/null 2>&1; then
  384. ls_options+=( -G )
  385. fi
  386. # Natural sorting order on GNU ls(1)
  387. # OSX and IllumOS have a -v option that is not natural sorting
  388. if ls --version |& grep -q 'GNU' >/dev/null 2>&1 && ls -v / >/dev/null 2>&1; then
  389. ls_options+=( -v )
  390. fi
  391. # Color on GNU and FreeBSD grep(1)
  392. if grep --color=auto -q "a" <<< "a" >/dev/null 2>&1; then
  393. grep_options+=( --color=auto )
  394. fi
  395. # utility functions
  396. # this function checks if a command exists and returns either true
  397. # or false. This avoids using 'which' and 'whence', which will
  398. # avoid problems with aliases for which on certain weird systems. :-)
  399. # Usage: check_com [-c|-g] word
  400. # -c only checks for external commands
  401. # -g does the usual tests and also checks for global aliases
  402. function check_com () {
  403. emulate -L zsh
  404. local -i comonly gatoo
  405. comonly=0
  406. gatoo=0
  407. if [[ $1 == '-c' ]] ; then
  408. comonly=1
  409. shift 1
  410. elif [[ $1 == '-g' ]] ; then
  411. gatoo=1
  412. shift 1
  413. fi
  414. if (( ${#argv} != 1 )) ; then
  415. printf 'usage: check_com [-c|-g] <command>\n' >&2
  416. return 1
  417. fi
  418. if (( comonly > 0 )) ; then
  419. (( ${+commands[$1]} )) && return 0
  420. return 1
  421. fi
  422. if (( ${+commands[$1]} )) \
  423. || (( ${+functions[$1]} )) \
  424. || (( ${+aliases[$1]} )) \
  425. || (( ${+reswords[(r)$1]} )) ; then
  426. return 0
  427. fi
  428. if (( gatoo > 0 )) && (( ${+galiases[$1]} )) ; then
  429. return 0
  430. fi
  431. return 1
  432. }
  433. # creates an alias and precedes the command with
  434. # sudo if $EUID is not zero.
  435. function salias () {
  436. emulate -L zsh
  437. local only=0 ; local multi=0
  438. local key val
  439. while getopts ":hao" opt; do
  440. case $opt in
  441. o) only=1 ;;
  442. a) multi=1 ;;
  443. h)
  444. printf 'usage: salias [-hoa] <alias-expression>\n'
  445. printf ' -h shows this help text.\n'
  446. printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n'
  447. printf ' be careful using this option.\n'
  448. printf ' -o only sets an alias if a preceding sudo would be needed.\n'
  449. return 0
  450. ;;
  451. *) salias -h >&2; return 1 ;;
  452. esac
  453. done
  454. shift "$((OPTIND-1))"
  455. if (( ${#argv} > 1 )) ; then
  456. printf 'Too many arguments %s\n' "${#argv}"
  457. return 1
  458. fi
  459. key="${1%%\=*}" ; val="${1#*\=}"
  460. if (( EUID == 0 )) && (( only == 0 )); then
  461. alias -- "${key}=${val}"
  462. elif (( EUID > 0 )) ; then
  463. (( multi > 0 )) && val="${val// ; / ; sudo }"
  464. alias -- "${key}=sudo ${val}"
  465. fi
  466. return 0
  467. }
  468. # Check if we can read given files and source those we can.
  469. function xsource () {
  470. if (( ${#argv} < 1 )) ; then
  471. printf 'usage: xsource FILE(s)...\n' >&2
  472. return 1
  473. fi
  474. while (( ${#argv} > 0 )) ; do
  475. [[ -r "$1" ]] && source "$1"
  476. shift
  477. done
  478. return 0
  479. }
  480. # Check if we can read a given file and 'cat(1)' it.
  481. function xcat () {
  482. emulate -L zsh
  483. if (( ${#argv} != 1 )) ; then
  484. printf 'usage: xcat FILE\n' >&2
  485. return 1
  486. fi
  487. [[ -r $1 ]] && cat $1
  488. return 0
  489. }
  490. # Remove these functions again, they are of use only in these
  491. # setup files. This should be called at the end of .zshrc.
  492. function xunfunction () {
  493. emulate -L zsh
  494. local -a funcs
  495. local func
  496. funcs=(salias xcat xsource xunfunction zrcautoload zrcautozle)
  497. for func in $funcs ; do
  498. [[ -n ${functions[$func]} ]] \
  499. && unfunction $func
  500. done
  501. return 0
  502. }
  503. # this allows us to stay in sync with grml's zshrc and put own
  504. # modifications in ~/.zshrc.local
  505. function zrclocal () {
  506. xsource "/etc/zsh/zshrc.local"
  507. xsource "${ZDOTDIR:-${HOME}}/.zshrc.local"
  508. return 0
  509. }
  510. # locale setup
  511. if (( ZSH_NO_DEFAULT_LOCALE == 0 )); then
  512. xsource "/etc/default/locale"
  513. fi
  514. for var in LANG LC_ALL LC_MESSAGES ; do
  515. [[ -n ${(P)var} ]] && export $var
  516. done
  517. builtin unset -v var
  518. # set some variables
  519. if check_com -c vim ; then
  520. #v#
  521. export EDITOR=${EDITOR:-vim}
  522. else
  523. export EDITOR=${EDITOR:-vi}
  524. fi
  525. #v#
  526. export PAGER=${PAGER:-less}
  527. #v#
  528. export MAIL=${MAIL:-/var/mail/$USER}
  529. # color setup for ls:
  530. check_com -c dircolors && eval $(dircolors -b)
  531. # color setup for ls on OS X / FreeBSD:
  532. isdarwin && export CLICOLOR=1
  533. isfreebsd && export CLICOLOR=1
  534. # do MacPorts setup on darwin
  535. if isdarwin && [[ -d /opt/local ]]; then
  536. # Note: PATH gets set in /etc/zprofile on Darwin, so this can't go into
  537. # zshenv.
  538. PATH="/opt/local/bin:/opt/local/sbin:$PATH"
  539. MANPATH="/opt/local/share/man:$MANPATH"
  540. fi
  541. # do Fink setup on darwin
  542. isdarwin && xsource /sw/bin/init.sh
  543. # load our function and completion directories
  544. for fdir in /usr/share/grml/zsh/completion /usr/share/grml/zsh/functions; do
  545. fpath=( ${fdir} ${fdir}/**/*(/N) ${fpath} )
  546. done
  547. typeset -aU ffiles
  548. ffiles=(/usr/share/grml/zsh/functions/**/[^_]*[^~](N.:t))
  549. (( ${#ffiles} > 0 )) && autoload -U "${ffiles[@]}"
  550. unset -v fdir ffiles
  551. # support colors in less
  552. export LESS_TERMCAP_mb=$'\E[01;31m'
  553. export LESS_TERMCAP_md=$'\E[01;31m'
  554. export LESS_TERMCAP_me=$'\E[0m'
  555. export LESS_TERMCAP_se=$'\E[0m'
  556. export LESS_TERMCAP_so=$'\E[01;44;33m'
  557. export LESS_TERMCAP_ue=$'\E[0m'
  558. export LESS_TERMCAP_us=$'\E[01;32m'
  559. # mailchecks
  560. MAILCHECK=30
  561. # report about cpu-/system-/user-time of command if running longer than
  562. # 5 seconds
  563. REPORTTIME=5
  564. # watch for everyone but me and root
  565. watch=(notme root)
  566. # automatically remove duplicates from these arrays
  567. typeset -U path PATH cdpath CDPATH fpath FPATH manpath MANPATH
  568. # Load a few modules
  569. is4 && \
  570. for mod in parameter complist deltochar mathfunc ; do
  571. zmodload -i zsh/${mod} 2>/dev/null || print "Notice: no ${mod} available :("
  572. done && builtin unset -v mod
  573. # autoload zsh modules when they are referenced
  574. if is4 ; then
  575. zmodload -a zsh/stat zstat
  576. zmodload -a zsh/zpty zpty
  577. zmodload -ap zsh/mapfile mapfile
  578. fi
  579. # completion system
  580. COMPDUMPFILE=${COMPDUMPFILE:-${ZDOTDIR:-${HOME}}/.zcompdump}
  581. if zrcautoload compinit ; then
  582. typeset -a tmp
  583. zstyle -a ':grml:completion:compinit' arguments tmp
  584. compinit -d ${COMPDUMPFILE} "${tmp[@]}" || print 'Notice: no compinit available :('
  585. unset tmp
  586. else
  587. print 'Notice: no compinit available :('
  588. function compdef { }
  589. fi
  590. # completion system
  591. # called later (via is4 && grmlcomp)
  592. # note: use 'zstyle' for getting current settings
  593. # press ^xh (control-x h) for getting tags in context; ^x? (control-x ?) to run complete_debug with trace output
  594. function grmlcomp () {
  595. # TODO: This could use some additional information
  596. # Make sure the completion system is initialised
  597. (( ${+_comps} )) || return 1
  598. # allow one error for every three characters typed in approximate completer
  599. zstyle ':completion:*:approximate:' max-errors 'reply=( $((($#PREFIX+$#SUFFIX)/3 )) numeric )'
  600. # don't complete backup files as executables
  601. zstyle ':completion:*:complete:-command-::commands' ignored-patterns '(aptitude-*|*\~)'
  602. # start menu completion only if it could find no unambiguous initial string
  603. zstyle ':completion:*:correct:*' insert-unambiguous true
  604. zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}'
  605. zstyle ':completion:*:correct:*' original true
  606. # activate color-completion
  607. zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
  608. # format on completion
  609. zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}completing %B%d%b%{\e[0m%}'
  610. # automatically complete 'cd -<tab>' and 'cd -<ctrl-d>' with menu
  611. # zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
  612. # insert all expansions for expand completer
  613. zstyle ':completion:*:expand:*' tag-order all-expansions
  614. zstyle ':completion:*:history-words' list false
  615. # activate menu
  616. zstyle ':completion:*:history-words' menu yes
  617. # ignore duplicate entries
  618. zstyle ':completion:*:history-words' remove-all-dups yes
  619. zstyle ':completion:*:history-words' stop yes
  620. # match uppercase from lowercase
  621. zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
  622. # separate matches into groups
  623. zstyle ':completion:*:matches' group 'yes'
  624. zstyle ':completion:*' group-name ''
  625. if [[ "$NOMENU" -eq 0 ]] ; then
  626. # if there are more than 5 options allow selecting from a menu
  627. zstyle ':completion:*' menu select=5
  628. else
  629. # don't use any menus at all
  630. setopt no_auto_menu
  631. fi
  632. zstyle ':completion:*:messages' format '%d'
  633. zstyle ':completion:*:options' auto-description '%d'
  634. # describe options in full
  635. zstyle ':completion:*:options' description 'yes'
  636. # on processes completion complete all user processes
  637. zstyle ':completion:*:processes' command 'ps -au$USER'
  638. # offer indexes before parameters in subscripts
  639. zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
  640. # provide verbose completion information
  641. zstyle ':completion:*' verbose true
  642. # recent (as of Dec 2007) zsh versions are able to provide descriptions
  643. # for commands (read: 1st word in the line) that it will list for the user
  644. # to choose from. The following disables that, because it's not exactly fast.
  645. zstyle ':completion:*:-command-:*:' verbose false
  646. # set format for warnings
  647. zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for:%{\e[0m%} %d'
  648. # define files to ignore for zcompile
  649. zstyle ':completion:*:*:zcompile:*' ignored-patterns '(*~|*.zwc)'
  650. zstyle ':completion:correct:' prompt 'correct to: %e'
  651. # Ignore completion functions for commands you don't have:
  652. zstyle ':completion::(^approximate*):*:functions' ignored-patterns '_*'
  653. # Provide more processes in completion of programs like killall:
  654. zstyle ':completion:*:processes-names' command 'ps c -u ${USER} -o command | uniq'
  655. # complete manual by their section
  656. zstyle ':completion:*:manuals' separate-sections true
  657. zstyle ':completion:*:manuals.*' insert-sections true
  658. zstyle ':completion:*:man:*' menu yes select
  659. # Search path for sudo completion
  660. zstyle ':completion:*:sudo:*' command-path /usr/local/sbin \
  661. /usr/local/bin \
  662. /usr/sbin \
  663. /usr/bin \
  664. /sbin \
  665. /bin \
  666. /usr/X11R6/bin
  667. # provide .. as a completion
  668. zstyle ':completion:*' special-dirs ..
  669. # run rehash on completion so new installed program are found automatically:
  670. function _force_rehash () {
  671. (( CURRENT == 1 )) && rehash
  672. return 1
  673. }
  674. ## correction
  675. # some people don't like the automatic correction - so run 'NOCOR=1 zsh' to deactivate it
  676. if [[ "$NOCOR" -gt 0 ]] ; then
  677. zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _files _ignored
  678. setopt nocorrect
  679. else
  680. # try to be smart about when to use what completer...
  681. setopt correct
  682. zstyle -e ':completion:*' completer '
  683. if [[ $_last_try != "$HISTNO$BUFFER$CURSOR" ]] ; then
  684. _last_try="$HISTNO$BUFFER$CURSOR"
  685. reply=(_complete _match _ignored _prefix _files)
  686. else
  687. if [[ $words[1] == (rm|mv) ]] ; then
  688. reply=(_complete _files)
  689. else
  690. reply=(_oldlist _expand _force_rehash _complete _ignored _correct _approximate _files)
  691. fi
  692. fi'
  693. fi
  694. # command for process lists, the local web server details and host completion
  695. zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html'
  696. # Some functions, like _apt and _dpkg, are very slow. We can use a cache in
  697. # order to speed things up
  698. if [[ ${GRML_COMP_CACHING:-yes} == yes ]]; then
  699. GRML_COMP_CACHE_DIR=${GRML_COMP_CACHE_DIR:-${ZDOTDIR:-$HOME}/.cache}
  700. if [[ ! -d ${GRML_COMP_CACHE_DIR} ]]; then
  701. command mkdir -p "${GRML_COMP_CACHE_DIR}"
  702. fi
  703. zstyle ':completion:*' use-cache yes
  704. zstyle ':completion:*:complete:*' cache-path "${GRML_COMP_CACHE_DIR}"
  705. fi
  706. # host completion
  707. if is42 ; then
  708. [[ -r ~/.ssh/config ]] && _ssh_config_hosts=(${${(s: :)${(ps:\t:)${${(@M)${(f)"$(<$HOME/.ssh/config)"}:#Host *}#Host }}}:#*[*?]*}) || _ssh_config_hosts=()
  709. [[ -r ~/.ssh/known_hosts ]] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
  710. [[ -r /etc/hosts ]] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}##[:blank:]#[^[:blank:]]#}}} || _etc_hosts=()
  711. else
  712. _ssh_config_hosts=()
  713. _ssh_hosts=()
  714. _etc_hosts=()
  715. fi
  716. hosts=(
  717. $(hostname)
  718. "$_ssh_config_hosts[@]"
  719. "$_ssh_hosts[@]"
  720. "$_etc_hosts[@]"
  721. localhost
  722. )
  723. zstyle ':completion:*:hosts' hosts $hosts
  724. # TODO: so, why is this here?
  725. # zstyle '*' hosts $hosts
  726. # use generic completion system for programs not yet defined; (_gnu_generic works
  727. # with commands that provide a --help option with "standard" gnu-like output.)
  728. for compcom in cp deborphan df feh fetchipac gpasswd head hnb ipacsum mv \
  729. pal stow uname ; do
  730. [[ -z ${_comps[$compcom]} ]] && compdef _gnu_generic ${compcom}
  731. done; unset compcom
  732. # see upgrade function in this file
  733. compdef _hosts upgrade
  734. }
  735. # Keyboard setup: The following is based on the same code, we wrote for
  736. # debian's setup. It ensures the terminal is in the right mode, when zle is
  737. # active, so the values from $terminfo are valid. Therefore, this setup should
  738. # work on all systems, that have support for `terminfo'. It also requires the
  739. # zsh in use to have the `zsh/terminfo' module built.
  740. #
  741. # If you are customising your `zle-line-init()' or `zle-line-finish()'
  742. # functions, make sure you call the following utility functions in there:
  743. #
  744. # - zle-line-init(): zle-smkx
  745. # - zle-line-finish(): zle-rmkx
  746. # Use emacs-like key bindings by default:
  747. bindkey -e
  748. # Custom widgets:
  749. ## beginning-of-line OR beginning-of-buffer OR beginning of history
  750. ## by: Bart Schaefer <schaefer@brasslantern.com>, Bernhard Tittelbach
  751. function beginning-or-end-of-somewhere () {
  752. local hno=$HISTNO
  753. if [[ ( "${LBUFFER[-1]}" == $'\n' && "${WIDGET}" == beginning-of* ) || \
  754. ( "${RBUFFER[1]}" == $'\n' && "${WIDGET}" == end-of* ) ]]; then
  755. zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
  756. else
  757. zle .${WIDGET:s/somewhere/line-hist/} "$@"
  758. if (( HISTNO != hno )); then
  759. zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
  760. fi
  761. fi
  762. }
  763. zle -N beginning-of-somewhere beginning-or-end-of-somewhere
  764. zle -N end-of-somewhere beginning-or-end-of-somewhere
  765. # add a command line to the shells history without executing it
  766. function commit-to-history () {
  767. print -s ${(z)BUFFER}
  768. zle send-break
  769. }
  770. zle -N commit-to-history
  771. # only slash should be considered as a word separator:
  772. function slash-backward-kill-word () {
  773. local WORDCHARS="${WORDCHARS:s@/@}"
  774. # zle backward-word
  775. zle backward-kill-word
  776. }
  777. zle -N slash-backward-kill-word
  778. # a generic accept-line wrapper
  779. # This widget can prevent unwanted autocorrections from command-name
  780. # to _command-name, rehash automatically on enter and call any number
  781. # of builtin and user-defined widgets in different contexts.
  782. #
  783. # For a broader description, see:
  784. # <http://bewatermyfriend.org/posts/2007/12-26.11-50-38-tooltime.html>
  785. #
  786. # The code is imported from the file 'zsh/functions/accept-line' from
  787. # <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>, which
  788. # distributed under the same terms as zsh itself.
  789. # A newly added command will may not be found or will cause false
  790. # correction attempts, if you got auto-correction set. By setting the
  791. # following style, we force accept-line() to rehash, if it cannot
  792. # find the first word on the command line in the $command[] hash.
  793. zstyle ':acceptline:*' rehash true
  794. function Accept-Line () {
  795. setopt localoptions noksharrays
  796. local -a subs
  797. local -xi aldone
  798. local sub
  799. local alcontext=${1:-$alcontext}
  800. zstyle -a ":acceptline:${alcontext}" actions subs
  801. (( ${#subs} < 1 )) && return 0
  802. (( aldone = 0 ))
  803. for sub in ${subs} ; do
  804. [[ ${sub} == 'accept-line' ]] && sub='.accept-line'
  805. zle ${sub}
  806. (( aldone > 0 )) && break
  807. done
  808. }
  809. function Accept-Line-getdefault () {
  810. emulate -L zsh
  811. local default_action
  812. zstyle -s ":acceptline:${alcontext}" default_action default_action
  813. case ${default_action} in
  814. ((accept-line|))
  815. printf ".accept-line"
  816. ;;
  817. (*)
  818. printf ${default_action}
  819. ;;
  820. esac
  821. }
  822. function Accept-Line-HandleContext () {
  823. zle Accept-Line
  824. default_action=$(Accept-Line-getdefault)
  825. zstyle -T ":acceptline:${alcontext}" call_default \
  826. && zle ${default_action}
  827. }
  828. function accept-line () {
  829. setopt localoptions noksharrays
  830. local -a cmdline
  831. local -x alcontext
  832. local buf com fname format msg default_action
  833. alcontext='default'
  834. buf="${BUFFER}"
  835. cmdline=(${(z)BUFFER})
  836. com="${cmdline[1]}"
  837. fname="_${com}"
  838. Accept-Line 'preprocess'
  839. zstyle -t ":acceptline:${alcontext}" rehash \
  840. && [[ -z ${commands[$com]} ]] \
  841. && rehash
  842. if [[ -n ${com} ]] \
  843. && [[ -n ${reswords[(r)$com]} ]] \
  844. || [[ -n ${aliases[$com]} ]] \
  845. || [[ -n ${functions[$com]} ]] \
  846. || [[ -n ${builtins[$com]} ]] \
  847. || [[ -n ${commands[$com]} ]] ; then
  848. # there is something sensible to execute, just do it.
  849. alcontext='normal'
  850. Accept-Line-HandleContext
  851. return
  852. fi
  853. if [[ -o correct ]] \
  854. || [[ -o correctall ]] \
  855. && [[ -n ${functions[$fname]} ]] ; then
  856. # nothing there to execute but there is a function called
  857. # _command_name; a completion widget. Makes no sense to
  858. # call it on the commandline, but the correct{,all} options
  859. # will ask for it nevertheless, so warn the user.
  860. if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
  861. # Okay, we warned the user before, he called us again,
  862. # so have it his way.
  863. alcontext='force'
  864. Accept-Line-HandleContext
  865. return
  866. fi
  867. if zstyle -t ":acceptline:${alcontext}" nocompwarn ; then
  868. alcontext='normal'
  869. Accept-Line-HandleContext
  870. else
  871. # prepare warning message for the user, configurable via zstyle.
  872. zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
  873. if [[ -z ${msg} ]] ; then
  874. msg="%c will not execute and completion %f exists."
  875. fi
  876. zformat -f msg "${msg}" "c:${com}" "f:${fname}"
  877. zle -M -- "${msg}"
  878. fi
  879. return
  880. elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
  881. # If we are here, the commandline contains something that is not
  882. # executable, which is neither subject to _command_name correction
  883. # and is not empty. might be a variable assignment
  884. alcontext='misc'
  885. Accept-Line-HandleContext
  886. return
  887. fi
  888. # If we got this far, the commandline only contains whitespace, or is empty.
  889. alcontext='empty'
  890. Accept-Line-HandleContext
  891. }
  892. zle -N accept-line
  893. zle -N Accept-Line
  894. zle -N Accept-Line-HandleContext
  895. # power completion / abbreviation expansion / buffer expansion
  896. # see http://zshwiki.org/home/examples/zleiab for details
  897. # less risky than the global aliases but powerful as well
  898. # just type the abbreviation key and afterwards 'ctrl-x .' to expand it
  899. declare -A abk
  900. setopt extendedglob
  901. setopt interactivecomments
  902. abk=(
  903. # key # value (#d additional doc string)
  904. #A# start
  905. '...' '../..'
  906. '....' '../../..'
  907. 'BG' '& exit'
  908. 'C' '| wc -l'
  909. 'G' '|& grep '${grep_options:+"${grep_options[*]}"}
  910. 'H' '| head'
  911. 'Hl' ' --help |& less -r' #d (Display help in pager)
  912. 'L' '| less'
  913. 'LL' '|& less -r'
  914. 'M' '| most'
  915. 'N' '&>/dev/null' #d (No Output)
  916. 'R' '| tr A-z N-za-m' #d (ROT13)
  917. 'SL' '| sort | less'
  918. 'S' '| sort -u'
  919. 'T' '| tail'
  920. 'V' '|& vim -'
  921. #A# end
  922. 'co' './configure && make && sudo make install'
  923. )
  924. function zleiab () {
  925. emulate -L zsh
  926. setopt extendedglob
  927. local MATCH
  928. LBUFFER=${LBUFFER%%(#m)[.\-+:|_a-zA-Z0-9]#}
  929. LBUFFER+=${abk[$MATCH]:-$MATCH}
  930. }
  931. zle -N zleiab
  932. function help-show-abk () {
  933. zle -M "$(print "Available abbreviations for expansion:"; print -a -C 2 ${(kv)abk})"
  934. }
  935. zle -N help-show-abk
  936. # press "ctrl-x d" to insert the actual date in the form yyyy-mm-dd
  937. function insert-datestamp () { LBUFFER+=${(%):-'%D{%Y-%m-%d}'}; }
  938. zle -N insert-datestamp
  939. # press esc-m for inserting last typed word again (thanks to caphuso!)
  940. function insert-last-typed-word () { zle insert-last-word -- 0 -1 };
  941. zle -N insert-last-typed-word;
  942. function grml-zsh-fg () {
  943. if (( ${#jobstates} )); then
  944. zle .push-input
  945. [[ -o hist_ignore_space ]] && BUFFER=' ' || BUFFER=''
  946. BUFFER="${BUFFER}fg"
  947. zle .accept-line
  948. else
  949. zle -M 'No background jobs. Doing nothing.'
  950. fi
  951. }
  952. zle -N grml-zsh-fg
  953. # run command line as user root via sudo:
  954. function sudo-command-line () {
  955. [[ -z $BUFFER ]] && zle up-history
  956. if [[ $BUFFER != sudo\ * ]]; then
  957. BUFFER="sudo $BUFFER"
  958. CURSOR=$(( CURSOR+5 ))
  959. fi
  960. }
  961. zle -N sudo-command-line
  962. ### jump behind the first word on the cmdline.
  963. ### useful to add options.
  964. function jump_after_first_word () {
  965. local words
  966. words=(${(z)BUFFER})
  967. if (( ${#words} <= 1 )) ; then
  968. CURSOR=${#BUFFER}
  969. else
  970. CURSOR=${#${words[1]}}
  971. fi
  972. }
  973. zle -N jump_after_first_word
  974. #f5# Create directory under cursor or the selected area
  975. function inplaceMkDirs () {
  976. # Press ctrl-xM to create the directory under the cursor or the selected area.
  977. # To select an area press ctrl-@ or ctrl-space and use the cursor.
  978. # Use case: you type "mv abc ~/testa/testb/testc/" and remember that the
  979. # directory does not exist yet -> press ctrl-XM and problem solved
  980. local PATHTOMKDIR
  981. if ((REGION_ACTIVE==1)); then
  982. local F=$MARK T=$CURSOR
  983. if [[ $F -gt $T ]]; then
  984. F=${CURSOR}
  985. T=${MARK}
  986. fi
  987. # get marked area from buffer and eliminate whitespace
  988. PATHTOMKDIR=${BUFFER[F+1,T]%%[[:space:]]##}
  989. PATHTOMKDIR=${PATHTOMKDIR##[[:space:]]##}
  990. else
  991. local bufwords iword
  992. bufwords=(${(z)LBUFFER})
  993. iword=${#bufwords}
  994. bufwords=(${(z)BUFFER})
  995. PATHTOMKDIR="${(Q)bufwords[iword]}"
  996. fi
  997. [[ -z "${PATHTOMKDIR}" ]] && return 1
  998. PATHTOMKDIR=${~PATHTOMKDIR}
  999. if [[ -e "${PATHTOMKDIR}" ]]; then
  1000. zle -M " path already exists, doing nothing"
  1001. else
  1002. zle -M "$(mkdir -p -v "${PATHTOMKDIR}")"
  1003. zle end-of-line
  1004. fi
  1005. }
  1006. zle -N inplaceMkDirs
  1007. #v1# set number of lines to display per page
  1008. HELP_LINES_PER_PAGE=20
  1009. #v1# set location of help-zle cache file
  1010. HELP_ZLE_CACHE_FILE=~/.cache/zsh_help_zle_lines.zsh
  1011. # helper function for help-zle, actually generates the help text
  1012. function help_zle_parse_keybindings () {
  1013. emulate -L zsh
  1014. setopt extendedglob
  1015. unsetopt ksharrays #indexing starts at 1
  1016. #v1# choose files that help-zle will parse for keybindings
  1017. ((${+HELPZLE_KEYBINDING_FILES})) || HELPZLE_KEYBINDING_FILES=( /etc/zsh/zshrc ~/.zshrc.pre ~/.zshrc ~/.zshrc.local )
  1018. if [[ -r $HELP_ZLE_CACHE_FILE ]]; then
  1019. local load_cache=0
  1020. local f
  1021. for f ($HELPZLE_KEYBINDING_FILES) [[ $f -nt $HELP_ZLE_CACHE_FILE ]] && load_cache=1
  1022. [[ $load_cache -eq 0 ]] && . $HELP_ZLE_CACHE_FILE && return
  1023. fi
  1024. #fill with default keybindings, possibly to be overwritten in a file later
  1025. #Note that due to zsh inconsistency on escaping assoc array keys, we encase the key in '' which we will remove later
  1026. local -A help_zle_keybindings
  1027. help_zle_keybindings['<Ctrl>@']="set MARK"
  1028. help_zle_keybindings['<Ctrl>x<Ctrl>j']="vi-join lines"
  1029. help_zle_keybindings['<Ctrl>x<Ctrl>b']="jump to matching brace"
  1030. help_zle_keybindings['<Ctrl>x<Ctrl>u']="undo"
  1031. help_zle_keybindings['<Ctrl>_']="undo"
  1032. help_zle_keybindings['<Ctrl>x<Ctrl>f<c>']="find <c> in cmdline"
  1033. help_zle_keybindings['<Ctrl>a']="goto beginning of line"
  1034. help_zle_keybindings['<Ctrl>e']="goto end of line"
  1035. help_zle_keybindings['<Ctrl>t']="transpose charaters"
  1036. help_zle_keybindings['<Alt>t']="transpose words"
  1037. help_zle_keybindings['<Alt>s']="spellcheck word"
  1038. help_zle_keybindings['<Ctrl>k']="backward kill buffer"
  1039. help_zle_keybindings['<Ctrl>u']="forward kill buffer"
  1040. help_zle_keybindings['<Ctrl>y']="insert previously killed word/string"
  1041. help_zle_keybindings["<Alt>'"]="quote line"
  1042. help_zle_keybindings['<Alt>"']="quote from mark to cursor"
  1043. help_zle_keybindings['<Alt><arg>']="repeat next cmd/char <arg> times (<Alt>-<Alt>1<Alt>0a -> -10 times 'a')"
  1044. help_zle_keybindings['<Alt>u']="make next word Uppercase"
  1045. help_zle_keybindings['<Alt>l']="make next word lowercase"
  1046. help_zle_keybindings['<Ctrl>xG']="preview expansion under cursor"
  1047. help_zle_keybindings['<Alt>q']="push current CL into background, freeing it. Restore on next CL"
  1048. help_zle_keybindings['<Alt>.']="insert (and interate through) last word from prev CLs"
  1049. help_zle_keybindings['<Alt>,']="complete word from newer history (consecutive hits)"
  1050. help_zle_keybindings['<Alt>m']="repeat last typed word on current CL"
  1051. help_zle_keybindings['<Ctrl>v']="insert next keypress symbol literally (e.g. for bindkey)"
  1052. help_zle_keybindings['!!:n*<Tab>']="insert last n arguments of last command"
  1053. help_zle_keybindings['!!:n-<Tab>']="insert arguments n..N-2 of last command (e.g. mv s s d)"
  1054. help_zle_keybindings['<Alt>h']="show help/manpage for current command"
  1055. #init global variables
  1056. unset help_zle_lines help_zle_sln
  1057. typeset -g -a help_zle_lines
  1058. typeset -g help_zle_sln=1
  1059. local k v f cline
  1060. local lastkeybind_desc contents #last description starting with #k# that we found
  1061. local num_lines_elapsed=0 #number of lines between last description and keybinding
  1062. #search config files in the order they a called (and thus the order in which they overwrite keybindings)
  1063. for f in $HELPZLE_KEYBINDING_FILES; do
  1064. [[ -r "$f" ]] || continue #not readable ? skip it
  1065. contents="$(<$f)"
  1066. for cline in "${(f)contents}"; do
  1067. #zsh pattern: matches lines like: #k# ..............
  1068. if [[ "$cline" == (#s)[[:space:]]#\#k\#[[:space:]]##(#b)(*)[[:space:]]#(#e) ]]; then
  1069. lastkeybind_desc="$match[*]"
  1070. num_lines_elapsed=0
  1071. #zsh pattern: matches lines that set a keybinding using bind2map, bindkey or compdef -k
  1072. # ignores lines that are commentend out
  1073. # grabs first in '' or "" enclosed string with length between 1 and 6 characters
  1074. elif [[ "$cline" == [^#]#(bind2maps[[:space:]](*)-s|bindkey|compdef -k)[[:space:]](*)(#b)(\"((?)(#c1,6))\"|\'((?)(#c1,6))\')(#B)(*) ]]; then
  1075. #description previously found ? description not more than 2 lines away ? keybinding not empty ?
  1076. if [[ -n $lastkeybind_desc && $num_lines_elapsed -lt 2 && -n $match[1] ]]; then
  1077. #substitute keybinding string with something readable
  1078. k=${${${${${${${match[1]/\\e\^h/<Alt><BS>}/\\e\^\?/<Alt><BS>}/\\e\[5~/<PageUp>}/\\e\[6~/<PageDown>}//(\\e|\^\[)/<Alt>}//\^/<Ctrl>}/3~/<Alt><Del>}
  1079. #put keybinding in assoc array, possibly overwriting defaults or stuff found in earlier files
  1080. #Note that we are extracting the keybinding-string including the quotes (see Note at beginning)
  1081. help_zle_keybindings[${k}]=$lastkeybind_desc
  1082. fi
  1083. lastkeybind_desc=""
  1084. else
  1085. ((num_lines_elapsed++))
  1086. fi
  1087. done
  1088. done
  1089. unset contents
  1090. #calculate length of keybinding column
  1091. local kstrlen=0
  1092. for k (${(k)help_zle_keybindings[@]}) ((kstrlen < ${#k})) && kstrlen=${#k}
  1093. #convert the assoc array into preformated lines, which we are able to sort
  1094. for k v in ${(kv)help_zle_keybindings[@]}; do
  1095. #pad keybinding-string to kstrlen chars and remove outermost characters (i.e. the quotes)
  1096. help_zle_lines+=("${(r:kstrlen:)k[2,-2]}${v}")
  1097. done
  1098. #sort lines alphabetically
  1099. help_zle_lines=("${(i)help_zle_lines[@]}")
  1100. [[ -d ${HELP_ZLE_CACHE_FILE:h} ]] || mkdir -p "${HELP_ZLE_CACHE_FILE:h}"
  1101. echo "help_zle_lines=(${(q)help_zle_lines[@]})" >| $HELP_ZLE_CACHE_FILE
  1102. zcompile $HELP_ZLE_CACHE_FILE
  1103. }
  1104. typeset -g help_zle_sln
  1105. typeset -g -a help_zle_lines
  1106. # Provides (partially autogenerated) help on keybindings and the zsh line editor
  1107. function help-zle () {
  1108. emulate -L zsh
  1109. unsetopt ksharrays #indexing starts at 1
  1110. #help lines already generated ? no ? then do it
  1111. [[ ${+functions[help_zle_parse_keybindings]} -eq 1 ]] && {help_zle_parse_keybindings && unfunction help_zle_parse_keybindings}
  1112. #already displayed all lines ? go back to the start
  1113. [[ $help_zle_sln -gt ${#help_zle_lines} ]] && help_zle_sln=1
  1114. local sln=$help_zle_sln
  1115. #note that help_zle_sln is a global var, meaning we remember the last page we viewed
  1116. help_zle_sln=$((help_zle_sln + HELP_LINES_PER_PAGE))
  1117. zle -M "${(F)help_zle_lines[sln,help_zle_sln-1]}"
  1118. }
  1119. zle -N help-zle
  1120. ## complete word from currently visible Screen or Tmux buffer.
  1121. if check_com -c screen || check_com -c tmux; then
  1122. function _complete_screen_display () {
  1123. [[ "$TERM" != "screen" ]] && return 1
  1124. local TMPFILE=$(mktemp)
  1125. local -U -a _screen_display_wordlist
  1126. trap "rm -f $TMPFILE" EXIT
  1127. # fill array with contents from screen hardcopy
  1128. if ((${+TMUX})); then
  1129. #works, but crashes tmux below version 1.4
  1130. #luckily tmux -V option to ask for version, was also added in 1.4
  1131. tmux -V &>/dev/null || return
  1132. tmux -q capture-pane \; save-buffer -b 0 $TMPFILE \; delete-buffer -b 0
  1133. else
  1134. screen -X hardcopy $TMPFILE
  1135. # screen sucks, it dumps in latin1, apparently always. so recode it
  1136. # to system charset
  1137. check_com recode && recode latin1 $TMPFILE
  1138. fi
  1139. _screen_display_wordlist=( ${(QQ)$(<$TMPFILE)} )
  1140. # remove PREFIX to be completed from that array
  1141. _screen_display_wordlist[${_screen_display_wordlist[(i)$PREFIX]}]=""
  1142. compadd -a _screen_display_wordlist
  1143. }
  1144. #m# k CTRL-x\,\,\,S Complete word from GNU screen buffer
  1145. bindkey -r "^xS"
  1146. compdef -k _complete_screen_display complete-word '^xS'
  1147. fi
  1148. # Load a few more functions and tie them to widgets, so they can be bound:
  1149. function zrcautozle () {
  1150. emulate -L zsh
  1151. local fnc=$1
  1152. zrcautoload $fnc && zle -N $fnc
  1153. }
  1154. function zrcgotwidget () {
  1155. (( ${+widgets[$1]} ))
  1156. }
  1157. function zrcgotkeymap () {
  1158. [[ -n ${(M)keymaps:#$1} ]]
  1159. }
  1160. zrcautozle insert-files
  1161. zrcautozle edit-command-line
  1162. zrcautozle insert-unicode-char
  1163. if zrcautoload history-search-end; then
  1164. zle -N history-beginning-search-backward-end history-search-end
  1165. zle -N history-beginning-search-forward-end history-search-end
  1166. fi
  1167. zle -C hist-complete complete-word _generic
  1168. zstyle ':completion:hist-complete:*' completer _history
  1169. # The actual terminal setup hooks and bindkey-calls:
  1170. # An array to note missing features to ease diagnosis in case of problems.
  1171. typeset -ga grml_missing_features
  1172. function zrcbindkey () {
  1173. if (( ARGC )) && zrcgotwidget ${argv[-1]}; then
  1174. bindkey "$@"
  1175. fi
  1176. }
  1177. function bind2maps () {
  1178. local i sequence widget
  1179. local -a maps
  1180. while [[ "$1" != "--" ]]; do
  1181. maps+=( "$1" )
  1182. shift
  1183. done
  1184. shift
  1185. if [[ "$1" == "-s" ]]; then
  1186. shift
  1187. sequence="$1"
  1188. else
  1189. sequence="${key[$1]}"
  1190. fi
  1191. widget="$2"
  1192. [[ -z "$sequence" ]] && return 1
  1193. for i in "${maps[@]}"; do
  1194. zrcbindkey -M "$i" "$sequence" "$widget"
  1195. done
  1196. }
  1197. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  1198. function zle-smkx () {
  1199. emulate -L zsh
  1200. printf '%s' ${terminfo[smkx]}
  1201. }
  1202. function zle-rmkx () {
  1203. emulate -L zsh
  1204. printf '%s' ${terminfo[rmkx]}
  1205. }
  1206. function zle-line-init () {
  1207. zle-smkx
  1208. }
  1209. function zle-line-finish () {
  1210. zle-rmkx
  1211. }
  1212. zle -N zle-line-init
  1213. zle -N zle-line-finish
  1214. else
  1215. for i in {s,r}mkx; do
  1216. (( ${+terminfo[$i]} )) || grml_missing_features+=($i)
  1217. done
  1218. unset i
  1219. fi
  1220. typeset -A key
  1221. key=(
  1222. Home "${terminfo[khome]}"
  1223. End "${terminfo[kend]}"
  1224. Insert "${terminfo[kich1]}"
  1225. Delete "${terminfo[kdch1]}"
  1226. Up "${terminfo[kcuu1]}"
  1227. Down "${terminfo[kcud1]}"
  1228. Left "${terminfo[kcub1]}"
  1229. Right "${terminfo[kcuf1]}"
  1230. PageUp "${terminfo[kpp]}"
  1231. PageDown "${terminfo[knp]}"
  1232. BackTab "${terminfo[kcbt]}"
  1233. )
  1234. # Guidelines for adding key bindings:
  1235. #
  1236. # - Do not add hardcoded escape sequences, to enable non standard key
  1237. # combinations such as Ctrl-Meta-Left-Cursor. They are not easily portable.
  1238. #
  1239. # - Adding Ctrl characters, such as '^b' is okay; note that '^b' and '^B' are
  1240. # the same key.
  1241. #
  1242. # - All keys from the $key[] mapping are obviously okay.
  1243. #
  1244. # - Most terminals send "ESC x" when Meta-x is pressed. Thus, sequences like
  1245. # '\ex' are allowed in here as well.
  1246. bind2maps emacs -- Home beginning-of-somewhere
  1247. bind2maps viins vicmd -- Home vi-beginning-of-line
  1248. bind2maps emacs -- End end-of-somewhere
  1249. bind2maps viins vicmd -- End vi-end-of-line
  1250. bind2maps emacs viins -- Insert overwrite-mode
  1251. bind2maps vicmd -- Insert vi-insert
  1252. bind2maps emacs -- Delete delete-char
  1253. bind2maps viins vicmd -- Delete vi-delete-char
  1254. bind2maps emacs viins vicmd -- Up up-line-or-search
  1255. bind2maps emacs viins vicmd -- Down down-line-or-search
  1256. bind2maps emacs -- Left backward-char
  1257. bind2maps viins vicmd -- Left vi-backward-char
  1258. bind2maps emacs -- Right forward-char
  1259. bind2maps viins vicmd -- Right vi-forward-char
  1260. #k# Perform abbreviation expansion
  1261. bind2maps emacs viins -- -s '^x.' zleiab
  1262. #k# Display list of abbreviations that would expand
  1263. bind2maps emacs viins -- -s '^xb' help-show-abk
  1264. #k# mkdir -p <dir> from string under cursor or marked area
  1265. bind2maps emacs viins -- -s '^xM' inplaceMkDirs
  1266. #k# display help for keybindings and ZLE
  1267. bind2maps emacs viins -- -s '^xz' help-zle
  1268. #k# Insert files and test globbing
  1269. bind2maps emacs viins -- -s "^xf" insert-files
  1270. #k# Edit the current line in \kbd{\$EDITOR}
  1271. bind2maps emacs viins -- -s '\ee' edit-command-line
  1272. #k# search history backward for entry beginning with typed text
  1273. bind2maps emacs viins -- -s '^xp' history-beginning-search-backward-end
  1274. #k# search history forward for entry beginning with typed text
  1275. bind2maps emacs viins -- -s '^xP' history-beginning-search-forward-end
  1276. #k# search history backward for entry beginning with typed text
  1277. bind2maps emacs viins -- PageUp history-beginning-search-backward-end
  1278. #k# search history forward for entry beginning with typed text
  1279. bind2maps emacs viins -- PageDown history-beginning-search-forward-end
  1280. bind2maps emacs viins -- -s "^x^h" commit-to-history
  1281. #k# Kill left-side word or everything up to next slash
  1282. bind2maps emacs viins -- -s '\ev' slash-backward-kill-word
  1283. #k# Kill left-side word or everything up to next slash
  1284. bind2maps emacs viins -- -s '\e^h' slash-backward-kill-word
  1285. #k# Kill left-side word or everything up to next slash
  1286. bind2maps emacs viins -- -s '\e^?' slash-backward-kill-word
  1287. # Do history expansion on space:
  1288. bind2maps emacs viins -- -s ' ' magic-space
  1289. #k# Trigger menu-complete
  1290. bind2maps emacs viins -- -s '\ei' menu-complete # menu completion via esc-i
  1291. #k# Insert a timestamp on the command line (yyyy-mm-dd)
  1292. bind2maps emacs viins -- -s '^xd' insert-datestamp
  1293. #k# Insert last typed word
  1294. bind2maps emacs viins -- -s "\em" insert-last-typed-word
  1295. #k# A smart shortcut for \kbd{fg<enter>}
  1296. bind2maps emacs viins -- -s '^z' grml-zsh-fg
  1297. #k# prepend the current command with "sudo"
  1298. bind2maps emacs viins -- -s "^os" sudo-command-line
  1299. #k# jump to after first word (for adding options)
  1300. bind2maps emacs viins -- -s '^x1' jump_after_first_word
  1301. #k# complete word from history with menu
  1302. bind2maps emacs viins -- -s "^x^x" hist-complete
  1303. # insert unicode character
  1304. # usage example: 'ctrl-x i' 00A7 'ctrl-x i' will give you an §
  1305. # See for example http://unicode.org/charts/ for unicode characters code
  1306. #k# Insert Unicode character
  1307. bind2maps emacs viins -- -s '^xi' insert-unicode-char
  1308. # use the new *-pattern-* widgets for incremental history search
  1309. if zrcgotwidget history-incremental-pattern-search-backward; then
  1310. for seq wid in '^r' history-incremental-pattern-search-backward \
  1311. '^s' history-incremental-pattern-search-forward
  1312. do
  1313. bind2maps emacs viins vicmd -- -s $seq $wid
  1314. done
  1315. builtin unset -v seq wid
  1316. fi
  1317. if zrcgotkeymap menuselect; then
  1318. #m# k Shift-tab Perform backwards menu completion
  1319. bind2maps menuselect -- BackTab reverse-menu-complete
  1320. #k# menu selection: pick item but stay in the menu
  1321. bind2maps menuselect -- -s '\e^M' accept-and-menu-complete
  1322. # also use + and INSERT since it's easier to press repeatedly
  1323. bind2maps menuselect -- -s '+' accept-and-menu-complete
  1324. bind2maps menuselect -- Insert accept-and-menu-complete
  1325. # accept a completion and try to complete again by using menu
  1326. # completion; very useful with completing directories
  1327. # by using 'undo' one's got a simple file browser
  1328. bind2maps menuselect -- -s '^o' accept-and-infer-next-history
  1329. fi
  1330. # Finally, here are still a few hardcoded escape sequences; Special sequences
  1331. # like Ctrl-<Cursor-key> etc do suck a fair bit, because they are not
  1332. # standardised and most of the time are not available in a terminals terminfo
  1333. # entry.
  1334. #
  1335. # While we do not encourage adding bindings like these, we will keep these for
  1336. # backward compatibility.
  1337. ## use Ctrl-left-arrow and Ctrl-right-arrow for jumping to word-beginnings on
  1338. ## the command line.
  1339. # URxvt sequences:
  1340. bind2maps emacs viins vicmd -- -s '\eOc' forward-word
  1341. bind2maps emacs viins vicmd -- -s '\eOd' backward-word
  1342. # These are for xterm:
  1343. bind2maps emacs viins vicmd -- -s '\e[1;5C' forward-word
  1344. bind2maps emacs viins vicmd -- -s '\e[1;5D' backward-word
  1345. ## the same for alt-left-arrow and alt-right-arrow
  1346. # URxvt again:
  1347. bind2maps emacs viins vicmd -- -s '\e\e[C' forward-word
  1348. bind2maps emacs viins vicmd -- -s '\e\e[D' backward-word
  1349. # Xterm again:
  1350. bind2maps emacs viins vicmd -- -s '^[[1;3C' forward-word
  1351. bind2maps emacs viins vicmd -- -s '^[[1;3D' backward-word
  1352. # Also try ESC Left/Right:
  1353. bind2maps emacs viins vicmd -- -s '\e'${key[Right]} forward-word
  1354. bind2maps emacs viins vicmd -- -s '\e'${key[Left]} backward-word
  1355. # autoloading
  1356. zrcautoload zmv
  1357. zrcautoload zed
  1358. # we don't want to quote/espace URLs on our own...
  1359. # if autoload -U url-quote-magic ; then
  1360. # zle -N self-insert url-quote-magic
  1361. # zstyle ':url-quote-magic:*' url-metas '*?[]^()~#{}='
  1362. # else
  1363. # print 'Notice: no url-quote-magic available :('
  1364. # fi
  1365. alias url-quote='autoload -U url-quote-magic ; zle -N self-insert url-quote-magic'
  1366. #m# k ESC-h Call \kbd{run-help} for the 1st word on the command line
  1367. alias run-help >&/dev/null && unalias run-help
  1368. for rh in run-help{,-git,-ip,-openssl,-p4,-sudo,-svk,-svn}; do
  1369. zrcautoload $rh
  1370. done; unset rh
  1371. # command not found handling
  1372. (( ${COMMAND_NOT_FOUND} == 1 )) &&
  1373. function command_not_found_handler () {
  1374. emulate -L zsh
  1375. if [[ -x ${GRML_ZSH_CNF_HANDLER} ]] ; then
  1376. ${GRML_ZSH_CNF_HANDLER} $1
  1377. fi
  1378. return 1
  1379. }
  1380. # history
  1381. #v#
  1382. HISTFILE=${HISTFILE:-${ZDOTDIR:-${HOME}}/.zsh_history}
  1383. isgrmlcd && HISTSIZE=500 || HISTSIZE=5000
  1384. isgrmlcd && SAVEHIST=1000 || SAVEHIST=10000 # useful for setopt append_history
  1385. # dirstack handling
  1386. DIRSTACKSIZE=${DIRSTACKSIZE:-20}
  1387. DIRSTACKFILE=${DIRSTACKFILE:-${ZDOTDIR:-${HOME}}/.zdirs}
  1388. if zstyle -T ':grml:chpwd:dirstack' enable; then
  1389. typeset -gaU GRML_PERSISTENT_DIRSTACK
  1390. function grml_dirstack_filter () {
  1391. local -a exclude
  1392. local filter entry
  1393. if zstyle -s ':grml:chpwd:dirstack' filter filter; then
  1394. $filter $1 && return 0
  1395. fi
  1396. if zstyle -a ':grml:chpwd:dirstack' exclude exclude; then
  1397. for entry in "${exclude[@]}"; do
  1398. [[ $1 == ${~entry} ]] && return 0
  1399. done
  1400. fi
  1401. return 1
  1402. }
  1403. function chpwd () {
  1404. (( ZSH_SUBSHELL )) && return
  1405. (( $DIRSTACKSIZE <= 0 )) && return
  1406. [[ -z $DIRSTACKFILE ]] && return
  1407. grml_dirstack_filter $PWD && return
  1408. GRML_PERSISTENT_DIRSTACK=(
  1409. $PWD "${(@)GRML_PERSISTENT_DIRSTACK[1,$DIRSTACKSIZE]}"
  1410. )
  1411. builtin print -l ${GRML_PERSISTENT_DIRSTACK} >! ${DIRSTACKFILE}
  1412. }
  1413. if [[ -f ${DIRSTACKFILE} ]]; then
  1414. # Enabling NULL_GLOB via (N) weeds out any non-existing
  1415. # directories from the saved dir-stack file.
  1416. dirstack=( ${(f)"$(< $DIRSTACKFILE)"}(N) )
  1417. # "cd -" won't work after login by just setting $OLDPWD, so
  1418. [[ -d $dirstack[1] ]] && cd -q $dirstack[1] && cd -q $OLDPWD
  1419. fi
  1420. if zstyle -t ':grml:chpwd:dirstack' filter-on-load; then
  1421. for i in "${dirstack[@]}"; do
  1422. if ! grml_dirstack_filter "$i"; then
  1423. GRML_PERSISTENT_DIRSTACK=(
  1424. "${GRML_PERSISTENT_DIRSTACK[@]}"
  1425. $i
  1426. )
  1427. fi
  1428. done
  1429. else
  1430. GRML_PERSISTENT_DIRSTACK=( "${dirstack[@]}" )
  1431. fi
  1432. fi
  1433. # directory based profiles
  1434. if is433 ; then
  1435. # chpwd_profiles(): Directory Profiles, Quickstart:
  1436. #
  1437. # In .zshrc.local:
  1438. #
  1439. # zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)' profile grml
  1440. # zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian
  1441. # chpwd_profiles
  1442. #
  1443. # For details see the `grmlzshrc.5' manual page.
  1444. function chpwd_profiles () {
  1445. local profile context
  1446. local -i reexecute
  1447. context=":chpwd:profiles:$PWD"
  1448. zstyle -s "$context" profile profile || profile='default'
  1449. zstyle -T "$context" re-execute && reexecute=1 || reexecute=0
  1450. if (( ${+parameters[CHPWD_PROFILE]} == 0 )); then
  1451. typeset -g CHPWD_PROFILE
  1452. local CHPWD_PROFILES_INIT=1
  1453. (( ${+functions[chpwd_profiles_init]} )) && chpwd_profiles_init
  1454. elif [[ $profile != $CHPWD_PROFILE ]]; then
  1455. (( ${+functions[chpwd_leave_profile_$CHPWD_PROFILE]} )) \
  1456. && chpwd_leave_profile_${CHPWD_PROFILE}
  1457. fi
  1458. if (( reexecute )) || [[ $profile != $CHPWD_PROFILE ]]; then
  1459. (( ${+functions[chpwd_profile_$profile]} )) && chpwd_profile_${profile}
  1460. fi
  1461. CHPWD_PROFILE="${profile}"
  1462. return 0
  1463. }
  1464. chpwd_functions=( ${chpwd_functions} chpwd_profiles )
  1465. fi # is433
  1466. # Prompt setup for grml:
  1467. # set colors for use in prompts (modern zshs allow for the use of %F{red}foo%f
  1468. # in prompts to get a red "foo" embedded, but it's good to keep these for
  1469. # backwards compatibility).
  1470. if is437; then
  1471. BLUE="%F{blue}"
  1472. RED="%F{red}"
  1473. GREEN="%F{green}"
  1474. CYAN="%F{cyan}"
  1475. MAGENTA="%F{magenta}"
  1476. YELLOW="%F{yellow}"
  1477. WHITE="%F{white}"
  1478. NO_COLOR="%f"
  1479. elif zrcautoload colors && colors 2>/dev/null ; then
  1480. BLUE="%{${fg[blue]}%}"
  1481. RED="%{${fg_bold[red]}%}"
  1482. GREEN="%{${fg[green]}%}"
  1483. CYAN="%{${fg[cyan]}%}"
  1484. MAGENTA="%{${fg[magenta]}%}"
  1485. YELLOW="%{${fg[yellow]}%}"
  1486. WHITE="%{${fg[white]}%}"
  1487. NO_COLOR="%{${reset_color}%}"
  1488. else
  1489. BLUE=$'%{\e[1;34m%}'
  1490. RED=$'%{\e[1;31m%}'
  1491. GREEN=$'%{\e[1;32m%}'
  1492. CYAN=$'%{\e[1;36m%}'
  1493. WHITE=$'%{\e[1;37m%}'
  1494. MAGENTA=$'%{\e[1;35m%}'
  1495. YELLOW=$'%{\e[1;33m%}'
  1496. NO_COLOR=$'%{\e[0m%}'
  1497. fi
  1498. # First, the easy ones: PS2..4:
  1499. # secondary prompt, printed when the shell needs more information to complete a
  1500. # command.
  1501. PS2='\`%_> '
  1502. # selection prompt used within a select loop.
  1503. PS3='?# '
  1504. # the execution trace prompt (setopt xtrace). default: '+%N:%i>'
  1505. PS4='+%N:%i:%_> '
  1506. # Some additional features to use with our prompt:
  1507. #
  1508. # - battery status
  1509. # - debian_chroot
  1510. # - vcs_info setup and version specific fixes
  1511. # display battery status on right side of prompt using 'GRML_DISPLAY_BATTERY=1' in .zshrc.pre
  1512. function battery () {
  1513. if [[ $GRML_DISPLAY_BATTERY -gt 0 ]] ; then
  1514. if islinux ; then
  1515. batterylinux
  1516. elif isopenbsd ; then
  1517. batteryopenbsd
  1518. elif isfreebsd ; then
  1519. batteryfreebsd
  1520. elif isdarwin ; then
  1521. batterydarwin
  1522. else
  1523. #not yet supported
  1524. GRML_DISPLAY_BATTERY=0
  1525. fi
  1526. fi
  1527. }
  1528. function batterylinux () {
  1529. GRML_BATTERY_LEVEL=''
  1530. local batteries bat capacity
  1531. batteries=( /sys/class/power_supply/BAT*(N) )
  1532. if (( $#batteries > 0 )) ; then
  1533. for bat in $batteries ; do
  1534. if [[ -e $bat/capacity ]]; then
  1535. capacity=$(< $bat/capacity)
  1536. else
  1537. typeset -F energy_full=$(< $bat/energy_full)
  1538. typeset -F energy_now=$(< $bat/energy_now)
  1539. typeset -i capacity=$(( 100 * $energy_now / $energy_full))
  1540. fi
  1541. case $(< $bat/status) in
  1542. Charging)
  1543. GRML_BATTERY_LEVEL+=" ^"
  1544. ;;
  1545. Discharging)
  1546. if (( capacity < 20 )) ; then
  1547. GRML_BATTERY_LEVEL+=" !v"
  1548. else
  1549. GRML_BATTERY_LEVEL+=" v"
  1550. fi
  1551. ;;
  1552. *) # Full, Unknown
  1553. GRML_BATTERY_LEVEL+=" ="
  1554. ;;
  1555. esac
  1556. GRML_BATTERY_LEVEL+="${capacity}%%"
  1557. done
  1558. fi
  1559. }
  1560. function batteryopenbsd () {
  1561. GRML_BATTERY_LEVEL=''
  1562. local bat batfull batwarn batnow num
  1563. for num in 0 1 ; do
  1564. bat=$(sysctl -n hw.sensors.acpibat${num} 2>/dev/null)
  1565. if [[ -n $bat ]]; then
  1566. batfull=${"$(sysctl -n hw.sensors.acpibat${num}.amphour0)"%% *}
  1567. batwarn=${"$(sysctl -n hw.sensors.acpibat${num}.amphour1)"%% *}
  1568. batnow=${"$(sysctl -n hw.sensors.acpibat${num}.amphour3)"%% *}
  1569. case "$(sysctl -n hw.sensors.acpibat${num}.raw0)" in
  1570. *" discharging"*)
  1571. if (( batnow < batwarn )) ; then
  1572. GRML_BATTERY_LEVEL+=" !v"
  1573. else
  1574. GRML_BATTERY_LEVEL+=" v"
  1575. fi
  1576. ;;
  1577. *" charging"*)
  1578. GRML_BATTERY_LEVEL+=" ^"
  1579. ;;
  1580. *)
  1581. GRML_BATTERY_LEVEL+=" ="
  1582. ;;
  1583. esac
  1584. GRML_BATTERY_LEVEL+="${$(( 100 * batnow / batfull ))%%.*}%%"
  1585. fi
  1586. done
  1587. }
  1588. function batteryfreebsd () {
  1589. GRML_BATTERY_LEVEL=''
  1590. local num
  1591. local -A table
  1592. for num in 0 1 ; do
  1593. table=( ${=${${${${${(M)${(f)"$(acpiconf -i $num 2>&1)"}:#(State|Remaining capacity):*}%%( ##|%)}//:[ $'\t']##/@}// /-}//@/ }} )
  1594. if [[ -n $table ]] && [[ $table[State] != "not-present" ]] ; then
  1595. case $table[State] in
  1596. *discharging*)
  1597. if (( $table[Remaining-capacity] < 20 )) ; then
  1598. GRML_BATTERY_LEVEL+=" !v"
  1599. else
  1600. GRML_BATTERY_LEVEL+=" v"
  1601. fi
  1602. ;;
  1603. *charging*)
  1604. GRML_BATTERY_LEVEL+=" ^"
  1605. ;;
  1606. *)
  1607. GRML_BATTERY_LEVEL+=" ="
  1608. ;;
  1609. esac
  1610. GRML_BATTERY_LEVEL+="$table[Remaining-capacity]%%"
  1611. fi
  1612. done
  1613. }
  1614. function batterydarwin () {
  1615. GRML_BATTERY_LEVEL=''
  1616. local -a table
  1617. table=( ${$(pmset -g ps)[(w)7,8]%%(\%|);} )
  1618. if [[ -n $table[2] ]] ; then
  1619. case $table[2] in
  1620. charging)
  1621. GRML_BATTERY_LEVEL+=" ^"
  1622. ;;
  1623. discharging)
  1624. if (( $table[1] < 20 )) ; then
  1625. GRML_BATTERY_LEVEL+=" !v"
  1626. else
  1627. GRML_BATTERY_LEVEL+=" v"
  1628. fi
  1629. ;;
  1630. *)
  1631. GRML_BATTERY_LEVEL+=" ="
  1632. ;;
  1633. esac
  1634. GRML_BATTERY_LEVEL+="$table[1]%%"
  1635. fi
  1636. }
  1637. # set variable debian_chroot if running in a chroot with /etc/debian_chroot
  1638. if [[ -z "$debian_chroot" ]] && [[ -r /etc/debian_chroot ]] ; then
  1639. debian_chroot=$(</etc/debian_chroot)
  1640. fi
  1641. # gather version control information for inclusion in a prompt
  1642. if zrcautoload vcs_info; then
  1643. # `vcs_info' in zsh versions 4.3.10 and below have a broken `_realpath'
  1644. # function, which can cause a lot of trouble with our directory-based
  1645. # profiles. So:
  1646. if [[ ${ZSH_VERSION} == 4.3.<-10> ]] ; then
  1647. function VCS_INFO_realpath () {
  1648. setopt localoptions NO_shwordsplit chaselinks
  1649. ( builtin cd -q $1 2> /dev/null && pwd; )
  1650. }
  1651. fi
  1652. zstyle ':vcs_info:*' max-exports 2
  1653. if [[ -o restricted ]]; then
  1654. zstyle ':vcs_info:*' enable NONE
  1655. fi
  1656. fi
  1657. typeset -A grml_vcs_coloured_formats
  1658. typeset -A grml_vcs_plain_formats
  1659. grml_vcs_plain_formats=(
  1660. format "(%s%)-[%b] " "zsh: %r"
  1661. actionformat "(%s%)-[%b|%a] " "zsh: %r"
  1662. rev-branchformat "%b:%r"
  1663. )
  1664. grml_vcs_coloured_formats=(
  1665. format "${MAGENTA}(${NO_COLOR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${MAGENTA}]${NO_COLOR} "
  1666. actionformat "${MAGENTA}(${NO_COLOR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${YELLOW}|${RED}%a${MAGENTA}]${NO_COLOR} "
  1667. rev-branchformat "%b${RED}:${YELLOW}%r"
  1668. )
  1669. typeset GRML_VCS_COLOUR_MODE=xxx
  1670. function grml_vcs_info_toggle_colour () {
  1671. emulate -L zsh
  1672. if [[ $GRML_VCS_COLOUR_MODE == plain ]]; then
  1673. grml_vcs_info_set_formats coloured
  1674. else
  1675. grml_vcs_info_set_formats plain
  1676. fi
  1677. return 0
  1678. }
  1679. function grml_vcs_info_set_formats () {
  1680. emulate -L zsh
  1681. #setopt localoptions xtrace
  1682. local mode=$1 AF F BF
  1683. if [[ $mode == coloured ]]; then
  1684. AF=${grml_vcs_coloured_formats[actionformat]}
  1685. F=${grml_vcs_coloured_formats[format]}
  1686. BF=${grml_vcs_coloured_formats[rev-branchformat]}
  1687. GRML_VCS_COLOUR_MODE=coloured
  1688. else
  1689. AF=${grml_vcs_plain_formats[actionformat]}
  1690. F=${grml_vcs_plain_formats[format]}
  1691. BF=${grml_vcs_plain_formats[rev-branchformat]}
  1692. GRML_VCS_COLOUR_MODE=plain
  1693. fi
  1694. zstyle ':vcs_info:*' actionformats "$AF" "zsh: %r"
  1695. zstyle ':vcs_info:*' formats "$F" "zsh: %r"
  1696. zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat "$BF"
  1697. return 0
  1698. }
  1699. # Change vcs_info formats for the grml prompt. The 2nd format sets up
  1700. # $vcs_info_msg_1_ to contain "zsh: repo-name" used to set our screen title.
  1701. if [[ "$TERM" == dumb ]] ; then
  1702. grml_vcs_info_set_formats plain
  1703. else
  1704. grml_vcs_info_set_formats coloured
  1705. fi
  1706. # Now for the fun part: The grml prompt themes in `promptsys' mode of operation
  1707. # This actually defines three prompts:
  1708. #
  1709. # - grml
  1710. # - grml-large
  1711. # - grml-chroot
  1712. #
  1713. # They all share the same code and only differ with respect to which items they
  1714. # contain. The main source of documentation is the `prompt_grml_help' function
  1715. # below, which gets called when the user does this: prompt -h grml
  1716. function prompt_grml_help () {
  1717. <<__EOF0__
  1718. prompt grml
  1719. This is the prompt as used by the grml-live system <http://grml.org>. It is
  1720. a rather simple one-line prompt, that by default looks something like this:
  1721. <user>@<host> <current-working-directory>[ <vcs_info-data>]%
  1722. The prompt itself integrates with zsh's prompt themes system (as you are
  1723. witnessing right now) and is configurable to a certain degree. In
  1724. particular, these aspects are customisable:
  1725. - The items used in the prompt (e.g. you can remove \`user' from
  1726. the list of activated items, which will cause the user name to
  1727. be omitted from the prompt string).
  1728. - The attributes used with the items are customisable via strings
  1729. used before and after the actual item.
  1730. The available items are: at, battery, change-root, date, grml-chroot,
  1731. history, host, jobs, newline, path, percent, rc, rc-always, sad-smiley,
  1732. shell-level, time, user, vcs
  1733. The actual configuration is done via zsh's \`zstyle' mechanism. The
  1734. context, that is used while looking up styles is:
  1735. ':prompt:grml:<left-or-right>:<subcontext>'
  1736. Here <left-or-right> is either \`left' or \`right', signifying whether the
  1737. style should affect the left or the right prompt. <subcontext> is either
  1738. \`setup' or 'items:<item>', where \`<item>' is one of the available items.
  1739. The styles:
  1740. - use-rprompt (boolean): If \`true' (the default), print a sad smiley
  1741. in $RPROMPT if the last command a returned non-successful error code.
  1742. (This in only valid if <left-or-right> is "right"; ignored otherwise)
  1743. - items (list): The list of items used in the prompt. If \`vcs' is
  1744. present in the list, the theme's code invokes \`vcs_info'
  1745. accordingly. Default (left): rc change-root user at host path vcs
  1746. percent; Default (right): sad-smiley
  1747. - strip-sensitive-characters (boolean): If the \`prompt_subst' option
  1748. is active in zsh, the shell performs lots of expansions on prompt
  1749. variable strings, including command substitution. So if you don't
  1750. control where some of your prompt strings is coming from, this is
  1751. an exploitable weakness. Grml's zsh setup does not set this option
  1752. and it is off in the shell in zsh-mode by default. If it *is* turned
  1753. on however, this style becomes active, and there are two flavours of
  1754. it: On per default is a global variant in the '*:setup' context. This
  1755. strips characters after the whole prompt string was constructed. There
  1756. is a second variant in the '*:items:<item>', that is off by default.
  1757. It allows fine grained control over which items' data is stripped.
  1758. The characters that are stripped are: \$ and \`.
  1759. Available styles in 'items:<item>' are: pre, post. These are strings that
  1760. are inserted before (pre) and after (post) the item in question. Thus, the
  1761. following would cause the user name to be printed in red instead of the
  1762. default blue:
  1763. zstyle ':prompt:grml:*:items:user' pre '%F{red}'
  1764. Note, that the \`post' style may remain at its default value, because its
  1765. default value is '%f', which turns the foreground text attribute off (which
  1766. is exactly, what is still required with the new \`pre' value).
  1767. __EOF0__
  1768. }
  1769. function prompt_grml-chroot_help () {
  1770. <<__EOF0__
  1771. prompt grml-chroot
  1772. This is a variation of the grml prompt, see: prompt -h grml
  1773. The main difference is the default value of the \`items' style. The rest
  1774. behaves exactly the same. Here are the defaults for \`grml-chroot':
  1775. - left: grml-chroot user at host path percent
  1776. - right: (empty list)
  1777. __EOF0__
  1778. }
  1779. function prompt_grml-large_help () {
  1780. <<__EOF0__
  1781. prompt grml-large
  1782. This is a variation of the grml prompt, see: prompt -h grml
  1783. The main difference is the default value of the \`items' style. In
  1784. particular, this theme uses _two_ lines instead of one with the plain
  1785. \`grml' theme. The rest behaves exactly the same. Here are the defaults
  1786. for \`grml-large':
  1787. - left: rc jobs history shell-level change-root time date newline user
  1788. at host path vcs percent
  1789. - right: sad-smiley
  1790. __EOF0__
  1791. }
  1792. function grml_prompt_setup () {
  1793. emulate -L zsh
  1794. autoload -Uz vcs_info
  1795. # The following autoload is disabled for now, since this setup includes a
  1796. # static version of the ‘add-zsh-hook’ function above. It needs to be
  1797. # re-enabled as soon as that static definition is removed again.
  1798. #autoload -Uz add-zsh-hook
  1799. add-zsh-hook precmd prompt_$1_precmd
  1800. }
  1801. function prompt_grml_setup () {
  1802. grml_prompt_setup grml
  1803. }
  1804. function prompt_grml-chroot_setup () {
  1805. grml_prompt_setup grml-chroot
  1806. }
  1807. function prompt_grml-large_setup () {
  1808. grml_prompt_setup grml-large
  1809. }
  1810. # These maps define default tokens and pre-/post-decoration for items to be
  1811. # used within the themes. All defaults may be customised in a context sensitive
  1812. # matter by using zsh's `zstyle' mechanism.
  1813. typeset -gA grml_prompt_pre_default \
  1814. grml_prompt_post_default \
  1815. grml_prompt_token_default \
  1816. grml_prompt_token_function
  1817. grml_prompt_pre_default=(
  1818. at ''
  1819. battery ' '
  1820. change-root ''
  1821. date '%F{blue}'
  1822. grml-chroot '%F{red}'
  1823. history '%F{green}'
  1824. host ''
  1825. jobs '%F{cyan}'
  1826. newline ''
  1827. path '%B'
  1828. percent ''
  1829. rc '%B%F{red}'
  1830. rc-always ''
  1831. sad-smiley ''
  1832. shell-level '%F{red}'
  1833. time '%F{blue}'
  1834. user '%B%F{blue}'
  1835. vcs ''
  1836. )
  1837. grml_prompt_post_default=(
  1838. at ''
  1839. battery ''
  1840. change-root ''
  1841. date '%f'
  1842. grml-chroot '%f '
  1843. history '%f'
  1844. host ''
  1845. jobs '%f'
  1846. newline ''
  1847. path '%b'
  1848. percent ''
  1849. rc '%f%b'
  1850. rc-always ''
  1851. sad-smiley ''
  1852. shell-level '%f'
  1853. time '%f'
  1854. user '%f%b'
  1855. vcs ''
  1856. )
  1857. grml_prompt_token_default=(
  1858. at '@'
  1859. battery 'GRML_BATTERY_LEVEL'
  1860. change-root 'debian_chroot'
  1861. date '%D{%Y-%m-%d}'
  1862. grml-chroot 'GRML_CHROOT'
  1863. history '{history#%!} '
  1864. host '%m '
  1865. jobs '[%j running job(s)] '
  1866. newline $'\n'
  1867. path '%40<..<%~%<< '
  1868. percent '%# '
  1869. rc '%(?..%? )'
  1870. rc-always '%?'
  1871. sad-smiley '%(?..:()'
  1872. shell-level '%(3L.+ .)'
  1873. time '%D{%H:%M:%S} '
  1874. user '%n'
  1875. vcs '0'
  1876. )
  1877. function grml_theme_has_token () {
  1878. if (( ARGC != 1 )); then
  1879. printf 'usage: grml_theme_has_token <name>\n'
  1880. return 1
  1881. fi
  1882. (( ${+grml_prompt_token_default[$1]} ))
  1883. }
  1884. function GRML_theme_add_token_usage () {
  1885. <<__EOF0__
  1886. Usage: grml_theme_add_token <name> [-f|-i] <token/function> [<pre> <post>]
  1887. <name> is the name for the newly added token. If the \`-f' or \`-i' options
  1888. are used, <token/function> is the name of the function (see below for
  1889. details). Otherwise it is the literal token string to be used. <pre> and
  1890. <post> are optional.
  1891. Options:
  1892. -f <function> Use a function named \`<function>' each time the token
  1893. is to be expanded.
  1894. -i <function> Use a function named \`<function>' to initialise the
  1895. value of the token _once_ at runtime.
  1896. The functions are called with one argument: the token's new name. The
  1897. return value is expected in the \$REPLY parameter. The use of these
  1898. options is mutually exclusive.
  1899. There is a utility function \`grml_theme_has_token', which you can use
  1900. to test if a token exists before trying to add it. This can be a guard
  1901. for situations in which a \`grml_theme_add_token' call may happen more
  1902. than once.
  1903. Example:
  1904. To add a new token \`day' that expands to the current weekday in the
  1905. current locale in green foreground colour, use this:
  1906. grml_theme_add_token day '%D{%A}' '%F{green}' '%f'
  1907. Another example would be support for \$VIRTUAL_ENV:
  1908. function virtual_env_prompt () {
  1909. REPLY=\${VIRTUAL_ENV+\${VIRTUAL_ENV:t} }
  1910. }
  1911. grml_theme_add_token virtual-env -f virtual_env_prompt
  1912. After that, you will be able to use a changed \`items' style to
  1913. assemble your prompt.
  1914. __EOF0__
  1915. }
  1916. function grml_theme_add_token () {
  1917. emulate -L zsh
  1918. local name token pre post
  1919. local -i init funcall
  1920. if (( ARGC == 0 )); then
  1921. GRML_theme_add_token_usage
  1922. return 0
  1923. fi
  1924. init=0
  1925. funcall=0
  1926. pre=''
  1927. post=''
  1928. name=$1
  1929. shift
  1930. if [[ $1 == '-f' ]]; then
  1931. funcall=1
  1932. shift
  1933. elif [[ $1 == '-i' ]]; then
  1934. init=1
  1935. shift
  1936. fi
  1937. if (( ARGC == 0 )); then
  1938. printf '
  1939. grml_theme_add_token: No token-string/function-name provided!\n\n'
  1940. GRML_theme_add_token_usage
  1941. return 1
  1942. fi
  1943. token=$1
  1944. shift
  1945. if (( ARGC != 0 && ARGC != 2 )); then
  1946. printf '
  1947. grml_theme_add_token: <pre> and <post> need to by specified _both_!\n\n'
  1948. GRML_theme_add_token_usage
  1949. return 1
  1950. fi
  1951. if (( ARGC )); then
  1952. pre=$1
  1953. post=$2
  1954. shift 2
  1955. fi
  1956. if grml_theme_has_token $name; then
  1957. printf '
  1958. grml_theme_add_token: Token `%s'\'' exists! Giving up!\n\n' $name
  1959. GRML_theme_add_token_usage
  1960. return 2
  1961. fi
  1962. if (( init )); then
  1963. $token $name
  1964. token=$REPLY
  1965. fi
  1966. grml_prompt_pre_default[$name]=$pre
  1967. grml_prompt_post_default[$name]=$post
  1968. if (( funcall )); then
  1969. grml_prompt_token_function[$name]=$token
  1970. grml_prompt_token_default[$name]=23
  1971. else
  1972. grml_prompt_token_default[$name]=$token
  1973. fi
  1974. }
  1975. function grml_wrap_reply () {
  1976. emulate -L zsh
  1977. local target="$1"
  1978. local new="$2"
  1979. local left="$3"
  1980. local right="$4"
  1981. if (( ${+parameters[$new]} )); then
  1982. REPLY="${left}${(P)new}${right}"
  1983. else
  1984. REPLY=''
  1985. fi
  1986. }
  1987. function grml_prompt_addto () {
  1988. emulate -L zsh
  1989. local target="$1"
  1990. local lr it apre apost new v REPLY
  1991. local -a items
  1992. shift
  1993. [[ $target == PS1 ]] && lr=left || lr=right
  1994. zstyle -a ":prompt:${grmltheme}:${lr}:setup" items items || items=( "$@" )
  1995. typeset -g "${target}="
  1996. for it in "${items[@]}"; do
  1997. zstyle -s ":prompt:${grmltheme}:${lr}:items:$it" pre apre \
  1998. || apre=${grml_prompt_pre_default[$it]}
  1999. zstyle -s ":prompt:${grmltheme}:${lr}:items:$it" post apost \
  2000. || apost=${grml_prompt_post_default[$it]}
  2001. zstyle -s ":prompt:${grmltheme}:${lr}:items:$it" token new \
  2002. || new=${grml_prompt_token_default[$it]}
  2003. if (( ${+grml_prompt_token_function[$it]} )); then
  2004. ${grml_prompt_token_function[$it]} $it
  2005. else
  2006. case $it in
  2007. battery)
  2008. grml_wrap_reply $target $new '' ''
  2009. ;;
  2010. change-root)
  2011. grml_wrap_reply $target $new '(' ')'
  2012. ;;
  2013. grml-chroot)
  2014. if [[ -n ${(P)new} ]]; then
  2015. REPLY="$CHROOT"
  2016. else
  2017. REPLY=''
  2018. fi
  2019. ;;
  2020. vcs)
  2021. v="vcs_info_msg_${new}_"
  2022. if (( ! vcscalled )); then
  2023. vcs_info
  2024. vcscalled=1
  2025. fi
  2026. if (( ${+parameters[$v]} )) && [[ -n "${(P)v}" ]]; then
  2027. REPLY="${(P)v}"
  2028. else
  2029. REPLY=''
  2030. fi
  2031. ;;
  2032. *) REPLY="$new" ;;
  2033. esac
  2034. fi
  2035. # Strip volatile characters per item. This is off by default. See the
  2036. # global stripping code a few lines below for details.
  2037. if [[ -o prompt_subst ]] && zstyle -t ":prompt:${grmltheme}:${lr}:items:$it" \
  2038. strip-sensitive-characters
  2039. then
  2040. REPLY="${REPLY//[$\`]/}"
  2041. fi
  2042. typeset -g "${target}=${(P)target}${apre}${REPLY}${apost}"
  2043. done
  2044. # Per default, strip volatile characters (in the prompt_subst case)
  2045. # globally. If the option is off, the style has no effect. For more
  2046. # control, this can be turned off and stripping can be configured on a
  2047. # per-item basis (see above).
  2048. if [[ -o prompt_subst ]] && zstyle -T ":prompt:${grmltheme}:${lr}:setup" \
  2049. strip-sensitive-characters
  2050. then
  2051. typeset -g "${target}=${${(P)target}//[$\`]/}"
  2052. fi
  2053. }
  2054. function prompt_grml_precmd () {
  2055. emulate -L zsh
  2056. local grmltheme=grml
  2057. local -a left_items right_items
  2058. left_items=(rc change-root user at host path vcs percent)
  2059. right_items=(sad-smiley)
  2060. prompt_grml_precmd_worker
  2061. }
  2062. function prompt_grml-chroot_precmd () {
  2063. emulate -L zsh
  2064. local grmltheme=grml-chroot
  2065. local -a left_items right_items
  2066. left_items=(grml-chroot user at host path percent)
  2067. right_items=()
  2068. prompt_grml_precmd_worker
  2069. }
  2070. function prompt_grml-large_precmd () {
  2071. emulate -L zsh
  2072. local grmltheme=grml-large
  2073. local -a left_items right_items
  2074. left_items=(rc jobs history shell-level change-root time date newline
  2075. user at host path vcs percent)
  2076. right_items=(sad-smiley)
  2077. prompt_grml_precmd_worker
  2078. }
  2079. function prompt_grml_precmd_worker () {
  2080. emulate -L zsh
  2081. local -i vcscalled=0
  2082. grml_prompt_addto PS1 "${left_items[@]}"
  2083. if zstyle -T ":prompt:${grmltheme}:right:setup" use-rprompt; then
  2084. grml_prompt_addto RPS1 "${right_items[@]}"
  2085. fi
  2086. }
  2087. function grml_prompt_fallback () {
  2088. setopt prompt_subst
  2089. local p0 p1
  2090. p0="${RED}%(?..%? )${WHITE}${debian_chroot:+($debian_chroot)}"
  2091. p1="${BLUE}%n${NO_COLOR}@%m %40<...<%B%~%b%<< "'${vcs_info_msg_0_}'"%# "
  2092. if (( EUID == 0 )); then
  2093. PROMPT="${BLUE}${p0}${RED}${p1}"
  2094. else
  2095. PROMPT="${RED}${p0}${BLUE}${p1}"
  2096. fi
  2097. }
  2098. if zrcautoload promptinit && promptinit 2>/dev/null ; then
  2099. # Since we define the required functions in here and not in files in
  2100. # $fpath, we need to stick the theme's name into `$prompt_themes'
  2101. # ourselves, since promptinit does not pick them up otherwise.
  2102. prompt_themes+=( grml grml-chroot grml-large )
  2103. # Also, keep the array sorted...
  2104. prompt_themes=( "${(@on)prompt_themes}" )
  2105. else
  2106. print 'Notice: no promptinit available :('
  2107. grml_prompt_fallback
  2108. function precmd () { (( ${+functions[vcs_info]} )) && vcs_info; }
  2109. fi
  2110. if is437; then
  2111. # The prompt themes use modern features of zsh, that require at least
  2112. # version 4.3.7 of the shell. Use the fallback otherwise.
  2113. if [[ $GRML_DISPLAY_BATTERY -gt 0 ]]; then
  2114. zstyle ':prompt:grml:right:setup' items sad-smiley battery
  2115. add-zsh-hook precmd battery
  2116. fi
  2117. if [[ "$TERM" == dumb ]] ; then
  2118. zstyle ":prompt:grml(|-large|-chroot):*:items:grml-chroot" pre ''
  2119. zstyle ":prompt:grml(|-large|-chroot):*:items:grml-chroot" post ' '
  2120. for i in rc user path jobs history date time shell-level; do
  2121. zstyle ":prompt:grml(|-large|-chroot):*:items:$i" pre ''
  2122. zstyle ":prompt:grml(|-large|-chroot):*:items:$i" post ''
  2123. done
  2124. unset i
  2125. zstyle ':prompt:grml(|-large|-chroot):right:setup' use-rprompt false
  2126. elif (( EUID == 0 )); then
  2127. zstyle ':prompt:grml(|-large|-chroot):*:items:user' pre '%B%F{red}'
  2128. fi
  2129. # Finally enable one of the prompts.
  2130. if [[ -n $GRML_CHROOT ]]; then
  2131. prompt grml-chroot
  2132. elif [[ $GRMLPROMPT -gt 0 ]]; then
  2133. prompt grml-large
  2134. else
  2135. prompt grml
  2136. fi
  2137. else
  2138. grml_prompt_fallback
  2139. function precmd () { (( ${+functions[vcs_info]} )) && vcs_info; }
  2140. fi
  2141. # Terminal-title wizardry
  2142. function ESC_print () {
  2143. info_print $'\ek' $'\e\\' "$@"
  2144. }
  2145. function set_title () {
  2146. info_print $'\e]0;' $'\a' "$@"
  2147. }
  2148. function info_print () {
  2149. local esc_begin esc_end
  2150. esc_begin="$1"
  2151. esc_end="$2"
  2152. shift 2
  2153. printf '%s' ${esc_begin}
  2154. printf '%s' "$*"
  2155. printf '%s' "${esc_end}"
  2156. }
  2157. function grml_reset_screen_title () {
  2158. # adjust title of xterm
  2159. # see http://www.faqs.org/docs/Linux-mini/Xterm-Title.html
  2160. [[ ${NOTITLE:-} -gt 0 ]] && return 0
  2161. case $TERM in
  2162. (xterm*|rxvt*)
  2163. set_title ${(%):-"%n@%m: %~"}
  2164. ;;
  2165. esac
  2166. }
  2167. function grml_vcs_to_screen_title () {
  2168. if [[ $TERM == screen* ]] ; then
  2169. if [[ -n ${vcs_info_msg_1_} ]] ; then
  2170. ESC_print ${vcs_info_msg_1_}
  2171. else
  2172. ESC_print "zsh"
  2173. fi
  2174. fi
  2175. }
  2176. function grml_maintain_name () {
  2177. # set hostname if not running on host with name 'grml'
  2178. if [[ -n "$HOSTNAME" ]] && [[ "$HOSTNAME" != $(hostname) ]] ; then
  2179. NAME="@$HOSTNAME"
  2180. fi
  2181. }
  2182. function grml_cmd_to_screen_title () {
  2183. # get the name of the program currently running and hostname of local
  2184. # machine set screen window title if running in a screen
  2185. if [[ "$TERM" == screen* ]] ; then
  2186. local CMD="${1[(wr)^(*=*|sudo|ssh|-*)]}$NAME"
  2187. ESC_print ${CMD}
  2188. fi
  2189. }
  2190. function grml_control_xterm_title () {
  2191. case $TERM in
  2192. (xterm*|rxvt*)
  2193. set_title "${(%):-"%n@%m:"}" "$1"
  2194. ;;
  2195. esac
  2196. }
  2197. # The following autoload is disabled for now, since this setup includes a
  2198. # static version of the ‘add-zsh-hook’ function above. It needs to be
  2199. # re-enabled as soon as that static definition is removed again.
  2200. #zrcautoload add-zsh-hook || add-zsh-hook () { :; }
  2201. if [[ $NOPRECMD -eq 0 ]]; then
  2202. add-zsh-hook precmd grml_reset_screen_title
  2203. add-zsh-hook precmd grml_vcs_to_screen_title
  2204. add-zsh-hook preexec grml_maintain_name
  2205. add-zsh-hook preexec grml_cmd_to_screen_title
  2206. if [[ $NOTITLE -eq 0 ]]; then
  2207. add-zsh-hook preexec grml_control_xterm_title
  2208. fi
  2209. fi
  2210. # 'hash' some often used directories
  2211. #d# start
  2212. hash -d deb=/var/cache/apt/archives
  2213. hash -d doc=/usr/share/doc
  2214. hash -d linux=/lib/modules/$(command uname -r)/build/
  2215. hash -d log=/var/log
  2216. hash -d slog=/var/log/syslog
  2217. hash -d src=/usr/src
  2218. hash -d www=/var/www
  2219. #d# end
  2220. # some aliases
  2221. if check_com -c screen ; then
  2222. if [[ $UID -eq 0 ]] ; then
  2223. if [[ -r /etc/grml/screenrc ]]; then
  2224. alias screen='screen -c /etc/grml/screenrc'
  2225. fi
  2226. elif [[ ! -r $HOME/.screenrc ]] ; then
  2227. if [[ -r /etc/grml/screenrc_grml ]]; then
  2228. alias screen='screen -c /etc/grml/screenrc_grml'
  2229. else
  2230. if [[ -r /etc/grml/screenrc ]]; then
  2231. alias screen='screen -c /etc/grml/screenrc'
  2232. fi
  2233. fi
  2234. fi
  2235. fi
  2236. # do we have GNU ls with color-support?
  2237. if [[ "$TERM" != dumb ]]; then
  2238. #a1# List files with colors (\kbd{ls \ldots})
  2239. alias ls="command ls ${ls_options:+${ls_options[*]}}"
  2240. #a1# List all files, with colors (\kbd{ls -la \ldots})
  2241. alias la="command ls -la ${ls_options:+${ls_options[*]}}"
  2242. #a1# List files with long colored list, without dotfiles (\kbd{ls -l \ldots})
  2243. alias ll="command ls -l ${ls_options:+${ls_options[*]}}"
  2244. #a1# List files with long colored list, human readable sizes (\kbd{ls -hAl \ldots})
  2245. alias lh="command ls -hAl ${ls_options:+${ls_options[*]}}"
  2246. #a1# List files with long colored list, append qualifier to filenames (\kbd{ls -l \ldots})\\&\quad(\kbd{/} for directories, \kbd{@} for symlinks ...)
  2247. alias l="command ls -l ${ls_options:+${ls_options[*]}}"
  2248. else
  2249. alias la='command ls -la'
  2250. alias ll='command ls -l'
  2251. alias lh='command ls -hAl'
  2252. alias l='command ls -l'
  2253. fi
  2254. if [[ -r /proc/mdstat ]]; then
  2255. alias mdstat='cat /proc/mdstat'
  2256. fi
  2257. alias ...='cd ../../'
  2258. # generate alias named "$KERNELVERSION-reboot" so you can use boot with kexec:
  2259. if [[ -x /sbin/kexec ]] && [[ -r /proc/cmdline ]] ; then
  2260. alias "$(uname -r)-reboot"="kexec -l --initrd=/boot/initrd.img-"$(uname -r)" --command-line=\"$(cat /proc/cmdline)\" /boot/vmlinuz-"$(uname -r)""
  2261. fi
  2262. # see http://www.cl.cam.ac.uk/~mgk25/unicode.html#term for details
  2263. alias term2iso="echo 'Setting terminal to iso mode' ; print -n '\e%@'"
  2264. alias term2utf="echo 'Setting terminal to utf-8 mode'; print -n '\e%G'"
  2265. # make sure it is not assigned yet
  2266. [[ -n ${aliases[utf2iso]} ]] && unalias utf2iso
  2267. function utf2iso () {
  2268. if isutfenv ; then
  2269. local ENV
  2270. for ENV in $(env | command grep -i '.utf') ; do
  2271. eval export "$(echo $ENV | sed 's/UTF-8/iso885915/ ; s/utf8/iso885915/')"
  2272. done
  2273. fi
  2274. }
  2275. # make sure it is not assigned yet
  2276. [[ -n ${aliases[iso2utf]} ]] && unalias iso2utf
  2277. function iso2utf () {
  2278. if ! isutfenv ; then
  2279. local ENV
  2280. for ENV in $(env | command grep -i '\.iso') ; do
  2281. eval export "$(echo $ENV | sed 's/iso.*/UTF-8/ ; s/ISO.*/UTF-8/')"
  2282. done
  2283. fi
  2284. }
  2285. # especially for roadwarriors using GNU screen and ssh:
  2286. if ! check_com asc &>/dev/null ; then
  2287. function asc () { autossh -t "$@" 'screen -RdU' }
  2288. compdef asc=ssh
  2289. fi
  2290. #f1# Hints for the use of zsh on grml
  2291. function zsh-help () {
  2292. print "$bg[white]$fg[black]
  2293. zsh-help - hints for use of zsh on grml
  2294. =======================================$reset_color"
  2295. print '
  2296. Main configuration of zsh happens in /etc/zsh/zshrc.
  2297. That file is part of the package grml-etc-core, if you want to
  2298. use them on a non-grml-system just get the tar.gz from
  2299. http://deb.grml.org/ or (preferably) get it from the git repository:
  2300. http://git.grml.org/f/grml-etc-core/etc/zsh/zshrc
  2301. This version of grml'\''s zsh setup does not use skel/.zshrc anymore.
  2302. The file is still there, but it is empty for backwards compatibility.
  2303. For your own changes use these two files:
  2304. $HOME/.zshrc.pre
  2305. $HOME/.zshrc.local
  2306. The former is sourced very early in our zshrc, the latter is sourced
  2307. very lately.
  2308. System wide configuration without touching configuration files of grml
  2309. can take place in /etc/zsh/zshrc.local.
  2310. For information regarding zsh start at http://grml.org/zsh/
  2311. Take a look at grml'\''s zsh refcard:
  2312. % xpdf =(zcat /usr/share/doc/grml-docs/zsh/grml-zsh-refcard.pdf.gz)
  2313. Check out the main zsh refcard:
  2314. % '$BROWSER' http://www.bash2zsh.com/zsh_refcard/refcard.pdf
  2315. And of course visit the zsh-lovers:
  2316. % man zsh-lovers
  2317. You can adjust some options through environment variables when
  2318. invoking zsh without having to edit configuration files.
  2319. Basically meant for bash users who are not used to the power of
  2320. the zsh yet. :)
  2321. "NOCOR=1 zsh" => deactivate automatic correction
  2322. "NOMENU=1 zsh" => do not use auto menu completion
  2323. (note: use ctrl-d for completion instead!)
  2324. "NOPRECMD=1 zsh" => disable the precmd + preexec commands (set GNU screen title)
  2325. "NOTITLE=1 zsh" => disable setting the title of xterms without disabling
  2326. preexec() and precmd() completely
  2327. "GRML_DISPLAY_BATTERY=1 zsh"
  2328. => activate battery status on right side of prompt (WIP)
  2329. "COMMAND_NOT_FOUND=1 zsh"
  2330. => Enable a handler if an external command was not found
  2331. The command called in the handler can be altered by setting
  2332. the GRML_ZSH_CNF_HANDLER variable, the default is:
  2333. "/usr/share/command-not-found/command-not-found"
  2334. A value greater than 0 is enables a feature; a value equal to zero
  2335. disables it. If you like one or the other of these settings, you can
  2336. add them to ~/.zshrc.pre to ensure they are set when sourcing grml'\''s
  2337. zshrc.'
  2338. print "
  2339. $bg[white]$fg[black]
  2340. Please report wishes + bugs to the grml-team: http://grml.org/bugs/
  2341. Enjoy your grml system with the zsh!$reset_color"
  2342. }
  2343. # debian stuff
  2344. if [[ -r /etc/debian_version ]] ; then
  2345. if [[ -z "$GRML_NO_APT_ALIASES" ]]; then
  2346. #a3# Execute \kbd{apt-cache policy}
  2347. alias acp='apt-cache policy'
  2348. if check_com -c apt ; then
  2349. #a3# Execute \kbd{apt search}
  2350. alias acs='apt search'
  2351. #a3# Execute \kbd{apt show}
  2352. alias acsh='apt show'
  2353. #a3# Execute \kbd{apt dist-upgrade}
  2354. salias adg="apt dist-upgrade"
  2355. #a3# Execute \kbd{apt upgrade}
  2356. salias ag="apt upgrade"
  2357. #a3# Execute \kbd{apt install}
  2358. salias agi="apt install"
  2359. #a3# Execute \kbd{apt update}
  2360. salias au="apt update"
  2361. else
  2362. alias acs='apt-cache search'
  2363. alias acsh='apt-cache show'
  2364. salias adg="apt-get dist-upgrade"
  2365. salias ag="apt-get upgrade"
  2366. salias agi="apt-get install"
  2367. salias au="apt-get update"
  2368. fi
  2369. #a3# Execute \kbd{aptitude install}
  2370. salias ati="aptitude install"
  2371. #a3# Execute \kbd{aptitude update ; aptitude safe-upgrade}
  2372. salias -a up="aptitude update ; aptitude safe-upgrade"
  2373. #a3# Execute \kbd{dpkg-buildpackage}
  2374. alias dbp='dpkg-buildpackage'
  2375. #a3# Execute \kbd{grep-excuses}
  2376. alias ge='grep-excuses'
  2377. fi
  2378. # get a root shell as normal user in live-cd mode:
  2379. if isgrmlcd && [[ $UID -ne 0 ]] ; then
  2380. alias su="sudo su"
  2381. fi
  2382. fi
  2383. # use /var/log/syslog iff present, fallback to journalctl otherwise
  2384. if [ -e /var/log/syslog ] ; then
  2385. #a1# Take a look at the syslog: \kbd{\$PAGER /var/log/syslog || journalctl}
  2386. salias llog="$PAGER /var/log/syslog" # take a look at the syslog
  2387. #a1# Take a look at the syslog: \kbd{tail -f /var/log/syslog || journalctl}
  2388. salias tlog="tail -f /var/log/syslog" # follow the syslog
  2389. elif check_com -c journalctl ; then
  2390. salias llog="journalctl"
  2391. salias tlog="journalctl -f"
  2392. fi
  2393. # sort installed Debian-packages by size
  2394. if check_com -c dpkg-query ; then
  2395. #a3# List installed Debian-packages sorted by size
  2396. alias debs-by-size="dpkg-query -Wf 'x \${Installed-Size} \${Package} \${Status}\n' | sed -ne '/^x /d' -e '/^x \(.*\) install ok installed$/s//\1/p' | sort -nr"
  2397. fi
  2398. # if cdrecord is a symlink (to wodim) or isn't present at all warn:
  2399. if [[ -L /usr/bin/cdrecord ]] || ! check_com -c cdrecord; then
  2400. if check_com -c wodim; then
  2401. function cdrecord () {
  2402. <<__EOF0__
  2403. cdrecord is not provided under its original name by Debian anymore.
  2404. See #377109 in the BTS of Debian for more details.
  2405. Please use the wodim binary instead
  2406. __EOF0__
  2407. return 1
  2408. }
  2409. fi
  2410. fi
  2411. if isgrmlcd; then
  2412. # No core dumps: important for a live-cd-system
  2413. limit -s core 0
  2414. fi
  2415. # grmlstuff
  2416. function grmlstuff () {
  2417. # people should use 'grml-x'!
  2418. if check_com -c 915resolution; then
  2419. function 855resolution () {
  2420. echo "Please use 915resolution as resolution modifying tool for Intel \
  2421. graphic chipset."
  2422. return -1
  2423. }
  2424. fi
  2425. #a1# Output version of running grml
  2426. alias grml-version='cat /etc/grml_version'
  2427. if check_com -c grml-debootstrap ; then
  2428. function debian2hd () {
  2429. echo "Installing debian to harddisk is possible by using grml-debootstrap."
  2430. return 1
  2431. }
  2432. fi
  2433. }
  2434. # now run the functions
  2435. isgrml && checkhome
  2436. is4 && isgrml && grmlstuff
  2437. is4 && grmlcomp
  2438. # keephack
  2439. is4 && xsource "/etc/zsh/keephack"
  2440. # wonderful idea of using "e" glob qualifier by Peter Stephenson
  2441. # You use it as follows:
  2442. # $ NTREF=/reference/file
  2443. # $ ls -l *(e:nt:)
  2444. # This lists all the files in the current directory newer than the reference file.
  2445. # You can also specify the reference file inline; note quotes:
  2446. # $ ls -l *(e:'nt ~/.zshenv':)
  2447. is4 && function nt () {
  2448. if [[ -n $1 ]] ; then
  2449. local NTREF=${~1}
  2450. fi
  2451. [[ $REPLY -nt $NTREF ]]
  2452. }
  2453. # shell functions
  2454. #f1# Reload an autoloadable function
  2455. function freload () { while (( $# )); do; unfunction $1; autoload -U $1; shift; done }
  2456. compdef _functions freload
  2457. #
  2458. # Usage:
  2459. #
  2460. # e.g.: a -> b -> c -> d ....
  2461. #
  2462. # sll a
  2463. #
  2464. #
  2465. # if parameter is given with leading '=', lookup $PATH for parameter and resolve that
  2466. #
  2467. # sll =java
  2468. #
  2469. # Note: limit for recursive symlinks on linux:
  2470. # http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/namei.c?id=refs/heads/master#l808
  2471. # This limits recursive symlink follows to 8,
  2472. # while limiting consecutive symlinks to 40.
  2473. #
  2474. # When resolving and displaying information about symlinks, no check is made
  2475. # that the displayed information does make any sense on your OS.
  2476. # We leave that decission to the user.
  2477. #
  2478. # The zstat module is used to detect symlink loops. zstat is available since zsh4.
  2479. # With an older zsh you will need to abort with <C-c> in that case.
  2480. # When a symlink loop is detected, a warning ist printed and further processing is stopped.
  2481. #
  2482. # Module zstat is loaded by default in grml zshrc, no extra action needed for that.
  2483. #
  2484. # Known bugs:
  2485. # If you happen to come across a symlink that points to a destination on another partition
  2486. # with the same inode number, that will be marked as symlink loop though it is not.
  2487. # Two hints for this situation:
  2488. # I) Play lottery the same day, as you seem to be rather lucky right now.
  2489. # II) Send patches.
  2490. #
  2491. # return status:
  2492. # 0 upon success
  2493. # 1 file/dir not accesible
  2494. # 2 symlink loop detected
  2495. #
  2496. #f1# List symlinks in detail (more detailed version of 'readlink -f', 'whence -s' and 'namei -l')
  2497. function sll () {
  2498. if [[ -z ${1} ]] ; then
  2499. printf 'Usage: %s <symlink(s)>\n' "${0}"
  2500. return 1
  2501. fi
  2502. local file jumpd curdir
  2503. local -i 10 RTN LINODE i
  2504. local -a SEENINODES
  2505. curdir="${PWD}"
  2506. RTN=0
  2507. for file in "${@}" ; do
  2508. SEENINODES=()
  2509. ls -l "${file:a}" || RTN=1
  2510. while [[ -h "$file" ]] ; do
  2511. if is4 ; then
  2512. LINODE=$(zstat -L +inode "${file}")
  2513. for i in ${SEENINODES} ; do
  2514. if (( ${i} == ${LINODE} )) ; then
  2515. builtin cd -q "${curdir}"
  2516. print 'link loop detected, aborting!'
  2517. return 2
  2518. fi
  2519. done
  2520. SEENINODES+=${LINODE}
  2521. fi
  2522. jumpd="${file:h}"
  2523. file="${file:t}"
  2524. if [[ -d ${jumpd} ]] ; then
  2525. builtin cd -q "${jumpd}" || RTN=1
  2526. fi
  2527. file=$(readlink "$file")
  2528. jumpd="${file:h}"
  2529. file="${file:t}"
  2530. if [[ -d ${jumpd} ]] ; then
  2531. builtin cd -q "${jumpd}" || RTN=1
  2532. fi
  2533. ls -l "${PWD}/${file}" || RTN=1
  2534. done
  2535. shift 1
  2536. if (( ${#} >= 1 )) ; then
  2537. print ""
  2538. fi
  2539. builtin cd -q "${curdir}"
  2540. done
  2541. return ${RTN}
  2542. }
  2543. # TODO: Is it supported to use pager settings like this?
  2544. # PAGER='less -Mr' - If so, the use of $PAGER here needs fixing
  2545. # with respect to wordsplitting. (ie. ${=PAGER})
  2546. if check_com -c $PAGER ; then
  2547. #f3# View Debian's changelog of given package(s)
  2548. function dchange () {
  2549. emulate -L zsh
  2550. [[ -z "$1" ]] && printf 'Usage: %s <package_name(s)>\n' "$0" && return 1
  2551. local package
  2552. for package in "$@" ; do
  2553. if [[ -r /usr/share/doc/${package}/changelog.Debian.gz ]] ; then
  2554. $PAGER /usr/share/doc/${package}/changelog.Debian.gz
  2555. elif [[ -r /usr/share/doc/${package}/changelog.gz ]] ; then
  2556. $PAGER /usr/share/doc/${package}/changelog.gz
  2557. elif [[ -r /usr/share/doc/${package}/changelog ]] ; then
  2558. $PAGER /usr/share/doc/${package}/changelog
  2559. else
  2560. if check_com -c aptitude ; then
  2561. echo "No changelog for package $package found, using aptitude to retrieve it."
  2562. aptitude changelog "$package"
  2563. elif check_com -c apt-get ; then
  2564. echo "No changelog for package $package found, using apt-get to retrieve it."
  2565. apt-get changelog "$package"
  2566. else
  2567. echo "No changelog for package $package found, sorry."
  2568. fi
  2569. fi
  2570. done
  2571. }
  2572. function _dchange () { _files -W /usr/share/doc -/ }
  2573. compdef _dchange dchange
  2574. #f3# View Debian's NEWS of a given package
  2575. function dnews () {
  2576. emulate -L zsh
  2577. if [[ -r /usr/share/doc/$1/NEWS.Debian.gz ]] ; then
  2578. $PAGER /usr/share/doc/$1/NEWS.Debian.gz
  2579. else
  2580. if [[ -r /usr/share/doc/$1/NEWS.gz ]] ; then
  2581. $PAGER /usr/share/doc/$1/NEWS.gz
  2582. else
  2583. echo "No NEWS file for package $1 found, sorry."
  2584. return 1
  2585. fi
  2586. fi
  2587. }
  2588. function _dnews () { _files -W /usr/share/doc -/ }
  2589. compdef _dnews dnews
  2590. #f3# View Debian's copyright of a given package
  2591. function dcopyright () {
  2592. emulate -L zsh
  2593. if [[ -r /usr/share/doc/$1/copyright ]] ; then
  2594. $PAGER /usr/share/doc/$1/copyright
  2595. else
  2596. echo "No copyright file for package $1 found, sorry."
  2597. return 1
  2598. fi
  2599. }
  2600. function _dcopyright () { _files -W /usr/share/doc -/ }
  2601. compdef _dcopyright dcopyright
  2602. #f3# View upstream's changelog of a given package
  2603. function uchange () {
  2604. emulate -L zsh
  2605. if [[ -r /usr/share/doc/$1/changelog.gz ]] ; then
  2606. $PAGER /usr/share/doc/$1/changelog.gz
  2607. else
  2608. echo "No changelog for package $1 found, sorry."
  2609. return 1
  2610. fi
  2611. }
  2612. function _uchange () { _files -W /usr/share/doc -/ }
  2613. compdef _uchange uchange
  2614. fi
  2615. # zsh profiling
  2616. function profile () {
  2617. ZSH_PROFILE_RC=1 zsh "$@"
  2618. }
  2619. #f1# Edit an alias via zle
  2620. function edalias () {
  2621. [[ -z "$1" ]] && { echo "Usage: edalias <alias_to_edit>" ; return 1 } || vared aliases'[$1]' ;
  2622. }
  2623. compdef _aliases edalias
  2624. #f1# Edit a function via zle
  2625. function edfunc () {
  2626. [[ -z "$1" ]] && { echo "Usage: edfunc <function_to_edit>" ; return 1 } || zed -f "$1" ;
  2627. }
  2628. compdef _functions edfunc
  2629. # use it e.g. via 'Restart apache2'
  2630. #m# f6 Start() \kbd{service \em{process}}\quad\kbd{start}
  2631. #m# f6 Restart() \kbd{service \em{process}}\quad\kbd{restart}
  2632. #m# f6 Stop() \kbd{service \em{process}}\quad\kbd{stop}
  2633. #m# f6 Reload() \kbd{service \em{process}}\quad\kbd{reload}
  2634. #m# f6 Force-Reload() \kbd{service \em{process}}\quad\kbd{force-reload}
  2635. #m# f6 Status() \kbd{service \em{process}}\quad\kbd{status}
  2636. if [[ -d /etc/init.d || -d /etc/service ]] ; then
  2637. function __start_stop () {
  2638. local action_="${1:l}" # e.g Start/Stop/Restart
  2639. local service_="$2"
  2640. local param_="$3"
  2641. local service_target_="$(readlink /etc/init.d/$service_)"
  2642. if [[ $service_target_ == "/usr/bin/sv" ]]; then
  2643. # runit
  2644. case "${action_}" in
  2645. start) if [[ ! -e /etc/service/$service_ ]]; then
  2646. $SUDO ln -s "/etc/sv/$service_" "/etc/service/"
  2647. else
  2648. $SUDO "/etc/init.d/$service_" "${action_}" "$param_"
  2649. fi ;;
  2650. # there is no reload in runits sysv emulation
  2651. reload) $SUDO "/etc/init.d/$service_" "force-reload" "$param_" ;;
  2652. *) $SUDO "/etc/init.d/$service_" "${action_}" "$param_" ;;
  2653. esac
  2654. else
  2655. # sysv/sysvinit-utils, upstart
  2656. if check_com -c service ; then
  2657. $SUDO service "$service_" "${action_}" "$param_"
  2658. else
  2659. $SUDO "/etc/init.d/$service_" "${action_}" "$param_"
  2660. fi
  2661. fi
  2662. }
  2663. function _grmlinitd () {
  2664. local -a scripts
  2665. scripts=( /etc/init.d/*(x:t) )
  2666. _describe "service startup script" scripts
  2667. }
  2668. for i in Start Restart Stop Force-Reload Reload Status ; do
  2669. eval "function $i () { __start_stop $i \"\$1\" \"\$2\" ; }"
  2670. compdef _grmlinitd $i
  2671. done
  2672. builtin unset -v i
  2673. fi
  2674. #f1# Provides useful information on globbing
  2675. function H-Glob () {
  2676. echo -e "
  2677. / directories
  2678. . plain files
  2679. @ symbolic links
  2680. = sockets
  2681. p named pipes (FIFOs)
  2682. * executable plain files (0100)
  2683. % device files (character or block special)
  2684. %b block special files
  2685. %c character special files
  2686. r owner-readable files (0400)
  2687. w owner-writable files (0200)
  2688. x owner-executable files (0100)
  2689. A group-readable files (0040)
  2690. I group-writable files (0020)
  2691. E group-executable files (0010)
  2692. R world-readable files (0004)
  2693. W world-writable files (0002)
  2694. X world-executable files (0001)
  2695. s setuid files (04000)
  2696. S setgid files (02000)
  2697. t files with the sticky bit (01000)
  2698. print *(m-1) # Files modified up to a day ago
  2699. print *(a1) # Files accessed a day ago
  2700. print *(@) # Just symlinks
  2701. print *(Lk+50) # Files bigger than 50 kilobytes
  2702. print *(Lk-50) # Files smaller than 50 kilobytes
  2703. print **/*.c # All *.c files recursively starting in \$PWD
  2704. print **/*.c~file.c # Same as above, but excluding 'file.c'
  2705. print (foo|bar).* # Files starting with 'foo' or 'bar'
  2706. print *~*.* # All Files that do not contain a dot
  2707. chmod 644 *(.^x) # make all plain non-executable files publically readable
  2708. print -l *(.c|.h) # Lists *.c and *.h
  2709. print **/*(g:users:) # Recursively match all files that are owned by group 'users'
  2710. echo /proc/*/cwd(:h:t:s/self//) # Analogous to >ps ax | awk '{print $1}'<"
  2711. }
  2712. alias help-zshglob=H-Glob
  2713. # grep for running process, like: 'any vim'
  2714. function any () {
  2715. emulate -L zsh
  2716. unsetopt KSH_ARRAYS
  2717. if [[ -z "$1" ]] ; then
  2718. echo "any - grep for process(es) by keyword" >&2
  2719. echo "Usage: any <keyword>" >&2 ; return 1
  2720. else
  2721. ps xauwww | grep -i "${grep_options[@]}" "[${1[1]}]${1[2,-1]}"
  2722. fi
  2723. }
  2724. # After resuming from suspend, system is paging heavily, leading to very bad interactivity.
  2725. # taken from $LINUX-KERNELSOURCE/Documentation/power/swsusp.txt
  2726. [[ -r /proc/1/maps ]] && \
  2727. function deswap () {
  2728. print 'Reading /proc/[0-9]*/maps and sending output to /dev/null, this might take a while.'
  2729. cat $(sed -ne 's:.* /:/:p' /proc/[0-9]*/maps | sort -u | grep -v '^/dev/') > /dev/null
  2730. print 'Finished, running "swapoff -a; swapon -a" may also be useful.'
  2731. }
  2732. # a wrapper for vim, that deals with title setting
  2733. # VIM_OPTIONS
  2734. # set this array to a set of options to vim you always want
  2735. # to have set when calling vim (in .zshrc.local), like:
  2736. # VIM_OPTIONS=( -p )
  2737. # This will cause vim to send every file given on the
  2738. # commandline to be send to it's own tab (needs vim7).
  2739. if check_com vim; then
  2740. function vim () {
  2741. VIM_PLEASE_SET_TITLE='yes' command vim ${VIM_OPTIONS} "$@"
  2742. }
  2743. fi
  2744. ssl_hashes=( sha512 sha256 sha1 md5 )
  2745. for sh in ${ssl_hashes}; do
  2746. eval 'ssl-cert-'${sh}'() {
  2747. emulate -L zsh
  2748. if [[ -z $1 ]] ; then
  2749. printf '\''usage: %s <file>\n'\'' "ssh-cert-'${sh}'"
  2750. return 1
  2751. fi
  2752. openssl x509 -noout -fingerprint -'${sh}' -in $1
  2753. }'
  2754. done; unset sh
  2755. function ssl-cert-fingerprints () {
  2756. emulate -L zsh
  2757. local i
  2758. if [[ -z $1 ]] ; then
  2759. printf 'usage: ssl-cert-fingerprints <file>\n'
  2760. return 1
  2761. fi
  2762. for i in ${ssl_hashes}
  2763. do ssl-cert-$i $1;
  2764. done
  2765. }
  2766. function ssl-cert-info () {
  2767. emulate -L zsh
  2768. if [[ -z $1 ]] ; then
  2769. printf 'usage: ssl-cert-info <file>\n'
  2770. return 1
  2771. fi
  2772. openssl x509 -noout -text -in $1
  2773. ssl-cert-fingerprints $1
  2774. }
  2775. # make sure our environment is clean regarding colors
  2776. builtin unset -v BLUE RED GREEN CYAN YELLOW MAGENTA WHITE NO_COLOR
  2777. # "persistent history"
  2778. # just write important commands you always need to $GRML_IMPORTANT_COMMANDS
  2779. # defaults for backward compatibility to ~/.important_commands
  2780. if [[ -r ~/.important_commands ]] ; then
  2781. GRML_IMPORTANT_COMMANDS=~/.important_commands
  2782. else
  2783. GRML_IMPORTANT_COMMANDS=${GRML_IMPORTANT_COMMANDS:-${ZDOTDIR:-${HOME}}/.important_commands}
  2784. fi
  2785. [[ -r ${GRML_IMPORTANT_COMMANDS} ]] && builtin fc -R ${GRML_IMPORTANT_COMMANDS}
  2786. # load the lookup subsystem if it's available on the system
  2787. zrcautoload lookupinit && lookupinit
  2788. # variables
  2789. # set terminal property (used e.g. by msgid-chooser)
  2790. export COLORTERM="yes"
  2791. # aliases
  2792. # general
  2793. #a2# Execute \kbd{du -sch}
  2794. [[ -n "$GRML_NO_SMALL_ALIASES" ]] || alias da='du -sch'
  2795. # listing stuff
  2796. #a2# Execute \kbd{ls -lSrah}
  2797. alias dir="command ls -lSrah"
  2798. #a2# Only show dot-directories
  2799. alias lad='command ls -d .*(/)'
  2800. #a2# Only show dot-files
  2801. alias lsa='command ls -a .*(.)'
  2802. #a2# Only files with setgid/setuid/sticky flag
  2803. alias lss='command ls -l *(s,S,t)'
  2804. #a2# Only show symlinks
  2805. alias lsl='command ls -l *(@)'
  2806. #a2# Display only executables
  2807. alias lsx='command ls -l *(*)'
  2808. #a2# Display world-{readable,writable,executable} files
  2809. alias lsw='command ls -ld *(R,W,X.^ND/)'
  2810. #a2# Display the ten biggest files
  2811. alias lsbig="command ls -flh *(.OL[1,10])"
  2812. #a2# Only show directories
  2813. alias lsd='command ls -d *(/)'
  2814. #a2# Only show empty directories
  2815. alias lse='command ls -d *(/^F)'
  2816. #a2# Display the ten newest files
  2817. alias lsnew="command ls -rtlh *(D.om[1,10])"
  2818. #a2# Display the ten oldest files
  2819. alias lsold="command ls -rtlh *(D.Om[1,10])"
  2820. #a2# Display the ten smallest files
  2821. alias lssmall="command ls -Srl *(.oL[1,10])"
  2822. #a2# Display the ten newest directories and ten newest .directories
  2823. alias lsnewdir="command ls -rthdl *(/om[1,10]) .*(D/om[1,10])"
  2824. #a2# Display the ten oldest directories and ten oldest .directories
  2825. alias lsolddir="command ls -rthdl *(/Om[1,10]) .*(D/Om[1,10])"
  2826. # some useful aliases
  2827. #a2# Remove current empty directory. Execute \kbd{cd ..; rmdir \$OLDCWD}
  2828. alias rmcdir='cd ..; rmdir $OLDPWD || cd $OLDPWD'
  2829. #a2# ssh with StrictHostKeyChecking=no \\&\quad and UserKnownHostsFile unset
  2830. alias insecssh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
  2831. #a2# scp with StrictHostKeyChecking=no \\&\quad and UserKnownHostsFile unset
  2832. alias insecscp='scp -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
  2833. # work around non utf8 capable software in utf environment via $LANG and luit
  2834. if check_com isutfenv && check_com luit ; then
  2835. if check_com -c mrxvt ; then
  2836. isutfenv && [[ -n "$LANG" ]] && \
  2837. alias mrxvt="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit mrxvt"
  2838. fi
  2839. if check_com -c aterm ; then
  2840. isutfenv && [[ -n "$LANG" ]] && \
  2841. alias aterm="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit aterm"
  2842. fi
  2843. if check_com -c centericq ; then
  2844. isutfenv && [[ -n "$LANG" ]] && \
  2845. alias centericq="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit centericq"
  2846. fi
  2847. fi
  2848. # useful functions
  2849. #f5# Backup \kbd{file_or_folder {\rm to} file_or_folder\_timestamp}
  2850. function bk () {
  2851. emulate -L zsh
  2852. local current_date=$(date -u "+%Y%m%dT%H%M%SZ")
  2853. local clean keep move verbose result all to_bk
  2854. setopt extended_glob
  2855. keep=1
  2856. while getopts ":hacmrv" opt; do
  2857. case $opt in
  2858. a) (( all++ ));;
  2859. c) unset move clean && (( ++keep ));;
  2860. m) unset keep clean && (( ++move ));;
  2861. r) unset move keep && (( ++clean ));;
  2862. v) verbose="-v";;
  2863. h) <<__EOF0__
  2864. bk [-hcmv] FILE [FILE ...]
  2865. bk -r [-av] [FILE [FILE ...]]
  2866. Backup a file or folder in place and append the timestamp
  2867. Remove backups of a file or folder, or all backups in the current directory
  2868. Usage:
  2869. -h Display this help text
  2870. -c Keep the file/folder as is, create a copy backup using cp(1) (default)
  2871. -m Move the file/folder, using mv(1)
  2872. -r Remove backups of the specified file or directory, using rm(1). If none
  2873. is provided, remove all backups in the current directory.
  2874. -a Remove all (even hidden) backups.
  2875. -v Verbose
  2876. The -c, -r and -m options are mutually exclusive. If specified at the same time,
  2877. the last one is used.
  2878. The return code is the sum of all cp/mv/rm return codes.
  2879. __EOF0__
  2880. return 0;;
  2881. \?) bk -h >&2; return 1;;
  2882. esac
  2883. done
  2884. shift "$((OPTIND-1))"
  2885. if (( keep > 0 )); then
  2886. if islinux || isfreebsd; then
  2887. for to_bk in "$@"; do
  2888. cp $verbose -a "${to_bk%/}" "${to_bk%/}_$current_date"
  2889. (( result += $? ))
  2890. done
  2891. else
  2892. for to_bk in "$@"; do
  2893. cp $verbose -pR "${to_bk%/}" "${to_bk%/}_$current_date"
  2894. (( result += $? ))
  2895. done
  2896. fi
  2897. elif (( move > 0 )); then
  2898. while (( $# > 0 )); do
  2899. mv $verbose "${1%/}" "${1%/}_$current_date"
  2900. (( result += $? ))
  2901. shift
  2902. done
  2903. elif (( clean > 0 )); then
  2904. if (( $# > 0 )); then
  2905. for to_bk in "$@"; do
  2906. rm $verbose -rf "${to_bk%/}"_[0-9](#c8)T([0-1][0-9]|2[0-3])([0-5][0-9])(#c2)Z
  2907. (( result += $? ))
  2908. done
  2909. else
  2910. if (( all > 0 )); then
  2911. rm $verbose -rf *_[0-9](#c8)T([0-1][0-9]|2[0-3])([0-5][0-9])(#c2)Z(D)
  2912. else
  2913. rm $verbose -rf *_[0-9](#c8)T([0-1][0-9]|2[0-3])([0-5][0-9])(#c2)Z
  2914. fi
  2915. (( result += $? ))
  2916. fi
  2917. fi
  2918. return $result
  2919. }
  2920. #f5# cd to directory and list files
  2921. function cl () {
  2922. emulate -L zsh
  2923. cd $1 && ls -a
  2924. }
  2925. # smart cd function, allows switching to /etc when running 'cd /etc/fstab'
  2926. function cd () {
  2927. if (( ${#argv} == 1 )) && [[ -f ${1} ]]; then
  2928. [[ ! -e ${1:h} ]] && return 1
  2929. print "Correcting ${1} to ${1:h}"
  2930. builtin cd ${1:h}
  2931. else
  2932. builtin cd "$@"
  2933. fi
  2934. }
  2935. #f5# Create Directory and \kbd{cd} to it
  2936. function mkcd () {
  2937. if (( ARGC != 1 )); then
  2938. printf 'usage: mkcd <new-directory>\n'
  2939. return 1;
  2940. fi
  2941. if [[ ! -d "$1" ]]; then
  2942. command mkdir -p "$1"
  2943. else
  2944. printf '`%s'\'' already exists: cd-ing.\n' "$1"
  2945. fi
  2946. builtin cd "$1"
  2947. }
  2948. #f5# Create temporary directory and \kbd{cd} to it
  2949. function cdt () {
  2950. builtin cd "$(mktemp -d)"
  2951. builtin pwd
  2952. }
  2953. #f5# List files which have been accessed within the last {\it n} days, {\it n} defaults to 1
  2954. function accessed () {
  2955. emulate -L zsh
  2956. print -l -- *(a-${1:-1})
  2957. }
  2958. #f5# List files which have been changed within the last {\it n} days, {\it n} defaults to 1
  2959. function changed () {
  2960. emulate -L zsh
  2961. print -l -- *(c-${1:-1})
  2962. }
  2963. #f5# List files which have been modified within the last {\it n} days, {\it n} defaults to 1
  2964. function modified () {
  2965. emulate -L zsh
  2966. print -l -- *(m-${1:-1})
  2967. }
  2968. # modified() was named new() in earlier versions, add an alias for backwards compatibility
  2969. check_com new || alias new=modified
  2970. # use colors when GNU grep with color-support
  2971. if (( $#grep_options > 0 )); then
  2972. o=${grep_options:+"${grep_options[*]}"}
  2973. #a2# Execute \kbd{grep -{}-color=auto}
  2974. alias grep='grep '$o
  2975. alias egrep='egrep '$o
  2976. unset o
  2977. fi
  2978. # Translate DE<=>EN
  2979. # 'translate' looks up a word in a file with language-to-language
  2980. # translations (field separator should be " : "). A typical wordlist looks
  2981. # like the following:
  2982. # | english-word : german-translation
  2983. # It's also only possible to translate english to german but not reciprocal.
  2984. # Use the following oneliner to reverse the sort order:
  2985. # $ awk -F ':' '{ print $2" : "$1" "$3 }' \
  2986. # /usr/local/lib/words/en-de.ISO-8859-1.vok > ~/.translate/de-en.ISO-8859-1.vok
  2987. #f5# Translates a word
  2988. function trans () {
  2989. emulate -L zsh
  2990. case "$1" in
  2991. -[dD]*)
  2992. translate -l de-en $2
  2993. ;;
  2994. -[eE]*)
  2995. translate -l en-de $2
  2996. ;;
  2997. *)
  2998. echo "Usage: $0 { -D | -E }"
  2999. echo " -D == German to English"
  3000. echo " -E == English to German"
  3001. esac
  3002. }
  3003. # Usage: simple-extract <file>
  3004. # Using option -d deletes the original archive file.
  3005. #f5# Smart archive extractor
  3006. function simple-extract () {
  3007. emulate -L zsh
  3008. setopt extended_glob noclobber
  3009. local ARCHIVE DELETE_ORIGINAL DECOMP_CMD USES_STDIN USES_STDOUT GZTARGET WGET_CMD
  3010. local RC=0
  3011. zparseopts -D -E "d=DELETE_ORIGINAL"
  3012. for ARCHIVE in "${@}"; do
  3013. case $ARCHIVE in
  3014. *(tar.bz2|tbz2|tbz))
  3015. DECOMP_CMD="tar -xvjf -"
  3016. USES_STDIN=true
  3017. USES_STDOUT=false
  3018. ;;
  3019. *(tar.gz|tgz))
  3020. DECOMP_CMD="tar -xvzf -"
  3021. USES_STDIN=true
  3022. USES_STDOUT=false
  3023. ;;
  3024. *(tar.xz|txz|tar.lzma))
  3025. DECOMP_CMD="tar -xvJf -"
  3026. USES_STDIN=true
  3027. USES_STDOUT=false
  3028. ;;
  3029. *tar)
  3030. DECOMP_CMD="tar -xvf -"
  3031. USES_STDIN=true
  3032. USES_STDOUT=false
  3033. ;;
  3034. *rar)
  3035. DECOMP_CMD="unrar x"
  3036. USES_STDIN=false
  3037. USES_STDOUT=false
  3038. ;;
  3039. *lzh)
  3040. DECOMP_CMD="lha x"
  3041. USES_STDIN=false
  3042. USES_STDOUT=false
  3043. ;;
  3044. *7z)
  3045. DECOMP_CMD="7z x"
  3046. USES_STDIN=false
  3047. USES_STDOUT=false
  3048. ;;
  3049. *(zip|jar))
  3050. DECOMP_CMD="unzip"
  3051. USES_STDIN=false
  3052. USES_STDOUT=false
  3053. ;;
  3054. *deb)
  3055. DECOMP_CMD="ar -x"
  3056. USES_STDIN=false
  3057. USES_STDOUT=false
  3058. ;;
  3059. *bz2)
  3060. DECOMP_CMD="bzip2 -d -c -"
  3061. USES_STDIN=true
  3062. USES_STDOUT=true
  3063. ;;
  3064. *(gz|Z))
  3065. DECOMP_CMD="gzip -d -c -"
  3066. USES_STDIN=true
  3067. USES_STDOUT=true
  3068. ;;
  3069. *(xz|lzma))
  3070. DECOMP_CMD="xz -d -c -"
  3071. USES_STDIN=true
  3072. USES_STDOUT=true
  3073. ;;
  3074. *)
  3075. print "ERROR: '$ARCHIVE' has unrecognized archive type." >&2
  3076. RC=$((RC+1))
  3077. continue
  3078. ;;
  3079. esac
  3080. if ! check_com ${DECOMP_CMD[(w)1]}; then
  3081. echo "ERROR: ${DECOMP_CMD[(w)1]} not installed." >&2
  3082. RC=$((RC+2))
  3083. continue
  3084. fi
  3085. GZTARGET="${ARCHIVE:t:r}"
  3086. if [[ -f $ARCHIVE ]] ; then
  3087. print "Extracting '$ARCHIVE' ..."
  3088. if $USES_STDIN; then
  3089. if $USES_STDOUT; then
  3090. ${=DECOMP_CMD} < "$ARCHIVE" > $GZTARGET
  3091. else
  3092. ${=DECOMP_CMD} < "$ARCHIVE"
  3093. fi
  3094. else
  3095. if $USES_STDOUT; then
  3096. ${=DECOMP_CMD} "$ARCHIVE" > $GZTARGET
  3097. else
  3098. ${=DECOMP_CMD} "$ARCHIVE"
  3099. fi
  3100. fi
  3101. [[ $? -eq 0 && -n "$DELETE_ORIGINAL" ]] && rm -f "$ARCHIVE"
  3102. elif [[ "$ARCHIVE" == (#s)(https|http|ftp)://* ]] ; then
  3103. if check_com curl; then
  3104. WGET_CMD="curl -L -s -o -"
  3105. elif check_com wget; then
  3106. WGET_CMD="wget -q -O -"
  3107. elif check_com fetch; then
  3108. WGET_CMD="fetch -q -o -"
  3109. else
  3110. print "ERROR: neither wget, curl nor fetch is installed" >&2
  3111. RC=$((RC+4))
  3112. continue
  3113. fi
  3114. print "Downloading and Extracting '$ARCHIVE' ..."
  3115. if $USES_STDIN; then
  3116. if $USES_STDOUT; then
  3117. ${=WGET_CMD} "$ARCHIVE" | ${=DECOMP_CMD} > $GZTARGET
  3118. RC=$((RC+$?))
  3119. else
  3120. ${=WGET_CMD} "$ARCHIVE" | ${=DECOMP_CMD}
  3121. RC=$((RC+$?))
  3122. fi
  3123. else
  3124. if $USES_STDOUT; then
  3125. ${=DECOMP_CMD} =(${=WGET_CMD} "$ARCHIVE") > $GZTARGET
  3126. else
  3127. ${=DECOMP_CMD} =(${=WGET_CMD} "$ARCHIVE")
  3128. fi
  3129. fi
  3130. else
  3131. print "ERROR: '$ARCHIVE' is neither a valid file nor a supported URI." >&2
  3132. RC=$((RC+8))
  3133. fi
  3134. done
  3135. return $RC
  3136. }
  3137. function __archive_or_uri () {
  3138. _alternative \
  3139. 'files:Archives:_files -g "*.(#l)(tar.bz2|tbz2|tbz|tar.gz|tgz|tar.xz|txz|tar.lzma|tar|rar|lzh|7z|zip|jar|deb|bz2|gz|Z|xz|lzma)"' \
  3140. '_urls:Remote Archives:_urls'
  3141. }
  3142. function _simple_extract () {
  3143. _arguments \
  3144. '-d[delete original archivefile after extraction]' \
  3145. '*:Archive Or Uri:__archive_or_uri'
  3146. }
  3147. compdef _simple_extract simple-extract
  3148. [[ -n "$GRML_NO_SMALL_ALIASES" ]] || alias se=simple-extract
  3149. #f5# Change the xterm title from within GNU-screen
  3150. function xtrename () {
  3151. emulate -L zsh
  3152. if [[ $1 != "-f" ]] ; then
  3153. if [[ -z ${DISPLAY} ]] ; then
  3154. printf 'xtrename only makes sense in X11.\n'
  3155. return 1
  3156. fi
  3157. else
  3158. shift
  3159. fi
  3160. if [[ -z $1 ]] ; then
  3161. printf 'usage: xtrename [-f] "title for xterm"\n'
  3162. printf ' renames the title of xterm from _within_ screen.\n'
  3163. printf ' also works without screen.\n'
  3164. printf ' will not work if DISPLAY is unset, use -f to override.\n'
  3165. return 0
  3166. fi
  3167. print -n "\eP\e]0;${1}\C-G\e\\"
  3168. return 0
  3169. }
  3170. # Create small urls via http://goo.gl using curl(1).
  3171. # API reference: https://code.google.com/apis/urlshortener/
  3172. function zurl () {
  3173. emulate -L zsh
  3174. setopt extended_glob
  3175. if [[ -z $1 ]]; then
  3176. print "USAGE: zurl <URL>"
  3177. return 1
  3178. fi
  3179. local PN url prog api json contenttype item
  3180. local -a data
  3181. PN=$0
  3182. url=$1
  3183. # Prepend 'http://' to given URL where necessary for later output.
  3184. if [[ ${url} != http(s|)://* ]]; then
  3185. url='http://'${url}
  3186. fi
  3187. if check_com -c curl; then
  3188. prog=curl
  3189. else
  3190. print "curl is not available, but mandatory for ${PN}. Aborting."
  3191. return 1
  3192. fi
  3193. api='https://www.googleapis.com/urlshortener/v1/url'
  3194. contenttype="Content-Type: application/json"
  3195. json="{\"longUrl\": \"${url}\"}"
  3196. data=(${(f)"$($prog --silent -H ${contenttype} -d ${json} $api)"})
  3197. # Parse the response
  3198. for item in "${data[@]}"; do
  3199. case "$item" in
  3200. ' '#'"id":'*)
  3201. item=${item#*: \"}
  3202. item=${item%\",*}
  3203. printf '%s\n' "$item"
  3204. return 0
  3205. ;;
  3206. esac
  3207. done
  3208. return 1
  3209. }
  3210. #f2# Find history events by search pattern and list them by date.
  3211. function whatwhen () {
  3212. emulate -L zsh
  3213. local usage help ident format_l format_s first_char remain first last
  3214. usage='USAGE: whatwhen [options] <searchstring> <search range>'
  3215. help='Use `whatwhen -h'\'' for further explanations.'
  3216. ident=${(l,${#${:-Usage: }},, ,)}
  3217. format_l="${ident}%s\t\t\t%s\n"
  3218. format_s="${format_l//(\\t)##/\\t}"
  3219. # Make the first char of the word to search for case
  3220. # insensitive; e.g. [aA]
  3221. first_char=[${(L)1[1]}${(U)1[1]}]
  3222. remain=${1[2,-1]}
  3223. # Default search range is `-100'.
  3224. first=${2:-\-100}
  3225. # Optional, just used for `<first> <last>' given.
  3226. last=$3
  3227. case $1 in
  3228. ("")
  3229. printf '%s\n\n' 'ERROR: No search string specified. Aborting.'
  3230. printf '%s\n%s\n\n' ${usage} ${help} && return 1
  3231. ;;
  3232. (-h)
  3233. printf '%s\n\n' ${usage}
  3234. print 'OPTIONS:'
  3235. printf $format_l '-h' 'show help text'
  3236. print '\f'
  3237. print 'SEARCH RANGE:'
  3238. printf $format_l "'0'" 'the whole history,'
  3239. printf $format_l '-<n>' 'offset to the current history number; (default: -100)'
  3240. printf $format_s '<[-]first> [<last>]' 'just searching within a give range'
  3241. printf '\n%s\n' 'EXAMPLES:'
  3242. printf ${format_l/(\\t)/} 'whatwhen grml' '# Range is set to -100 by default.'
  3243. printf $format_l 'whatwhen zsh -250'
  3244. printf $format_l 'whatwhen foo 1 99'
  3245. ;;
  3246. (\?)
  3247. printf '%s\n%s\n\n' ${usage} ${help} && return 1
  3248. ;;
  3249. (*)
  3250. # -l list results on stout rather than invoking $EDITOR.
  3251. # -i Print dates as in YYYY-MM-DD.
  3252. # -m Search for a - quoted - pattern within the history.
  3253. fc -li -m "*${first_char}${remain}*" $first $last
  3254. ;;
  3255. esac
  3256. }
  3257. # mercurial related stuff
  3258. if check_com -c hg ; then
  3259. # gnu like diff for mercurial
  3260. # http://www.selenic.com/mercurial/wiki/index.cgi/TipsAndTricks
  3261. #f5# GNU like diff for mercurial
  3262. function hgdi () {
  3263. emulate -L zsh
  3264. local i
  3265. for i in $(hg status -marn "$@") ; diff -ubwd <(hg cat "$i") "$i"
  3266. }
  3267. # build debian package
  3268. #a2# Alias for \kbd{hg-buildpackage}
  3269. alias hbp='hg-buildpackage'
  3270. # execute commands on the versioned patch-queue from the current repos
  3271. [[ -n "$GRML_NO_SMALL_ALIASES" ]] || alias mq='hg -R $(readlink -f $(hg root)/.hg/patches)'
  3272. # diffstat for specific version of a mercurial repository
  3273. # hgstat => display diffstat between last revision and tip
  3274. # hgstat 1234 => display diffstat between revision 1234 and tip
  3275. #f5# Diffstat for specific version of a mercurial repos
  3276. function hgstat () {
  3277. emulate -L zsh
  3278. [[ -n "$1" ]] && hg diff -r $1 -r tip | diffstat || hg export tip | diffstat
  3279. }
  3280. fi # end of check whether we have the 'hg'-executable
  3281. # disable bracketed paste mode for dumb terminals
  3282. [[ "$TERM" == dumb ]] && unset zle_bracketed_paste
  3283. # grml-small cleanups and workarounds
  3284. # The following is used to remove zsh-config-items that do not work
  3285. # in grml-small by default.
  3286. # If you do not want these adjustments (for whatever reason), set
  3287. # $GRMLSMALL_SPECIFIC to 0 in your .zshrc.pre file (which this configuration
  3288. # sources if it is there).
  3289. if (( GRMLSMALL_SPECIFIC > 0 )) && isgrmlsmall ; then
  3290. # Clean up
  3291. unset "abk[V]"
  3292. unalias 'V' &> /dev/null
  3293. unfunction vman &> /dev/null
  3294. unfunction viless &> /dev/null
  3295. unfunction 2html &> /dev/null
  3296. # manpages are not in grmlsmall
  3297. unfunction manzsh &> /dev/null
  3298. unfunction man2 &> /dev/null
  3299. # Workarounds
  3300. # See https://github.com/grml/grml/issues/56
  3301. if ! [[ -x ${commands[dig]} ]]; then
  3302. function dig_after_all () {
  3303. unfunction dig
  3304. unfunction _dig
  3305. autoload -Uz _dig
  3306. unfunction dig_after_all
  3307. }
  3308. function dig () {
  3309. if [[ -x ${commands[dig]} ]]; then
  3310. dig_after_all
  3311. command dig "$@"
  3312. return "$!"
  3313. fi
  3314. printf 'This installation does not include `dig'\'' for size reasons.\n'
  3315. printf 'Try `drill'\'' as a light weight alternative.\n'
  3316. return 0
  3317. }
  3318. function _dig () {
  3319. if [[ -x ${commands[dig]} ]]; then
  3320. dig_after_all
  3321. zle -M 'Found `dig'\'' installed. '
  3322. else
  3323. zle -M 'Try `drill'\'' instead of `dig'\''.'
  3324. fi
  3325. }
  3326. compdef _dig dig
  3327. fi
  3328. fi
  3329. zrclocal
  3330. ## genrefcard.pl settings
  3331. ### doc strings for external functions from files
  3332. #m# f5 grml-wallpaper() Sets a wallpaper (try completion for possible values)
  3333. ### example: split functions-search 8,16,24,32
  3334. #@# split functions-search 8
  3335. ## END OF FILE #################################################################
  3336. # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4
  3337. # Local variables:
  3338. # mode: sh
  3339. # End:
  3340. MOZ_USE_XINPUT2=1
  3341. unsetopt no_match
  3342. #source /opt/intel/sgxsdk/environment