Sublime Forum

HOWTO: Keybind, insert_snippet + clipboard question

#1

Hi All,

I’m getting familiar with writing custom key bindings.
I have the following snippet:

... { "keys": "ctrl+shift+d"], "command": "insert_snippet", "args": { "contents": "console.log('=== $SELECTION $TM_FILENAME $TM_LINE_NUMBER] ===', $SELECTION);${0}" }, "context": { "key": "selector", "operator": "equal", "operand": "source.js", "match_all": true }] } ...

It is a simple snippet insertion with a purpose to aid JS debugging. It allows me to select text (variable name, e.g. testvar) and using ctrl+shift+d converts it into:

console.log('=== testvar some_file.js [477] ===', testvar); // where 477 is a line number and some_file.js is the current js file I'm working in.

That works all fine and dandy.
What I’m trying to accomplish is to be able to pass clipboard content as an argument to insert_snippet. Is it possible to pass commands as arguments (paste in this case), or is there a different approach to accomplish this task? It would be nice to be able to use clipboard content instead of selection (or both if needed) somehow.

Any help is appreciated!

0 Likes

#2

TextMate uses pbpaste for the clipboard content but AFAIK this is not available to ST (it is an interpolated shell command). I don’t believe that you can achieve this with a simple key-binding, soz… I won’t be upset, though, if someone proves me wrong :wink:.

It is possible via a plug-in, using get_clipboard(), but this won’t behave the same as a snippet.

0 Likes

#3

My** Dynamic Snippet**: patent pending! :sunglasses:

[code]import sublime, sublime_plugin

MYSNIPPET =
“”"
This guy ${1:%(name1)s} pasted this ${2:%(name2)s}.$0
“”"
class DynamicSnip(sublime_plugin.TextCommand):
def run(self, edit):
someName = self.view.substr(self.view.sel()[0])
pasted = sublime.get_clipboard()
self.view.run_command(“insert_snippet”, { “contents” : (MYSNIPPET %
{ “name1”: someName, “name2”: pasted }) })[/code]
Simpler, of course, is to use a tab-placeholder and for the user to Paste (Ctrl-V) the clipboard content…?

1 Like

#4

Restrict to JS file:

[code]import sublime, sublime_plugin

MYSNIPPET =
“”"
This guy ${1:%(name1)s} pasted this ${2:%(name2)s}.$0
“”"
class DynamicSnip(sublime_plugin.TextCommand):
def run(self, edit):
if not self.view.match_selector(0, ‘source.js -string -comment -constant’):
return
someName = self.view.substr(self.view.sel()[0])
pasted = sublime.get_clipboard()
self.view.run_command(“insert_snippet”, { “contents” : (MYSNIPPET %
{ “name1”: someName, “name2”: pasted }) })[/code]

1 Like

#5

My dynamic snippet has great potential, which I don’t think has been spotted. It could be a package where we can set and read package-settings, or variables created for a particular view, and dynamically insert these within snippets. Changing a setting would mean that the next time we insert a snippet it would reflect the current setting.

An example would be for docstrings, where the snippet could read the previous-lines’ argument list, and insert other settings, such as the project-name, user or a datestamp, etc… I don’t know if there is a package that already does something similar for docstrings(?).

1 Like