debian.plugin.zsh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Authors:
  2. # https://github.com/AlexBio
  3. # https://github.com/dbb
  4. # https://github.com/Mappleconfusers
  5. #
  6. # Debian-related zsh aliases and functions for zsh
  7. # Use aptitude if installed, or apt-get if not.
  8. # You can just set apt_pref='apt-get' to override it.
  9. if [[ -e $( which -p aptitude 2>&1 ) ]]; then
  10. apt_pref='aptitude'
  11. apt_upgr='safe-upgrade'
  12. else
  13. apt_pref='apt-get'
  14. apt_upgr='upgrade'
  15. fi
  16. # Use sudo by default if it's installed
  17. if [[ -e $( which -p sudo 2>&1 ) ]]; then
  18. use_sudo=1
  19. fi
  20. # Aliases ###################################################################
  21. # These are for more obscure uses of apt-get and aptitude that aren't covered
  22. # below.
  23. alias age='apt-get'
  24. alias api='aptitude'
  25. # Some self-explanatory aliases
  26. alias acs="apt-cache search"
  27. alias aps='aptitude search'
  28. alias as="aptitude -F \"* %p -> %d \n(%v/%V)\" \
  29. --no-gui --disable-columns search" # search package
  30. # apt-file
  31. alias afs='apt-file search --regexp'
  32. # These are apt-get only
  33. alias asrc='apt-get source'
  34. alias app='apt-cache policy'
  35. # superuser operations ######################################################
  36. if [[ $use_sudo -eq 1 ]]; then
  37. # commands using sudo #######
  38. alias aac='sudo $apt_pref autoclean'
  39. alias abd='sudo $apt_pref build-dep'
  40. alias ac='sudo $apt_pref clean'
  41. alias ad='sudo $apt_pref update'
  42. alias adg='sudo $apt_pref update && sudo $apt_pref $apt_upgr'
  43. alias adu='sudo $apt_pref update && sudo $apt_pref dist-upgrade'
  44. alias afu='sudo apt-file update'
  45. alias au='sudo $apt_pref $apt_upgr'
  46. alias ai='sudo $apt_pref install'
  47. # Install all packages given on the command line while using only the first word of each line:
  48. # acs ... | ail
  49. alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | "' xargs sudo $apt_pref install'
  50. alias ap='sudo $apt_pref purge'
  51. alias ar='sudo $apt_pref remove'
  52. # apt-get only
  53. alias ads='sudo apt-get dselect-upgrade'
  54. # Install all .deb files in the current directory.
  55. # Warning: you will need to put the glob in single quotes if you use:
  56. # glob_subst
  57. alias dia='sudo dpkg -i ./*.deb'
  58. alias di='sudo dpkg -i'
  59. # Remove ALL kernel images and headers EXCEPT the one in use
  60. alias kclean='sudo aptitude remove -P ?and(~i~nlinux-(ima|hea) \
  61. ?not(~n`uname -r`))'
  62. # commands using su #########
  63. else
  64. alias aac='su -ls \'$apt_pref autoclean\' root'
  65. abd() {
  66. cmd="su -lc '$apt_pref build-dep $@' root"
  67. print "$cmd"
  68. eval "$cmd"
  69. }
  70. alias ac='su -ls \'$apt_pref clean\' root'
  71. alias ad='su -lc \'$apt_pref update\' root'
  72. alias adg='su -lc \'$apt_pref update && aptitude $apt_upgr\' root'
  73. alias adu='su -lc \'$apt_pref update && aptitude dist-upgrade\' root'
  74. alias afu='su -lc "apt-file update"'
  75. alias ag='su -lc \'$apt_pref $apt_upgr\' root'
  76. ai() {
  77. cmd="su -lc 'aptitude -P install $@' root"
  78. print "$cmd"
  79. eval "$cmd"
  80. }
  81. ap() {
  82. cmd="su -lc '$apt_pref -P purge $@' root"
  83. print "$cmd"
  84. eval "$cmd"
  85. }
  86. ar() {
  87. cmd="su -lc '$apt_pref -P remove $@' root"
  88. print "$cmd"
  89. eval "$cmd"
  90. }
  91. # Install all .deb files in the current directory
  92. # Assumes glob_subst is off
  93. alias dia='su -lc "dpkg -i ./*.deb" root'
  94. alias di='su -lc "dpkg -i" root'
  95. # Remove ALL kernel images and headers EXCEPT the one in use
  96. alias kclean='su -lc '\''aptitude remove -P ?and(~i~nlinux-(ima|hea) \
  97. ?not(~n`uname -r`))'\'' root'
  98. fi
  99. # Completion ################################################################
  100. #
  101. # Registers a compdef for $1 that calls $apt_pref with the commands $2
  102. # To do that it creates a new completion function called _apt_pref_$2
  103. #
  104. apt_pref_compdef() {
  105. local f fb
  106. f="_apt_pref_${2}"
  107. eval "function ${f}() {
  108. shift words;
  109. service=\"\$apt_pref\";
  110. words=(\"\$apt_pref\" '$2' \$words);
  111. ((CURRENT++))
  112. test \"\${apt_pref}\" = 'aptitude' && _aptitude || _apt
  113. }"
  114. compdef "$f" "$1"
  115. }
  116. apt_pref_compdef aac "autoclean"
  117. apt_pref_compdef abd "build-dep"
  118. apt_pref_compdef ac "clean"
  119. apt_pref_compdef ad "update"
  120. apt_pref_compdef afu "update"
  121. apt_pref_compdef ag "$apt_upgr"
  122. apt_pref_compdef ai "install"
  123. apt_pref_compdef ail "install"
  124. apt_pref_compdef ap "purge"
  125. apt_pref_compdef ar "remove"
  126. apt_pref_compdef ads "dselect-upgrade"
  127. # Misc. #####################################################################
  128. # print all installed packages
  129. alias allpkgs='aptitude search -F "%p" --disable-columns ~i'
  130. # Create a basic .deb package
  131. alias mydeb='time dpkg-buildpackage -rfakeroot -us -uc'
  132. # Functions #################################################################
  133. # create a simple script that can be used to 'duplicate' a system
  134. apt-copy() {
  135. print '#!/bin/sh'"\n" > apt-copy.sh
  136. cmd='$apt_pref install'
  137. for p in ${(f)"$(aptitude search -F "%p" --disable-columns \~i)"}; {
  138. cmd="${cmd} ${p}"
  139. }
  140. print $cmd "\n" >> apt-copy.sh
  141. chmod +x apt-copy.sh
  142. }
  143. # Prints apt history
  144. # Usage:
  145. # apt-history install
  146. # apt-history upgrade
  147. # apt-history remove
  148. # apt-history rollback
  149. # apt-history list
  150. # Based On: http://linuxcommando.blogspot.com/2008/08/how-to-show-apt-log-history.html
  151. apt-history () {
  152. case "$1" in
  153. install)
  154. zgrep --no-filename 'install ' $(ls -rt /var/log/dpkg*)
  155. ;;
  156. upgrade|remove)
  157. zgrep --no-filename $1 $(ls -rt /var/log/dpkg*)
  158. ;;
  159. rollback)
  160. zgrep --no-filename upgrade $(ls -rt /var/log/dpkg*) | \
  161. grep "$2" -A10000000 | \
  162. grep "$3" -B10000000 | \
  163. awk '{print $4"="$5}'
  164. ;;
  165. list)
  166. zgrep --no-filename '' $(ls -rt /var/log/dpkg*)
  167. ;;
  168. *)
  169. echo "Parameters:"
  170. echo " install - Lists all packages that have been installed."
  171. echo " upgrade - Lists all packages that have been upgraded."
  172. echo " remove - Lists all packages that have been removed."
  173. echo " rollback - Lists rollback information."
  174. echo " list - Lists all contains of dpkg logs."
  175. ;;
  176. esac
  177. }
  178. # Kernel-package building shortcut
  179. kerndeb () {
  180. # temporarily unset MAKEFLAGS ( '-j3' will fail )
  181. MAKEFLAGS=$( print - $MAKEFLAGS | perl -pe 's/-j\s*[\d]+//g' )
  182. print '$MAKEFLAGS set to '"'$MAKEFLAGS'"
  183. appendage='-custom' # this shows up in $ (uname -r )
  184. revision=$(date +"%Y%m%d") # this shows up in the .deb file name
  185. make-kpkg clean
  186. time fakeroot make-kpkg --append-to-version "$appendage" --revision \
  187. "$revision" kernel_image kernel_headers
  188. }
  189. # List packages by size
  190. function apt-list-packages {
  191. dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n' | \
  192. grep -v deinstall | \
  193. sort -n | \
  194. awk '{print $1" "$2}'
  195. }