22

How can I highlight XML from stdin (e.g. piped from xmllint --format) to stdout?

I know how to get highlighting working in nano and view, but is there something that just outputs to stdout and exits?

What I'd link to do is just type something like

xmllint --format xmlfile.xml | some-highlighter

or maybe, for big files

xmllint --format xmlfile.xml | some-highlighter | less

and get pretty output.

Sietse
  • 554
  • 5
  • 16

4 Answers4

21

Supercat and grcat (grc) can do piped syntax highlighting. You'll probably have to make or find configuration files for XML. They are both available in the Ubuntu repositories as well as at the links provided.

Pygmentize has XML highlighting included. It's available as "python-pygments" in the Ubuntu repositories or by using easy_install Pygments.

xmllint --format xmlfile.xml | pygmentize -l xml | less
Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • Pygmentize did the trick for me, although, at least on OS X, I had to give it the option `-O encoding=UTF-8` to make it work on xmllint's output. Thanks! – Sietse Sep 29 '10 at 12:38
  • 1
    If you're like me and annoyed by pygmentize complaining when you exit less without consuming all its output (Broken pipe), pipe through buffer (`apt-get install buffer`). E.g. `xmllint --format foo.xml |pygmentize -g |buffer |less -r` – Marlies Jan 14 '14 at 16:29
  • 1
    In more modern Ubuntu, I used `sudo apt install python-pygments` to install it. – MarkHu Mar 31 '19 at 07:44
14

This is how you do it using GNU source-highlight and less:

source-highlight -i /tmp/foo.xml -f esc | less -r
Pete
  • 317
  • 2
  • 13
user78659
  • 141
  • 1
  • 2
  • 1
    From stdin, it's just `source-highlight -s xml -f esc` – MikeFHay Apr 25 '13 at 16:08
  • Although actually, I'm finding `esc256` produces prettier output than `esc` – MikeFHay Apr 25 '13 at 16:26
  • 1
    Note, this won't format the XML, and if your XML is all on one line, it will cause `source-highlight` to run very slowly. It is probably parsing the input line by line. This command gave me good, fast results: `xmllint --format - < input.xml | source-highlight -f esc -s xml | less -F` – Winny Jun 15 '15 at 21:25
  • @Winny, that is awesome, thank you. One minor note: I got it working with `less -r` (as stated in the answer) instead of `less -F`. – Anatoly Scherbakov Feb 04 '16 at 16:47
8

I found highlight in Homebrew for OSX, and I'm sure it's available in the Ubuntu repository. It does highlighting and output to a number of formats, including terminal output.

Sietse
  • 554
  • 5
  • 16
  • 3
    `pbpaste|xmllint --format -|highlight --out-format=ansi --syntax=xml` did the trick for me. (I could argue about highlight's color choices, though.) – tuomassalo Oct 03 '12 at 09:19
5

I use bat:

xmllint --format - | bat -pP

-p removes all decoration (line numbers, etc.), -P disables the pager. Update: use -pp with newer versions of bat.

bat: https://github.com/sharkdp/bat

sebnukem
  • 151
  • 1
  • 4
  • For `bat` 0.12.1 I cannot use `-P`, but I can use `--paging=never` instead. (The error message was: `error: Found argument '-P' which wasn't expected, or isn't valid in this context`) – knb Apr 03 '22 at 08:50