Directory Listing for gVim Folded viewing

The problem: do I have that somewhere?

Something that was holding me up organisationally was finding out if I had a film, or a podcast, or some notes somewhere on a disk drive that belongs to me but that’s not connected to my machine, which while I’m writing this is my old netbook.

The first solution: tree

While I had all my hard drives hooked up to my GNU+Linux machine, I could open a terminal in a directory, and:

find . -mindepth 1 -maxdepth 1 -type d -exec sh -c 'dn=$(basename "{}"); tree "$dn">"$dn".txt' \;

– which creates a nicely tree‘d text file listing the contents of each subdirectory of the current directory.  Neat, but I couldn’t figure out how to fold up those pretty text trees in gVim.  It got me thinking though.

The second solution: a Python script creating a bespoke Vim filetype

I wrote a short Python script that I drop into a directory, and run.  It works recursively through the directory, adding all directory and filenames that it finds to one long string, which it then prints out to a text file (unambiguously named after both the directory and the script).  Python’s os.walk handles the ordering of the search in a natural way, and I shorten the directory names down to just the slashes and tail, and pack all the filenames together into one line.  I also wrote an accompanying gVim folding plugin for my beskope Vim filetype dirlist, and a simple syntax highlighting for it too, so the end result looks like this (click for full-sized):

DirLVF example

– that’s a file over 5k lines long listing, in a highly compact folded form, all of the directory’s contents.  So, for example, I thought I’d written something sometime ago about being hoist by ones own petard, and there it is – found it in Other_stack\UK\Petard Hoist.doc.  Of course I’d have to go and connect the drive to actually look in that doc, but at least I know it’s there.

Share Button