Vi and Vim Asked on November 17, 2021
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?)
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")
Answered by Sparhawk on November 17, 2021
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
endfunc
Answered by LaTechneuse on November 17, 2021
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
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.
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
Extract into some directory: tar xf 163537-kprinter4-12.tar.xz
(filename may be different for a newer version)
Cd into the source directory: cd kprinter4-12
(directory may be different in a newer version)
Create a build directory and change into it: mkdir build && cd build
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):
cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/john/opt/kprinter4 ..
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.
Run make: make
Install: make install
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
endfunction
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:
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
endfunction
set printexpr=PrintFile(v:fname_in)
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
endfunction
set printexpr=PrintFile(v:fname_in)
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
endfunction
set printexpr=PrintFile(v:fname_in)
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.
Answered by John O'M. on November 17, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP