debian.plugin.zsh 6.0 KB

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