4

If I start vim in Ex-mode, I can have it print the entire file with %print, but it just prints the raw text. Is there a command I can use to get it to print the file with syntax highlighting?

  • Unrelated question: I've often felt there was a good use case for Ex-mode but I've yet to find myself using it. How do you utilize Ex-mode? What is your workflow with it? – Sukima Dec 18 '15 at 13:49
  • Sukima: this is one of my first stabs at using ex-mode - I wanted to use it to put vim at the end of a pipeline to emit the syntax-highlighted version of a file. – rampion Dec 18 '15 at 14:18
  • 1
    Ahh. I see the trouble. So what you want isn't useful. Vim doesn't have the ability to output text with VT100 escape codes. They can show color via ncurses but that isn't pipe-able. I suggest something like pygments.org – Sukima Dec 18 '15 at 14:36
  • One interesting possibility is to diff files with diff -e file1 file2 to produce an ed/ex script. Then apply the ex script(s) to another file. codeselfstudy.com/blogs/command-line-scripting-with-ed-and-ex – Josh Nov 20 '17 at 6:15
  • I don't know if there is a solution to this problem, but this may be relevant: github.com/neovim/neovim/issues/2765 – Josh Nov 21 '17 at 3:21
4

You can do this by combining two tools:

  1. :TOhtml generates an HTML file that contains syntax highlight.
  2. elinks prints the HTML file into the terminal with colors. See this post.

For example:

  1. I created ~/test_vimrc.vim:

    The file in Vim

  2. I typed :TOhtml, so Vim created ~/test_vimrc.vim.html.

  3. I go to the terminal and type this:

    elinks -dump -dump-color-mode 3 ~/test_vimrc.vim.html
    

    Then I see this:

    The file in the terminal with syntax highlight

You need to test which color mode works in your terminal. For me 2 and 3 work, but the background is gray if I use 1 or 4. Also, within tmux, it it always gray:

The file in the terminal inside tmux

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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