Vim Tips Wiki

Previous TipNext Tip

Tip: #533 - Page 1 of 123 in header of hardcopy

Created: August 18, 2003 16:14 Complexity: intermediate Author: Mikolaj Machowski Version: 6.0 Karma: 18/9 Imported from: Tip#533

To get this to work you have to know how many lines per page :hardcopy

is normally making. Open empty document and in Normal mode "100o". Then

make ":%s/^/\=line('.')". Now ":hardcopy >". Open in

PostScript viewer. You will see how many lines per page Vim is printing.

In my case this is 73.

Now set 'printheader' option:

set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}

Explanations of % items in option are here


"I forgot about case when total number of lines is multiplication of "lines per page. Here is an improved version. Number of lines per page "was put in variable - you have to change it only once :)

function! PH_Multiple()

let lpp = "73" " lpp - lines per page 
let modulo = line('$') % lpp 
if modulo != 0 
return ( line('$') / lpp ) + 1 
return line('$') / lpp 

endfunction set printheader =%<%f%h%m%=Page\ %N\ of\ %{PH_Multiple()}

Mikolaj , August 19, 2003 4:36

Some nice additions from comp.editors:

Peppe Guldberg:

set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+(line('$')%73!=0)}

NOTE: smart use of condition


:set printheader=%<%f%h%m%=Page\ %N\ of\ %{(line('$')-1)/73+1} 

IMO this is best of all: oneliner and number of lines appears only once - easy to correct.


Mikolaj , August 20, 2003 8:20

Clever; however there are multiple settings (see e.g. 'printfont' & 'printoptions') which can change the number of lines per page. Also, if long lines wrap when printed (the default), then the number of lines in the buffer may not match the number of lines printed.

Given these limitations, it would be great if the 'printheader' header option was extended (Vim 7?) to handle this in the Vim code itself. Maybe %T = Total number of pages to be printed.

Anonymous , November 22, 2004 4:57