Wikia

Vim Tips Wiki

Changes: Page 1 of 123 in header of hardcopy

Edit

Back to page

(Comments)
(Remove html character entities)
 
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
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 > nu.ps". Open nu.ps in PostScript viewer. You will see how many lines per page Vim is printing.
+
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 > nu.ps". Open nu.ps in PostScript viewer. You will see how many lines per page Vim is printing.
   
 
In my case this is 73. Now set the 'printheader' option:
 
In my case this is 73. Now set the 'printheader' option:
   
:set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}
+
:set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}
   
 
<pre>
 
<pre>
Line 40: Line 40:
 
endif
 
endif
 
endfunction
 
endfunction
set printheader =%&lt;%f%h%m%=Page\ %N\ of\ %{PH_Multiple()}
+
set printheader =%<%f%h%m%=Page\ %N\ of\ %{PH_Multiple()}
 
</pre>
 
</pre>
   
Line 46: Line 46:
 
Some nice additions from comp.editors:
 
Some nice additions from comp.editors:
   
:set printheader=%&lt;%f%h%m%=Page\ %N\ of\ %{line('$')/73+(line('$')%73!=0)}
+
:set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+(line('$')%73!=0)}
   
:set printheader=%&lt;%f%h%m%=Page\ %N\ of\ %{(line('$')-1)/73+1}
+
:set printheader=%<%f%h%m%=Page\ %N\ of\ %{(line('$')-1)/73+1}
   
 
----
 
----
Clever; however there are multiple settings (see e.g. 'printfont' &amp; '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.
+
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.
   
 
----
 
----
 
A simpler way to do the page count arithmetic:
 
A simpler way to do the page count arithmetic:
   
:set printheader=%&lt;%f%h%m=Page\ %N\ of %{(line('$')+72)/73}
+
:set printheader=%<%f%h%m=Page\ %N\ of %{(line('$')+72)/73}

Latest revision as of 08:53, September 29, 2008

Tip 533 Printable Monobook Previous Next

created August 18, 2003 · complexity intermediate · author Mikolaj Machowski · version 6.0


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 > nu.ps". Open nu.ps in PostScript viewer. You will see how many lines per page Vim is printing.

In my case this is 73. Now set the 'printheader' option:

:set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}
line('$') - number of lines in buffer
73 - number of lines per page
+1 - Vim uses integer math and everything rounds down.

ReferencesEdit

CommentsEdit

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
  else
    return line('$') / lpp
  endif
endfunction
set printheader =%<%f%h%m%=Page\ %N\ of\ %{PH_Multiple()}

Some nice additions from comp.editors:

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

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.


A simpler way to do the page count arithmetic:

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

Around Wikia's network

Random Wiki