2020-04-06 Printing from the command line

Recently, I decided that the time has come to go through another geek rite of passage and to learn how to print files from the command line. Being on GNU/Linux, I (of course) use CUPS for printing. Here is the first complication: there are two command-line utilities for printing in CUPS – lp, and lpr. I am not sure why I’d need two, but the reason for their existence seems to be some legacy mess. Apparently, lp is slightly better in my opinion, since it lets the user print only the selected pages of the document; otherwise, the two commands are pretty similar in terms of their capabilities and differ mainly in their UI.

Anyway, the most interesting options to lp are:

The easiest scenario is lp <file>, which just prints the <file> on the default printer. (The default printer is specified via one of the environment variables LPDEST or PRINTER, and if neither is set, via a config set by lpoptions. I started with using a variable, but then learned that lpoptions is really useful, so I switched to that. For example, assuming that I have a printer called my_printer, I can say lpoptions -d my_printer to make it the default. The options are saved in ~/.cups/lpoptions.)

The reason I found lpoptions useful was that it lets the user create so-called instances of printers, i.e., sets of predefined options for a printer.

I print pdfs 2-up (i.e., two pages per physical sheet of paper) quite often, so I want to have an easy way of specifying that this is what I want, too. Instead of saying lp -o number-up=2 <file> every time, I can define an instance of the printer my_printer with this option set this way: lpoptions -p my_printer/2up -o number-up=2. From now on, lp -d my_printer/2up <file> will print <file> 2-up. (This is not that much shorter than the -o way, but in case I want this “instance” to contain more options, it is clearly a win.

The last thing that seems useful to me is classes of printers. Grouping two or more printers in a class lets the user send the document to a class, and it will then be printed on the first free printer in this class. This might be useful if I have one printer at home and another one in the office – I assume that this would let me say just lp <file> and print it on whichever printer I have attached to my computer, although I won’t be able to check it until I get to my office (not sure when this is going to happen…).

Now, the only thing left is to set up Emacs to use my new-found knowledge of command-line printing. If I don’t want to print 2-up, this is easy – just C-c C-p from a pdf-tools buffer, but I want to be able to select 2-up printing. This will require a bit of coding, which I am going to do soon. Stay tuned!

CategoryEnglish, CategoryBlog