Is there a way to print from the cliboard?

Is there a way to print from the cliboard?

Oleg Goldshmidt pub at goldshmidt.org
Tue Mar 17 15:10:33 IST 2009


Michael Shiloh <michaelshiloh1010 at gmail.com> writes:

> I'm editing a file, or viewing a web page, or whatever.
>
> I'm interested in printing only a small portion of it.
>
> I'd like to be able to essentially "print selection".
>
> Of course this would require that each program (gvim, firefox,
> thunderbird, terminal, whatever) provide such an option.

Actually, I think it is not true. What you need is a program that can
access what is selected in X. 

Speaking of old/stupid/useful tricks, I am an emacs guy (sorry vi
afficionados, to each his own, I am a freak, yadda-yadda). Way back
then I had a bunch of emacs utilities that let me perform operations
on text selected anywhere on my screen (accessed via get-selection or
x-get-selection function). The most useful was the code that took the
selection and did a web search on it with my favorite browser and
favorite search engine of the day, but I don't use it anymore because
nowadays every browser comes with a search bar that can be pasted
into.

More to the point, there was also this clumsy snippet that I have just
tested (after all these years) to print to a file selections from
konsole, oowriter, konqueror, and firefox. Amazingly, it still works.

(setq print-selection-command "a2ps -q")
(setq print-destination-option " -o ")

(defun get-print-selection-destination (dest)
  (interactive "sFile to print to: ")
  dest)

(defun print-selection (arg)
  (interactive "P")
  (let ((cmd (concat "echo \""
		     (get-selection)
		     "\" | "
		     print-selection-command
		     (if arg 
			 (concat print-destination-option
				 (call-interactively
				  'get-print-selection-destination))
		       ""))))
    (shell-command cmd)))

Load the code above into emacs (e.g., put it into ~/.emacs), and
at your convenience select some text in X, then in emacs do 

M-x print-selection

and it will be sent to your default printer (unless the specification
of a2ps has changed - just change print-selection-command to "lpr -p"
or something if it has). If you do

C-u M-x print-selection

it will prompt you for an output file name. If you want to dry-run it
change "shell-command" to "message" in the last line of the code.

Caveat emptor - I use xemacs, though I think the snippet should work
with GNU emacs as well.

If emacs is not your primary tool, then something else can probably do
the same.

-- 
Oleg Goldshmidt | pub at goldshmidt.org



More information about the Linux-il mailing list