问题
I have occasion to need to print code (Horrors!!! ;) ), and i was wondering what editor or tool would i use to print that code out with proper formatting and syntax highlighting? If it's important, it will be c# code but ideally the tool will work for as many languages as possible. Can Notepad++ or something handle this?
回答1:
Yes, Notepad++ can certainly print code with syntax highlighting.
Colour printing would obviously be preferable, but on the occasions when I've printed in black and white, the subtle differences in colour [rendered as shades of grey, of course] can be difficult to distinguish.
However, I think a little customisation of the colour schemes should make this less of a problem.
回答2:
You can use Vim! It's probably installed already if you're on modern Linux/MacOS and an easy install if not.
:syntax
will turn syntax highlighting on and :hardcopy
will print it. There's syntax highlighting definitions for many languages out there. The default look is usually optimised for screen display, but you can fix that.
Simply open the file on command line with vim <filename>
, type :syntax on<ENTER>
, then :hardcopy<ENTER>
to print it. Quit Vim with :q!<ENTER>
.
There's also the :TOhtml
command which will open the current selection as HTML in a new Vim window. Capture the entire document with :%y<ENTER>
followed by :TOhtml<ENTER>
to open it.
回答3:
Use vim. Its the easiest method to do it in my experience by far, that is, once you know how.
Vim comes pre-installed on Macs, btw. And I know how you special people like colors, so I'm going to make this impatientbusinessman-proof for the benefit of all.
1.) open file
vim filename.m
2.) enable syntax coloration (mine did not have enabled by default)
:syntax on
3.) print
:hardcopy
Vim will choose your system's default printer without asking you so make sure you set that up first.
4.) exit the program (this is actually not a given)
:q
OR
Use Aptana. Aptana is pretty sweet, its free and it has great ftp functionality.
回答4:
http://pygments.org/ is one option. It supports a ton of languages, and since it's written as a python library, you can script the conversion process however you want.
回答5:
Visual Studio will, and allows you have a completely separate configuration for printing.
回答6:
I recently compared the 2 solutions already mentioned : vim
& pygments
. They both give great results, but there is how you can practically use them quickly:
pygments
does not provide direct export to PDF. Hence, the simplest solution I found was to export to HTML and then usewkhtmltopdf
. You can combine both operations using the following bash script:
src2pdf () {
local noext="${1%.*}"
pygmentize -O full -o "$noext.html" "$1"
# enabling line wrapping in <pre> blocks
perl -i -wpe '/<style.*>$/&&($_.="pre{white-space:pre-wrap;}\n")' "$noext.html"
wkhtmltopdf "$noext.html" "$noext.pdf"
rm "$noext.html"
}
- for
vim
, it's as simple as this:TERM=xterm-256color vim '+hardcopy >out.ps' +q code.src
I found out that the$TERM
environment variable can affect the output colors, so I prefer to set it explicitly. And finally, you may need to tweak your .vimrc a little:
set printfont=:h9
set printoptions=number:y,left:5pc
回答7:
The tool called enscript
is very much the tool for doing this. It is very powerful, is not tied to an editor nor a language and you can create PDF's with syntax highlighting.
The documentation pretty much says it all.
enscript man page
回答8:
Under unix you might want to try a2ps. It is flexible and produces nice results.
回答9:
I while ago I created a household python script that wraps pygments into a small console utility. It works with any language supported by pygments.
Also if you happen to use eclipse, you could simply copy the selected text in the editor and then paste it in a RTF-aware editor like MS Word - it will preserve all the colors, fonts and formatting.
回答10:
If you have problems with Visual Studio 2012 concerning the highlighted printing an handeling the described problem:
- Download and install this Power Tool which implements the color printing, besides some other features and bug fixes. Works for me!
回答11:
Solution For Bash Shell
Add this line to
~/.bashrc
if you are using UBUNTU
or, to~/.bash_profile
if you are using MAC
If that file does not exists, create it.alias lprc='vim -me -c ":syntax on" -c ":hardcopy" -c ":q"'
source ~/.bashrc
orsource ~/.bash_profile
To print colored hello.py just do this:
lprc hello.py
instead oflpr hello.py
来源:https://stackoverflow.com/questions/1656914/printing-code-with-syntax-highlighting