15

I would have thought this is rather simple, but I don't get it done: I use gVim and would like to get the text as a PDF file. In other applications like Firefox the print dialog shows me available printers and I can choose to print directly to a PDF file. However, in Vim there is no such dialog and the file is just sent to the standard printer of the system.

I tried the following:

  • I'm not able to make the "print-to-pdf" thing my standard way of printing via the printer window of Ubuntu.
  • :ha > file converts my file to a .ps file. That's nice, but .pdf would be nicer...
0

2 Answers 2

27

Vim will not show you the print dialog box. Instead, you can print to a PostScript file, open it in a PostScript viewer and print from there.

To print to a PostScript file from Vim:

:hardcopy > myfile.ps

You can also convert PostScript to PDF using ps2pdf:

ps2pdf myfile.ps
7
  • 1
    The first proposal means just changing the file extension... The second one might work in a mapping or something like that, I will have to do some more research to get that done... Thanks so far...
    – caligula
    Commented Dec 4, 2015 at 18:13
  • 1
    It is as easy as changing an extension ;)
    – Ron
    Commented Dec 4, 2015 at 18:15
  • 1
    This is awesome, definitely didn't know this was a thing! Can you get multiple files into a single document? Commented Mar 30, 2017 at 17:20
  • 2
    (cygwin user) :ha > file.pdf did not create a valid pdf file. Combining it with ps2dpf did work but gives me white background. I'm happiest with :TOhtml file.html and then printing this html file to pdf
    – lucidbrot
    Commented May 10, 2017 at 6:35
  • I used a variation of this command today: :1,50 hardcopy, it didn't create a file, just printed from lines 1 to 50.
    – DrBeco
    Commented Mar 8, 2019 at 21:39
15

Building on what others have already stated:

You can use the following single-line Vim command to create a PDF file:

:hardcopy > %.ps | !ps2pdf %.ps && rm %.ps

Note:

  • The % is shorthand for the current filename, so HelloWorld.C will print to HelloWorld.C.pdf
  • If you want to also retain the intermediate .ps file, simply omit the && rm %.ps, obtaining: :hardcopy > %.ps | !ps2pdf %.ps

Additionally, to change the rendered font, set the printfont before executing the hardcopy command. For example, to select Courier 8:

:set printfont=Courier:h8

Putting it all together, I decided to add the following to my .vimrc file so that I can simply execute the :HardcopyPdf command. This command can also operate on a selected range within a file:

" Select the font for the hardcopy
set printfont=Courier:h8
command! -range=% HardcopyPdf <line1>,<line2> hardcopy > %.ps | !ps2pdf %.ps && rm %.ps && echo 'Created: %.pdf'
2
  • That is very neat Mathias, thanks. The range thing didn't work for me. I highlighted a range (using v) and the command acted on the whole file and ignored the range.
    – user587469
    Commented May 11, 2020 at 16:07
  • Do fonts other than Courier ever work? Commented Apr 7, 2023 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .