Home Download Buy Blog Forum Support

capture_snippet — it's like macros but slightly useful

capture_snippet — it's like macros but slightly useful

Postby Nilium on Tue Apr 15, 2014 8:07 am

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.

capture_snippet

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:

Image

(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.)
Nilium
 
Posts: 53
Joined: Fri May 25, 2012 11:22 pm

Re: capture_snippet — it's like macros but slightly useful

Postby quodlibet on Wed Apr 16, 2014 2:36 pm

Cool! :)

FWIW, I find that creating a .sublime-completions file is less of a hassle than a snippet. (They are more limited, though.)
quodlibet
 
Posts: 531
Joined: Sat Jun 18, 2011 5:53 pm

Re: capture_snippet — it's like macros but slightly useful

Postby facelessuser on Wed Apr 16, 2014 4:10 pm

Does this fall apart in a language that might naturally use `$1` in part of its notation?
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: capture_snippet — it's like macros but slightly useful

Postby Nilium on Wed Apr 16, 2014 8:42 pm

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


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.
Nilium
 
Posts: 53
Joined: Fri May 25, 2012 11:22 pm

Re: capture_snippet — it's like macros but slightly useful

Postby bordaigorl on Tue Apr 29, 2014 6:03 pm

I had a similar need and what I did is a simpler command
Code: Select all
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

Code: Select all
{ "keys": ["ctrl+alt+v"], "command": "paste_as_snippet" },
bordaigorl
 
Posts: 8
Joined: Sun Mar 16, 2014 2:24 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 8 guests