debian.plugin.zsh 6.6 KB

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