Earlier today I found myself writing a bunch of code that was repetitive but didn’t really make sense to shorten (i.e., similar but not the same — the worst kind of situation) and wasn’t really worth focusing on at the time. Being the sort of person who would rather not type out the same-ish thing 6 times in a row and not the sort of person to create a dedicated snippet file for that, I decided it would be really nice if I could just type out a snippet inline with my code, mash a key, and have that get thrown in a snippet file. Turns out that’s easy to do, hence this post.
[size=150]capture_snippet[/size]
In the above gist is my capture_snippet command, which basically does exactly what I wanted earlier. It captures the current selection (or selections) as a snippet and shoves it into a file. That’s it. It also unindents the captured selections within reason (i.e., strips out the minimum amount of indentation it can), but that’s not noteworthy.
Basically, you just map it to one key and map an insert_snippet for the captured snippet file to another key and it ends up being kind of like macros except not completely and utterly useless (insert much grumbling about the limitations of macros here). It’s definitely a niche case thing, but I like it, so I figured I’d share it.
Here’s a quick gif of it in use in some sort of basic, contrived example situation:
(Note: I won’t be surprised if someone’s already done this considering I did zero research to see if it already existed. If it does: cool.)