debian.plugin.zsh 6.7 KB

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