Printing in vim: Using programs other than the default lpr

May 25th, 2007 Felix Leong Posted in vim | Hits: 110859 | 8 Comments »

Printing in vim is as easy as entering the :hardcopy (or its shorthand equivalent :ha), that command is equivalent to creating a temporary Postscript file and passing it through lpr.

But what if you want to modify the printing behaviour of vim? Say, printing 2 pages on a page using the a2ps command?* Luckily that can be easily overridden using the :printexpr option. Place the following code in your ~/.vimrc

(Adapted from 2. Print options, from VIM Reference Manual, Printing)

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

Basically all you need to do is to replace the argument in the call system() line and substitute it with your printing command of your choice

* Shameless plug: Save some trees, think before you print :)

UPDATE: Explanation on the vim script

Since we are at it, I think it’s a good idea to explain the script (thanks to mysurface for brining this up) (*Note: some basic knowledge of programming is required)

Let’s go this line by line, shall we? set printexpr=PrintFile(v:fname_in) Basically sets the printexpr to redirect printing requests through the PrintFile function that we’ll be defining later on. According to the VIM Reference Manual in the printing section,
“The file name to be printed is in v:fname_in”. So we will be using this variable to be passed to PrintFile as its function argument. (*note: v:fname_in is a VIM pre-defined variable meaning the name of the input file)

And in the second line, we define the PrintFile function (function PrintFile(fname)), which will accept a parameter that we define as fname. And in the third line, we make a system call to which will look something like “a2ps [file name]“. The parameter in system function call, "a2ps " . a:fname, means concatenating the strings (the period character) “a2ps ” (note the space) and the string being passed into the PrintFile parameter variable (which you’ll notice fname is the name of the variable that we have defined previously, and the a: is needed to let vim know that it’s the functional argument fname that we had defined ourselves)

Then lastly, once we are done with it, we delete the file (call delete(a:fname) and return the shell error code (return v:shell_error) so that vim can notify us what error had occurred, if any. Finally we end our function declaration with end func.


8 Responses to “Printing in vim: Using programs other than the default lpr”

  1. It is an amazing example showing “vim script” :)

    But, would you mind further explain what is the meaning of v: and a: and why in
    First line variable name is fname_in and second line become fname ?

  2. I have added the explanation to the vim script. This is the first time I explained actual programming logic, hope it’s simple enough to understand :).

  3. Nice, thats great! Vim is a niche editor, you define your own functionality and adapt to it.

  4. Its really an amazing exmaple of using vim script

  5. Regards from Magheramorne ;)

  6. Appreciate it from Altmore ;)

  7. I got this website from my buddy who informed me on the topic of this website and
    at the moment this time I am visiting this website and reading very informative content at this place.

  8. yamadori co uk 2012 03 12 british shohin association annual show 2012 photos and movie shohin acer

Leave a Reply