• programming in python • a course for the curious •

Completion for the linux lpr command

Quite often I print from the command line using the command lpr. Unfortunatelly, the bash-completion package does not provide a command line completion for it. Here you can find my bash completion script which supports all of the lpr switches. The script adds also a completion for the lprm command. How to install it? Well, it depends on the Linux distribution. On Gentoo, just put this script in the $HOME/.bash_completion.d/ folder, or if you want to do that system-wise drop it under /etc/bash_completion.d directory (this directory exists in major Linux distribution, while the former one is Gentoo specific).

  1. # lpr(1) and lprm(1) completions by Marcin Szamotulski <mszamot@gmail.com> March 2011
  2. # This script uses two functions from 'base': _known_hosts_real and _user_at_host.
  3. # to complete -H and -U switches of the lpr command (-h and -U of lprm).
  4. _lpr()
  5. {
  6. local cur prev opts comp_opts configfile
  7. COMPREPLY=()
  8. cur="${COMP_WORDS[COMP_CWORD]}"
  9. prev="${COMP_WORDS[COMP_CWORD-1]}"
  10. comp_opts="+o default +o filenames"
  11. if [[ ${prev} == "=" ]];then
  12. prev="${COMP_WORDS[COMP_CWORD-2]}"
  13. fi
  14. if [[ ${cur} == "-" ]];then
  15. opts="-E -H -C -T -J -P -U -# -h -l -m -o -p -q -r"
  16. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  17. else
  18. case ${prev} in
  19. -o)
  20. opts="scaling= media= sides= Collate= orientation-requested=
  21. job-sheets= job-hold-until= page-ranges= page-set= number-up=
  22. page-border= number-up-layout= outputorder= cpi= lpi= ppi=
  23. columns= page-left= page-right= page-top= page-bottom= position=
  24. natural-scaling= hue= saturation= penwidth= landscape mirror raw
  25. fitplot prettyprint nowrap blackplot";;
  26. sides)
  27. opts="two-sided-long-edge two-sided-short-edge one-sided";;
  28. Collate)
  29. opts="True False";;
  30. job-hold-until)
  31. opts="indefinite day-time night second-shift third-shift
  32. weekend";;
  33. job-sheets)
  34. opts="none classified confidential secret standard topsecret
  35. unclassified";;
  36. media)
  37. opts="Letter Legal A4 COM10 DL Transparency Upper Lower
  38. MultiPurpose LargeCapacity Custom\.";;
  39. number-up-layout)
  40. opts="btlr btrl lrbt lrtb rlbt rltb tblr tbrl";;
  41. outputorder)
  42. opts="normal reverse";;
  43. page-border)
  44. opts="double none double-thick single single-thick";;
  45. page-set)
  46. opts="odd even";;
  47. position)
  48. opts="center top left right top-left top-right bottom bottom-left
  49. bottom-right";;
  50. orientation-requested)
  51. opts="3 4 5 6";;
  52. -P)
  53. opts=`lpstat -a 2>/dev/null | awk '{print $1}'`;;
  54. -i)
  55. opts=`lpstat -R 2>/dev/null | awk '{print $2}' | sed 's/^[a-zA-Z0-9\-]*\-//'`;;
  56. -H)
  57. comp_opts="no compreply"
  58. _known_hosts_real "$cur";;
  59. -U)
  60. comp_opts="no compreply"
  61. _user_at_host;;
  62. -C)
  63. comp_opts="-o filenames";;
  64. -J)
  65. comp_opts="-o filenames";;
  66. -T)
  67. comp_opts="-o filenames";;
  68. -#)
  69. comp_opts="-o filenames";;
  70. *)
  71. opts=""
  72. if [[ ${cur} != "=" ]];then
  73. compopt +o filenames
  74. _filedir
  75. comp_opts="no compreply"
  76. else
  77. return 0
  78. fi;;
  79. esac
  80. fi
  81. [[ ${cur} == "=" ]] && cur=""
  82. if [[ ${comp_opts} != "no compreply" ]];then
  83. if [[ ${comp_opts} != "" ]];then
  84. compopt ${comp_opts}
  85. fi
  86. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );
  87. fi
  88. # For options from opt_array do not append a white space after completin
  89. opt_array=("scaling media sides Collate orientation-requested job-sheets
  90. job-hold-until page-ranges page-set number-up page-border number-up-layout
  91. outputorder cpi lpi ppi columns page-left page-right page-top page-bottom
  92. position natural-scaling hue saturation penwidth")
  93. nospace=0
  94. for i in ${COMPREPLY};do
  95. for j in ${opt_array};do
  96. if [[ $i =~ $j ]];then
  97. nospace=1;
  98. fi
  99. done
  100. done
  101. [[ $nospace == 1 ]] && compopt -o nospace
  102. return 0;
  103. }
  104. complete -F _lpr lpr lp
  105. _lprm()
  106. {
  107. local cur prev opts configfile
  108. COMPREPLY=()
  109. cur="${COMP_WORDS[COMP_CWORD]}"
  110. prev="${COMP_WORDS[COMP_CWORD-1]}"
  111. [[ ${prev} == "=" ]] && prev="${COMP_WORDS[COMP_CWORD-2]}"
  112. if [[ ${cur} == "-" ]];then
  113. opts="-E -P -U -h"
  114. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  115. else
  116. case ${prev} in
  117. -P)
  118. opts=`lpstat -a 2>/dev/null | awk '{print $1}'`
  119. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );;
  120. -h)
  121. _known_hosts_real "$cur";;
  122. -U)
  123. _user_at_host;;
  124. *)
  125. opts=`lpstat -R 2>/dev/null | awk '{print $2}' | sed 's/.*-\([0-9\-]*\)$/\1/'`
  126. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );;
  127. esac
  128. fi
  129. return 0;
  130. }
  131. complete -F _lprm lprm

Contact us.
Created using , and
we are not associated with Python Software Foundation
© Copyright 2012, 2013 Accorda Institute.