Vim Tips Wiki
No edit summary
 
(Remove html character entities)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=533
 
|id=533
  +
|previous=531
|title=Page 1 of 123 in header of
 
  +
|next=534
|created=August 18, 2003 16:14
+
|created=August 18, 2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Mikolaj Machowski
 
|author=Mikolaj Machowski
 
|version=6.0
 
|version=6.0
 
|rating=18/9
 
|rating=18/9
  +
|category1=
|text=
 
  +
|category2=
To get this to work you have to know how many lines per page :hardcopy
 
 
}}
  +
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:
is normally making. Open empty document and in Normal mode "100o". Then
 
   
 
:set printheader=%<%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}
make ":%s/^/\=line('.')". Now ":hardcopy &gt; nu.ps". Open nu.ps in
 
   
  +
<pre>
PostScript viewer. You will see how many lines per page Vim is printing.
 
 
line('$') - number of lines in buffer
 
73 - number of lines per page
 
+1 - Vim uses integer math and everything rounds down.
  +
</pre>
   
  +
==References==
  +
*{{help|'statusline'}}
   
 
==Comments==
  +
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.
   
  +
<pre>
In my case this is 73.
 
 
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()}
  +
</pre>
   
 
----
Now set 'printheader' option:
 
 
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}
   
:set printheader=%&lt;%f%h%m%=Page\ %N\ of\ %{line('$')/73+1}
 
 
 
 
Explanations of % items in option are here |'statusline'|.
 
 
line('$') - number of lines in buffer
 
 
73 - number of lines per page
 
 
+1 - Vim don't know floating point math and everything rounds down.
 
 
 
}}
 
 
== Comments ==
 
"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 =%&lt;%f%h%m%=Page\ %N\ of\ %{PH_Multiple()}
 
 
 
Mikolaj
 
, August 19, 2003 4:36
 
 
----
 
----
 
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.
Some nice additions from comp.editors:
 
   
Peppe Guldberg:
 
 
:set printheader=%&lt;%f%h%m%=Page\ %N\ of\ %{line('$')/73+(line('$')%73!=0)}
 
 
NOTE: smart use of condition
 
 
Anthony:
 
 
:set printheader=%&lt;%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.
 
 
Thanks!
 
 
Mikolaj
 
, August 20, 2003 8:20
 
 
----
 
----
  +
A simpler way to do the page count arithmetic:
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.
 
   
  +
:set printheader=%<%f%h%m=Page\ %N\ of %{(line('$')+72)/73}
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
 
----
 
<!-- parsed by vimtips.py in 0.532027 seconds-->
 

Latest revision as of 08:53, 29 September 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.

References[]

Comments[]

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}