Dan Bechard
Dan Bechard

Reputation: 5291

Vim different color scheme when printing

Is there a way in _vimrc to set a different colorscheme to be used when printing files?

I like a dark background light text scheme on screen, but obviously that doesn't translate well to paper.

Edit: I can change the scheme manually before printing, then change it back after and it works fine. Just curious if there's a way to tell Vim to always use a specific scheme while printing.

This is what :hardcopy outputs: enter image description here

Upvotes: 5

Views: 2084

Answers (4)

thebluebeagle
thebluebeagle

Reputation: 11

Using :TOhtml works quite well. It creates a html file with the same colour scheme being used.

I like the onehalf (link to github download instructions for vim) colour schemes because it it has a simple light and dark scheme. The syntax highlighting is the same between both schemes, besides white and black text which is switched.

So you can have a dark scheme on your computer, and then still print your code out and retain the general syntax highlighting which I find really useful.

this is what I do:

  1. open the code I want to print
  2. change to light scheme if not already in it by :colorscheme :onehalflight
  3. run :TOhmtl which also opens the html file in vim
  4. go to new html file, and save it to desired location example, to save as text.html to downloads: :w ~/Downloads/text.html
  5. open saved html file with your favourite browser, and print or do whatever you want, this is nice because it allows you to see how many pages you are going to print before you break your printer!

Upvotes: 1

benjifisher
benjifisher

Reputation: 5122

How about something like

:command Hardcopy let colors_save = g:colors_name <Bar> colorscheme default <Bar> hardcopy <Bar> execute 'colorscheme' colors_save

Maybe throw in the 'bg' option. If you care about local variables, make it a function:

command Hardcopy call Hardcopy()
function! Hardcopy()
  let colors_save = g:colors_name
  colorscheme default
  hardcopy
  execute 'colorscheme' colors_save
endfun

Upvotes: 6

Habi
Habi

Reputation: 3300

The Vim plugin "Printer Dialog" allows to configure printing parameters, one of them is the colorscheme to be used for printing.

After installing and configuring "Printer Dialog" press \pd in the Vim window you want to print. The following "dialog" will open:

enter image description here

Beneath other things the syntax-highlighting for printing can be selected. See :help printer-dialog for further details. The variable g:prd_syntaxList defines the syntax-highlightings that can be selected. Default is

g:prd_syntaxList = "no,current,default,print_bw,zellner" 

See :help prd_syntaxList for details on how to setup this feature.

Upvotes: 3

Nikita Kouevda
Nikita Kouevda

Reputation: 5756

:hardcopy will always print with a white background. From :help hardcopy:

The current highlighting colors are used in the printout, with the following
considerations:
1) The normal background is always rendered as white (i.e. blank paper).
2) White text or the default foreground is rendered as black, so that it shows
   up!
3) If 'background' is "dark", then the colours are darkened to compensate for
   the fact that otherwise they would be too bright to show up clearly on
   white paper.

However, I'm not sure how exactly "[...] the colours are darkened to compensate [...]" works, so you may still want to go with @benjifisher's solution.

Alternatively, you could use :TOhtml to get an identical representation (definitely with a different colorscheme in this case), and then print that out in some other way. See :help TOhtml for relevant options, e.g. g:html_number_lines.

Upvotes: 1

Related Questions