Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I execute the following commands from within a vim session that's visiting a file of code, the resulting hardcopy is syntax highlighted in color:

:syntax on
:hardcopy >/path/to/

But if I try the same sequence of commands from the command line (Unix shell), the resulting hardcopy is not color-highlighted (I get just black text, sometimes bold, or underlined, etc., on a white background):

% vim -c ':syntax on' -c ':hardcopy >/path/to/' -c quit /path/to/original

Any ideas on how to fix this problem?

share|improve this question

I suspect that what may be at issue here is Vim's colorization is a kind of hack which is integrated with the display code. Note that, for instance, when you're jumping around in a large file, sometimes the coloring will be wrong, because Vim tries not to process the entire file in applying its hierarchical "region" pattern matches. Also, if your terminal doesn't support colors, then Vim will not print color or generate colorized HTML, even though those destinations have nothing to do with the terminal.

I once tried to turn Vim into a back end for doing on-the-fly syntax highlighting under CGIT, for viewing files in a GIT repository. I tried numerous hacks to try to get Vim to produce output in batch mode, but in the end I gave up.

But I stopped short of trying to use a utility like expect. Scripting Vim with expect is something that might work, provided you also lie to it, and tell it that it's talking to a color Xterm.

share|improve this answer

I found out that the environment variable TERM has an impact on the PS output colors used.

You can maybe try this:

TERM=xterm-256color vim '+hardcopy >' +q src_file
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .