Sublime Forum

capture_snippet — it's like macros but slightly useful

#1

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.)

0 Likes

#2

Cool! :smile:

FWIW, I find that creating a .sublime-completions file is less of a hassle than a snippet. (They are more limited, though.)

0 Likes

#3

Does this fall apart in a language that might naturally use $1 in part of its notation?

0 Likes

#4

Only if you didn’t escape the $ symbols. Where it really falls apart is if you have ]]> in a language, though this is a side-effect of it being more or less impossible to have ]]> in CDATA blocks (CDATA is a strange beast), which I could handle but don’t see much reason to since it’s not a case I need to worry about.

If it becomes an issue, it’s just a matter of adjusting the combined snippet string, but right now I don’t bother since I assume your $s are intentional and you’ll escape them otherwise. You could always, for example, use a different character and replace it with a $ where needed, but outside of PHP, I can’t think of many languages that use $ in a meaningful way (one is a BASIC which uses $… for hex literals). So, any failing in the plugin is also just an issue with snippets and you’ll have to adjust as needed.

0 Likes

#5

I had a similar need and what I did is a simpler command

class PasteAsSnippetCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        v = self.view
        snippet = sublime.get_clipboard()
        v.run_command("insert_snippet", {"contents": snippet})

this way you just copy or cut your snippet normally from a view and then define a key map to “paste_as_snippet” like this

{ "keys": "ctrl+alt+v"], "command": "paste_as_snippet" },
0 Likes