I'm using vim from a terminal emulator, and I'd like to be able to print using KDE's native print dialogue. This would let me select options such as printer, collating, number of copies and pages per piece of paper.

(There is an older tip here explaining how to link vim with kprinter, but this does not work, and I suspect kprinter has been deprecated.)

I know that I can "print" to a postscript file with :ha > foo.ps (or :hardcopy), then open this up with a GUI viewer, such as okular, but is there a way to get directly to the print dialogue from vim? (Also, I assume it's impossible, but can I open up a print preview window too?)

  • @Carpetsmoker I don't totally understand my link above, but I thought perhaps that it was piping the "hardcopy" postscript file to KDE's print interface. Hence, I thought that my question wasn't so much about Vim being compatible with KDE, but more about finding the right KDE component to receive the hardcopy. – Sparhawk Mar 9 '15 at 2:23
  • kprinter was removed from the KDE4 source, and someone published a clone: github.com/credativ/kprinter4 – muru Mar 9 '15 at 13:42
  • @muru Yes, I think that's the same one that John O'M. mentioned in his answer below. – Sparhawk Mar 9 '15 at 14:19
  • Ah, I didn't see that answer. I came across this in the review queue. – muru Mar 9 '15 at 14:24

The loss of kprinter in the move to kde4 was one of the reasons I stuck with kde3 for as long as I could.

After some searching, I found this "3rd party" replacement: http://kde-apps.org/content/show.php/KPrinter4?content=163537, https://github.com/credativ/kprinter4.

It looks like you have to compile it yourself (Its not included in distros since it isn't core kde). The result works like the old one that came with kde3, but in the kde4 environment. It is used the same way, except the executable is called kprinter4.

Note: Some non-KDE alternatives are presented at the bottom.

I tried following the instructions at the vimwiki link you posted to use it, but I found those difficult to follow (too many escaped characters), so I based my vimrc on the function method found in :help pexpr-option

Building the executable

This bit isn't about Vim, but is a prereq to the solution. I'm writing this with limited assumptions about your (or whoever else finds this answer useful) knowledge of building software.

  1. Download the "latest" source to kprinter4 from the link above. I used http://kde-apps.org/content/download.php?content=163537&id=1&tan=32050597

  2. Extract into some directory: tar xf 163537-kprinter4-12.tar.xz (filename may be different for a newer version)

  3. Cd into the source directory: cd kprinter4-12 (directory may be different in a newer version)

  4. Create a build directory and change into it: mkdir build && cd build

  5. Run cmake. I used the options given at the kde-apps page, but changed the INSTALL_PREFIX such that I don't need to be administrator to install (but the install is only for me):


    The trailing .. is important. The INSTALL_PREFIX can be whatever you want, but it will be used in your vimrc (see below). You'll at least want to replace my username with your own.

    If the command fails, it will probably tell you that you are missing libraries (for me it was missing LIBSPECTRE). Install whatever is missing and loop until cmake is successful. (e.g. in my case: apt-get install libspectre-dev) Depending on your distro and setup, different commands and package names may be needed.

  6. Run make: make

  7. Install: make install

Configure Vim

I'm using the install path from my sample isntructions above (which include my home directory). Change the paths as appropriate for your system (e.g. replacing my username with yours, or whatever you did for the INSTALL_PREFIX in the above section)

In my vimrc:

function PrintFile(fname)
   call system("/home/john/opt/kprinter4/bin/kprinter4 " . a:fname)
   call delete(a:fname)
   return v:shell_error
set printexpr=PrintFile(v:fname_in)

This creates a function that calls your newly built kprinter4 program and then cleans up the file whenever you run :hardcopy.

After that, run :hardcopy on a document and get a pretty printer selector:

Screenshot of kprinter4 on top of a vim session that had just run :hardcopy

Non-KDE Alternatives

Some Linux distributions provide gpr and/or gtklp (thanks @derobert and @Sparhawk for pointing out and testing these in the comments/chat)


Example for using gpr (presently available on Ubuntu but not Arch). Note that this errors on my system due to a missing PPD file or something. Your mileage may vary.

function PrintFile(fname)
   call system("gpr " . a:fname)
   call delete(a:fname)
   return v:shell_error
set printexpr=PrintFile(v:fname_in)

Screenshot of gpr after :hardcopy


Example for using gtklp (Available on both Ubuntu and Arch)

function PrintFile(fname)
   call system("gtklp " . a:fname)
   call delete(a:fname)
   return v:shell_error
set printexpr=PrintFile(v:fname_in)

Screenshot of gtklp after :hardcopy

Alternative gtklp

At least one person has had an issue where gtklp on Arch Linux doesn't accept files specified on the command line. Below is an alternative method that works in that case (or in any case) that passes the contents of the file instead.

function PrintFile(fname)
   call system("cat " . a:fname . " | gtklp")
   call delete(a:fname)
   return v:shell_error
set printexpr=PrintFile(v:fname_in)

Screenshot of gtklp after :hardcopy but with a pipe

Note that with this method, gtklp doesn't even have a files tab displayed, but it does in fact print the document when print is selected.

  • Seems like a reasonable solution (+1), but is there no solution with the core (or semi-core) KDE packages? I read that kprinter has been replaced by a qt/kde-specific program, but presumably my gtk apps use another frontend. – Sparhawk Mar 9 '15 at 2:43
  • I think the answer is that there isn't a native way. What I had read was that the kprinter program was replaced by the built-in qt printer dialog (i.e. a function call that can be made from within a program that uses qt). That transition resulted in the removal of the stand-alone program kprinter. There is some info and discussion at the following link (which then links to what I posted in the answer) liquidat.wordpress.com/2014/02/18/kprinter-available-for-kde-4 – John O'M. Mar 9 '15 at 2:49
  • We linked to the same thing! :) Okay, I'll leave the question up for a little while anyway, and I'll accept your answer if I don't hear anything better. – Sparhawk Mar 9 '15 at 3:02
  • @Sparhawk Hah, indeed. I missed the link when I first read your comment and posted mine. Anyway, I've just added build and vimrc instructions to make my answer more complete (and less of a link-only answer). That still doesn't help you find a built-in solution, but hopefully it helps others looking for this. – John O'M. Mar 9 '15 at 3:24
  • No worries. Thanks for the instructions; they are so clear! I'm using Arch, so I'd do it via a PKGBUILD, which has it's own… peculiarities, but your instructions look very helpful. BTW, are you using Ubuntu? Do you know if there's a way to pipe hardcopy to its native print dialogue? If so, I wonder if I could co-opt a different DE's print frontend… (which I was implying with my first comment). – Sparhawk Mar 9 '15 at 3:28

Just for the sake of completeness: If kprinter4 doesn't work, you can also make a parallel installation of (relevant components of) Trinity KDE (TDE) desktop, which is available in some distros such as Debian or Arch. Then you can use good old kprinter instead of kprinter4:

set popt=duplex:off 
set printexpr=PrintFile(v:fname_in)
function PrintFile(fname)
  call system("/opt/trinity/bin/kprinter " . a:fname)
  call delete(a:fname)
  return v:shell_error

vim's :ha wasn't printing UTF-8 characters, and I wanted an built-in preview anyway. In the end, I just decided to convert to a pdf and open that instead.

command Printpreview w! /tmp/vim_print.txt | call system("unoconv /tmp/vim_print.txt && xdg-open /tmp/vim_print.pdf")

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.