Vim different color scheme when printing - vim

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:

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

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:
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.

: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.

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:
open the code I want to print
change to light scheme if not already in it by :colorscheme :onehalflight
run :TOhmtl which also opens the html file in vim
go to new html file, and save it to desired location
example, to save as text.html to downloads: :w ~/Downloads/text.html
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!

Related

How to "highlight" using text in Vim?

These three lines of Vim script "highlights" all instances of TODO in my source code by placing [[[ and ]]] around it.
:set t_Co=1
:highlight Braces start=[[[ stop=]]]
:call matchadd("Braces", "TODO")
Unfortunately, this only works in "normal terminals". This is why I have to set t_Co to 1.
Can something similar be achieved in a color terminal or in gui? (Or in NeoVim?). This could be an extremely useful way of augmenting your code with meta information.
Please keep in mind that Vim is a text editor; these display the file contents mostly as-is. In order to augment your code with meta information, Vim offers:
(syntax) highlighting: colors and text attributes like bold or italic
concealment: hide or condense matches to nothing / a single character
signs (shown in a column left of the window)
Your approach is a hack that misuses the definition of raw terminal codes; these are meant to be invisible control sequences interpreted by the terminal, but you send visible text.
As you've found out at :help highlight-args:
There are three types of terminals for highlighting:
term a normal terminal (vt100, xterm)
cterm a color terminal (MS-DOS console, color-xterm, these have the "Co"
termcap entry)
gui the GUI
The start= and end= arguments (that your hack relies on) are only supported for "normal terminals", not for cterm and gui. That's why you have to :set t_Co=1 (i.e. force a non-color terminal) for this to work.
Because of these downsides (and more: redrawing problems, vertical navigation that's off), I would recommend to drop this, and use one of the "approved" methods listed above. There are so many Vim users, and they seem to be fine with them as well.

GVIM : in /root/.vimrc i have ":colorscheme desert" which worked, but where would i have found this value if someone hadn't told me?

So in GVIM i changed the color-scheme to
desert
but i never understood how to find this line
:colorscheme desert
so i could place it in
/root/.vimrc
file.
where would i have found out about " :colorscheme desert " ?
You could read the help (it is quite long and very detailed)
:h
Opens up to the main help for vim. Under getting started you will see the following. You might have to scroll down a bit (Use Control+f to page down)
usr_toc.txt Table Of Contents
Getting Started
usr_01.txt About the manuals
usr_02.txt The first steps in Vim
usr_03.txt Moving around
usr_04.txt Making small changes
usr_05.txt Set your settings
usr_06.txt Using syntax highlighting
usr_07.txt Editing more than one file
usr_08.txt Splitting windows
usr_09.txt Using the GUI
usr_10.txt Making big changes
usr_11.txt Recovering from a crash
usr_12.txt Clever tricks
It seems the that usr_06.txt is related to syntax highlighting so that might be worth reading. So type :h usr_06.txt or <c-]> (Control+]) to jump to that file when your cursor is on top of it.
Once you are in there you can look at the table of contents again.
06.1 Switching it on
06.2 No or wrong colors?
06.3 Different colors
06.4 With colors or without colors
06.5 Printing with colors
06.6 Further reading
Different colors seems to be what you want. So type :h 06.3 (or <c-]>) and the first paragraph talks about changing the colorscheme.
Control+] jumps to a tag and is talked about in :h tagsrch.txt (And on the line "Jump to a subject" immediately as you open :h)
The help have an auto completion feature.
So you can type, :help color followed by Ctrl+d. You won't necessary find the good entry right of the bat but it should put you on the right track. It would have shown you a colorscheme option.
An alternative is to use :helpgrep. :helpgrep color should give you a list of help file wich contains the string color.
On Debian Linux, it's part of the the vim-runtime package and sits in /usr/share/vim/vim73/colors/desert.vim. You can list contents of this direcory for more options. I don't think they have hard-coded color schemes in vim itself, so vim help will not list them for you.
I suppose like most other vim plugins, customized color schemes can also be put in ~/.vim directory.

Linux console text editor - writing with colors

Is there a way to write a text in some console text editor under linux so that I can define the color of the text displayed based on system console colors defined (*color0.. *color15)?
For example I what the part of the text to be displayed as green, and part as cyan while I am typing in the text editor.
The Txtfmt plugin for Vim provides what you're looking for. In addition to colors, you can also apply formatting to the text: e.g., bold, underline, italic, undercurl, etc..., as well as any combination thereof.
Txtfmt
Note: The errors mentioned by the previous poster are most likely caused by failure to enable filetype plugins in his vimrc. For some reason, the normally desired setting is not the default in many Vim installations. Adding the following line should prevent the errors:
filetype plugin on
You can use these codes: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
Basic Syntax:
echo '/033[ the color code m' (no spaces)
To reset
echo '\033[0;0m'
To make a literal color:
In vi you can use Ctrl-V then Esc instead of echo '\033
in nano you can use Meta-V then Esc instead of echo '\033
Meta-v can either be Esc then v or Alt-v depending on your setup.
The only thing I have found so far is a VIM plugin called TxtFmt available at http://www.vim.org/scripts/script.php?script_id=2208
The only problem is that I cannot make it work - On :MakeTextPage I get bunch of errors like "Unknown function: Txtfmt_GetTokStr" and many others.

:hardcopy a file in vim with full colorscheme

I'd like to print some files I edited in MacVIM using full color, including the dark background.
Of course I won't send it to printer, I just want to save it in PDF.
:hardcopy removes the background and applies an ugly scheme.
Is it possible?
Rather use :TOhtml (this is a plugin that is usually included in the default Vim runtimes): type Q to enter Ex mode, and then:
let html_number_lines=1
let html_no_pre=0
TOhtml
w
visual
Quoting 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.
So it would appear the answer is, no you can't from :hardcopy.
It also gives this note:
Note: If you have problems printing with :hardcopy, an alternative is to use
:TOhtml and print the resulting html file from a browser.
If that's an option, that's going to be the easiest way to do it.

vimrc make comments italic

How do I change the ~/.vimrc to have the comments in my code italicized?
In my ~/.vimrc file I have:
highlight Comment ctermfg=blue
that makes the comments blue. What do I need to do differently to make them italic?
First and foremost, you should check if you terminal is capable of displaying text in italics. In your terminal type (-e flag makes sure escape codes are interpreted)
echo -e "\e[3m foo \e[23m"
If you see foo then okay, otherwise you need to change terminal (Gnome Terminal and Konsole are good choices).
Then you should help Vim to recognise the kind of terminal you are using, put in you ~/.bashrc:
export TERM="xterm-256color"
Now you can try and see if this is enough, open a new file vim foo.html with the following content
<i>foo</i>
Do you see foo in italic? If no then you need to go a little further, right now Vim doesn't know the escape codes to switch to italic mode, you need to tell it (this is the hardest part, it took me a few years to figure that out).
Put the following two lines in your ~/.vimrc
set t_ZH=^[[3m
set t_ZR=^[[23m
These are the same escape codes we used before in the terminal, be aware that ^[ are not literal characters but represent the escape character, you can insert it in insert mode with CTRL-V followed by ESC (see :help i_CTRL-V)
Now reopen the file we created before foo.html and you should see foo in italic; if you don't then I can't help you any more. Otherwise you are almost done; there is one last step.
Put in you ~/.vimrc file
highlight Comment cterm=italic
after loading any colorscheme.
highlight Comment cterm=italic gui=italic
You'll need a font with an italic set and a terminal capable of displaying italics. Also, if you're using a color scheme other than the default, the above line should come after the color scheme is loaded in your ~/.vimrc so that the color scheme doesn't override it.
The cterm makes it work in the terminal and the gui is for graphical Vim clients.
In my case I had to put this in my vimrc file:
let &t_ZH="\e[3m"
let &t_ZR="\e[23m"
highlight Comment cterm=italic
Notice it is not the same as:
set t_ZH=^[[3m
set t_ZR=^[[23m
highlight Comment cterm=italic
The former worked for me, while the latter didn't.
for GUI environments like gvim, a simple
highlight Comment gui=italic
does it.
michaelmichael's answer should solve it for most cases. But, just in case you need this for a font in gvim that doesn't have italics (but oblique or something instead), you can try something like this in ~/.gvimrc
highlight Comment font=Bitstream_Vera_Sans_Mono_Oblique:h14
where h14 is the font size. This font should have the same cell size as your normal font though, so don't use an altogether different font.
Because I'm using the Solarized colorscheme, I had to edit .vim/colors/solarized.vim as recommended in Solarized #120 to replace lines 137-157 with the following:
if has("gui_running") || ( has("unix") && system("tput sitm") == "\033[3m" )
let s:terminal_italic=1
else
let s:terminal_italic=0
endif
That was in addition following the instructions in this Gist and adding these two lines to my .vimrc, using Ctrl-vEsc to enter ^[:
set t_ZH=^[[3m
set t_ZR=^[[23m
(Thanks to Gabriele Lana for the tip to add those lines to my .vimrc.)

Resources