4

I would like to be able to send my currently open text file to the paper printer.

I often use nvim to create a daily scratch to do list, and need a paper copy to go with me in my back pocket through the day.

How can I set-up nvim to send a copy of the current text in nvim to the paper printer?

I'm using the latest nvim, v0.6.0, on the latest linux kernel, v5.15.5-arch1-1.

3
  • 2
    Did you try :ha[rdcopy]?
    – Dubu
    Dec 9, 2021 at 14:23
  • Oh goodness, how did I miss that!! Thanks
    – Kes
    Dec 9, 2021 at 15:06
  • Thanks @Dubu i've updated the answer to reflect this simple most obvious option
    – Kes
    Dec 9, 2021 at 15:52

1 Answer 1

3

The below solution works on the most up to date version of nvim, v0.6.0, and the most up to date arch linux which I am using, v5.15.5-arch1, and the shell I am using which is the most up to date bash v5.1.12(1).

The simple answer is use :ha

:ha is short for :ha[rdcopy] and prints the text in nvim to the default printer.

If you want to print to other printers (not the default printer) and with other various custom print parameters then first you need to find out which printers you have available. Use this line in your .vimrc to do this

nnoremap <leader><leader><leader>p :r !echo; lpstat -p \| sed 's/printer //g' \| sed 's/is idle.  enabled since.*//g'; echo<cr>

Becasue I have \ assigned as my leader key I press \ three times followed by p and I get a list of available, enabled, printers.
Use your own key assignment, or just use mine.

The core shell command is lpstat -p. The sed shell commands strip the excess text around the printer names for clarity. If any of your available printers are not enabled then obviously this shell command will not poduce such a clean, printers only list output, becasue the text output from lpstat -p will be different.

  • r reads the result of the shell command into the currently open text file.
  • ! indicates the start of the shell command.
  • <cr> at the end executes everything without an extra manual carridge return inside nvim to make it happen.

My output in my currently open text file looks like this:

HP_LaserJet_2200_LOCAL_hpcups
HP_LaserJet_2200_INTERNET_hpcups
HP_LaserJet_2200_LOCAL_gutenprint
HP_LaserJet_2200_INTERNET_gutenprint
HP_Officejet_3830

If you have no printer driver installed you will have to install one first and make sure your printer is printing/ working. If you have a printer already attached to your machine and in use the above command will show it, if it is enabled.

To print the currently open file (which has to be saved first) using custom printer options, a line like this should be used in your .vimrc

nnoremap <leader><leader>pl :w<cr>:!lpoptions -d HP_LaserJet_2200_LOCAL_hpcups<cr>:!lp -n 1 -o media=a4 -o sides=two-sided-long-edge %<cr><cr>

You will need to save the file first or it won't print. Saving the file has to be done becasue the % near the end of the line is the way in which the current file is selected for printing from the shell within nvim.

  • :w<cr> saves the file.
  • ! indicates the start of the shell command.
  • -d HP_LaserJet_2200_LOCAL_hpcups is the destination printer and it's name. Choose your own printer from the list you have generated above.
  • -n 1 indicates one copy only.
  • -o media=a4 indicates the paper size.
  • -o sides=two-sided-long-edge indicates double sided printing and long edge selection. Other available options that can be used, which may be useful to you are, -o sides=one-sided, -o sides=two-sided-long-edge, -o sides=two-sided-short-edge
  • % indicates print the current file. Has to be saved to work.
  • <cr> at the end executes it without an extra manual carridge return inside nvim.

You could use other shortcuts in .vimrc too, for other printers, such as

" Print to HP_LaserJet_2200_INTERNET_hpcups
nnoremap <leader><leader><leader>pi :w<cr>:!lpoptions -d HP_LaserJet_2200_INTERNET_hpcups<cr>:!lp -n 1 -o media=a4 -o sides=two-sided-long-edge %<cr><cr>

" Print to hardcopy default printer. ":ha" also works 
nnoremap <leader><leader><leader>ph :hardcopy<cr><cr>
4
  • 1
    Thanks to @B-Layer for help with getting there ;) Thanks.
    – Kes
    Dec 9, 2021 at 13:50
  • Does anyone know if the current working file in nvim can be printed without first having to save it so that the shell can find it with the current file reference %?
    – Kes
    Dec 9, 2021 at 14:08
  • 2
    Using % just gives the filename, so an external program that reads it can only see what's saved on disk. If the program supports reading from stdin, you can do :%write !prog to write the contents to stdin of the program. Also, check out :hardcopy
    – D. Ben Knoble
    Dec 9, 2021 at 14:16
  • Thanks @D-Ben-Knoble i've updated the answer to reflect `:ha[rdcopy] as the most simple most obvious option
    – Kes
    Dec 9, 2021 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.