Loading video...
Video Failed to Load
Bad Apple but it's 6,500 regular expressions that I search for in Vim one at a time with a macro i built most of a general purpose video-in-vim-using-regex-search tool for this lol (details below!)
65,676 views • 1 year ago •via X (Twitter)
13 Comments

(two links, then I'll summarize how this works below in a thread) blog here: I haven't cleaned up or documented the code *at all* (this was a one day project) but the raw code is over here if you want to piece it together lol:

step one is pretty easy: take the video, turn it into 6,500 pngs (one for each frame), then convert frames to a 2D array of ints representing black and white pixels. this repo does some of the work already:

next - how do we draw in vim at all? well, we can make a grid of chars and then search for one of them to highlight it. but that has some problems.

problem one - the image isn't as sharp as it could be. We can fix this by tweaking vim's search highlighting (`hi Search cterm=NONE ctermfg=grey ctermbg=grey`). problem two - we want square pixels, not rectangles. so swap to a font that is square (font i use is called square)

So that lets us draw one pattern. but how do we draw arbitrary stuff?? I thought I'd have to craft some crazy regexs, but i read the vim search manual and found that you can literally write search patterns for rows and columns!! i love vim so much

we can combine these patterns to create a "search" that matches a rectangle on the screen. And then we can combine *those* patterns to create a search for *many* rectangles on the screen. So now we can draw, and the problem is "decompose each frame into a series of rectangles"

Turns out that's hard to do in a general purpose way. I wrote something naive that worked really well in some cases, but fell flat in other cases (which killed our framerate)

the above algo works really poorly for this image, for example

so i pulled out one of my fave tricks - instead of writing one good algorithm, write several bad ones! then try them all and use the best result

wait so how do we actually play this inside vim??? Here's the vim setup. There are 3 windows across the top and 1 on the bottom. top left/right are just to center top middle one, which is a 120x90 file of just spaces (to "search") through. bottom window has our search strings

then we record a macro! it is "ay$:let @/=@a^M+ Here's an explanation:

And that's it! Now we can play bad apple inside vim only using regular expressions. or search strings, please don't yell at me about the technical definition of a regex.

Say hello to Direct Mail – the macOS app that makes crafting email newsletters a breeze! 🚀 Create, send, and engage effortlessly. #MacUsers #EmailMarketing
Related Videos
Sensitive content
I built a Multimodal AI Agent using Gemini 2.0 that can understand videos and search the internet at the same time. 100% Opensource Code with step-by-step tutorial.
Shubham Saboo
97,816 views • 1 year ago
