Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Similar to using markdown on Stack Overflow to create monospace output, how can I do that for my programs.

I tried lp filename.txt and was pleasantly please to find that it was able to use the local network printer and print 'as is'

The problem is the output is not mono-spaced.

Is there an quicker option than...

1) copy the text
2) paste into a text doc
3) format the document as mono
4) print it 

I would love to find some option like lp filename.txt -format=mono ;)

share|improve this question
1  
possible duplicate of syntax_highlight <source_file> | lpr –  Gilles Jul 19 '12 at 23:57
add comment

3 Answers

You can use the vim editor to pretty print arbitrary files vim can read. Use the following to create a PostScript file.

vim \
  -c 'hardcopy > output.ps' \
  -c quit <input_file>

If you want a PDF file, add && ps2pdf output.ps to the command line.

You can also write a small script that pastes the current clipboard content into a temporary file, runs vim on the file, prints the resulting file and deletes the temporary file.

share|improve this answer
add comment

The shortest path here is probably to use a2ps, it generates monospaced postscript by default. Depending on its setup, it will send the output directly to stdout or lp, override with -o. Also, a2ps recognizes several programming languages and pretty-prints them.

a2ps -o output.ps infile.text

Use --pro=color if color is available:

a2ps --pro=color -o output.ps infile.text

For HTML output I tend to use vim's 2html feature.

share|improve this answer
    
this looked great... but I don't have a2ps. Anyone know how to install? –  Michael Durrant Jul 19 '12 at 21:12
    
That depends on what operating system you want to install it to? –  Thor Jul 20 '12 at 1:20
    
sure its a mac - osx –  Michael Durrant Jul 20 '12 at 3:14
    
Googling a bit it seems that a2ps has some installation issues on osx. I do not have much experience with osx but enscript has been mentioned as an alternative. –  Thor Jul 20 '12 at 3:46
    
You can install a2ps using MacPorts (macports.org). I did not have any problem installing it (on Lion) –  Matteo Jul 20 '12 at 5:45
show 2 more comments
up vote 0 down vote accepted

a2ps was the answer. I installed it with brew:

brew install a2ps

Now I can a2ps myfilename and it works.

Unfortunately it comes out landscape and if I try to make it portrait it's squished over to the left and tiny, only up taking 50% of the page. [upate - found fix to this with parameter -1 (for number of pages to find on one sheet - the default was 2)

However as landscaped it worked and the code has the fixed format style I was looking for.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.