5

Is there a solution to printing unicode characters from gvim?

See this:

enter image description here

Results in this print result

enter image description here

(Using a Linux setup and Vim 8.0.)

4
  • 2
    It looks like your printing setup expects a different character set, or doesn't support unicode at all Commented Apr 11, 2017 at 19:59
  • What do you get from :set enc? fenc? penc? (as one command)
    – Amadan
    Commented Apr 12, 2017 at 4:17
  • In the example above, where I did not save to a file, I get: encoding=utf-8 fileencoding= printencoding= Commented Apr 12, 2017 at 9:26
  • 1
    If I execute set penc=utf8 the problem in the printed output persists. Commented Apr 12, 2017 at 9:27

3 Answers 3

6

Not trivially, but as a workaround, you can convert to HTML then print that from a browser. For example, on a Debian/Ubuntu-based Linux distro:

:TOhtml | w | !x-www-browser %

This might work for other distros:

:TOhtml | w | !xdg-open %

This should work on macOS:

:TOhtml | w | !open -a Safari %

For example I have this in vim: enter image description here

If I type

:ha

I get this: enter image description here

But when I send it to the browser I get this (with color scheme!):

enter image description here

I have this in my .vimrc, which deletes the new html buffer and stored file:

nnoremap <F2> <ESC> :TOhtml <bar> w <bar> !open -a Safari % <CR> <bar> ZQ <CR> <bar> execute '!rm %:p.html' <CR>
1
  • Great tip, thanks, and I didn't even know about using the pipe character like thiat! Note that if you normally set background=dark in your .vimrc, you might want to :colorscheme default (twice) to ensure that what you see on the screen better matches what comes out of the printer. Otherwise you're likely to end up with something like yellow (basically invisible) line numbers in your printout.
    – Kevin E
    Commented Nov 23 at 22:44
3

Per this Stackoverflow:4586628 Q/A, How do you pipe a vim buffer through lpr?, I have this entry in my ~/.vimrc:

nmap PpP :%w !lpr -o lpi=8 -o cpi=14<CR><CR>

that replaces my old entry,

nmap PpP :ha<CR>

(:ha = :hardcopy).

Using :ha, Neovim was not printing Greek letters, ... even though my (Arch Linux) locale was

LANG=en_US.UTF-8
...

and I have this line, near the top of my ~/.vimrc,

scriptencoding utf-8

[Printing is system-dependent, not vim-dependent.]

I added the lpi (lines-per-inch) and cpi (characters-per-inch) to jiggle my font size to that similar to as what I was getting with the :ha command (I measured/counted with an actual ruler), and run it as a PpP command/shortcut, added to my ~/.vimrc, whenever I want to print the current buffer.

1
  • 1
    Briliant! It does, naturally, omit the syntax highlighting, which is a bit of a downside, but for packing more lines on the page, I'll definitely keep this tip handy.
    – Kevin E
    Commented Nov 23 at 22:49
1

Vim seems to produce fairly simple PostScript through the :hardcopy command. PostScript itself has no understanding of Unicode, and so having it render "special characters" properly is a complicated process involving intimate knowledge of fonts, etc.

I filed a bug about this, FWIW: https://github.com/vim/vim/issues/8935

I the meantime, I think the best solution is to use an external program to produce your printable output. For instance paps (link) converts UTF-8 text to PostScript. You can, of course, run this external program from inside Vim using your own command, as described in another answer.

1
  • Yep, and your summary in the GitHub issue is spot on: "though the bad behavior here seems pretty clear, the solution might be non-trivial."
    – Kevin E
    Commented Nov 23 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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