Pretty Printing Tab Indented Text with Tree

2025-02-26

I like how clean and intuitive tree's output is, and I have been trying to replicate this type of printing myself.

Recently I discovered tree can directly format tab-indented files, and here's how.

Pre-process: Convert Spaces to Tabs

For example, a cue file is indented by spaces, and looks like this:

FILE "audio.flac" WAVE
  TRACK 01 AUDIO
    TITLE "title"
    PERFORMER "author"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "frisk"
    PERFORMER "Dedf1sh"
    INDEX 01 01:43:54
  TRACK 03 AUDIO
    TITLE "In Filtration"
    PERFORMER "MOF8"
    INDEX 01 03:46:22

The text is indented by two spaces, let's convert it to tabs.

Open it up with (neo)vim, and run the following: [1]

:set tabstop=2      " Set to number of spaces that matches the original file
:set noexpandtab    " Force to use tab
:%retab!            " Retabulate the whole file
:wq                 " Save and quit

This will convert spaces to tabs, verify with movement keys like hjkl.

Using tree to Pretty Print

From tree(1), run the following

tree --fromtabfile <indented_file> -n

--fromtabfile Like --fromfile, tree reads a directory tree from a text file where the files are tab indented in a tree like format to indi‐ cate the directory nesting level.

also:

-n Turn colorization off always, over-ridden by the -C option, however overrides CLICOLOR_FORCE if present.

The output should look like the following:

tracks.txt
└── FILE "2025-02-25_22h08m22s.flac" WAVE
    ├── TRACK 01 AUDIO
    │   ├── INDEX 01 00:00:00
    │   ├── PERFORMER "OCTOTOOL"
    │   └── TITLE "Cephaloparade"
    ├── TRACK 02 AUDIO
    │   ├── INDEX 01 01:43:54
    │   ├── PERFORMER "Dedf1sh"
    │   └── TITLE "#6 frisk"
    ├── TRACK 03 AUDIO
    │   ├── INDEX 01 03:46:22
    │   ├── PERFORMER "MOF8"
    │   └── TITLE "In Filtration"

(...)

Style Options

You can specify charset to use, for other styles, for example:

tree --fromtabfile stuff.txt --charset=ascii

yields:

2025-02-25_22h08m22s.cue
`-- FILE "2025-02-25_22h08m22s.flac" WAVE
    |-- TRACK 01 AUDIO
    |   |-- INDEX 01 00:00:00
    |   |-- PERFORMER "OCTOTOOL"
    |   `-- TITLE "Cephaloparade"
    |-- TRACK 02 AUDIO
    |   |-- INDEX 01 01:43:54
    |   |-- PERFORMER "Dedf1sh"
    |   `-- TITLE "#6 frisk"
    |-- TRACK 03 AUDIO
    |   |-- INDEX 01 03:46:22
    |   |-- PERFORMER "MOF8"
    |   `-- TITLE "In Filtration"

(...)

  1. Link to original solution