Loading video...

Video Failed to Load

Go Home

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

nolen's profile picture
nolen1 year ago

(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:

nolen's profile picture
nolen1 year ago

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:

nolen's profile picture
nolen1 year ago

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.

nolen's profile picture
nolen1 year ago

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)

nolen's profile picture
nolen1 year ago

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

nolen's profile picture
nolen1 year ago

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"

nolen's profile picture
nolen1 year ago

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)

nolen's profile picture
nolen1 year ago

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

nolen's profile picture
nolen1 year ago

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

nolen's profile picture
nolen1 year ago

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

nolen's profile picture
nolen1 year ago

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

nolen's profile picture
nolen1 year ago

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.

Direct Mail for Mac's profile picture
Direct Mail for Mac2 years ago

Say hello to Direct Mail – the macOS app that makes crafting email newsletters a breeze! 🚀 Create, send, and engage effortlessly. #MacUsers #EmailMarketing

Related Videos