Question

I would like to set my sublime up to enter a mode where when I paste a set of data looking like this:

1234567 This is code 1
2345678 This is code 2
3456789 This is code 3

It will end up like this:

['1234567', 'This is code1'],
['2345678', 'This is code2'],
['3456789', 'This is code3'],

Is there any way to easily do this using a hot key to switch in and out of my "special paste mode" or even a way to create a snippet that will edit the selection changing it. Heck, at this point even a simple find/replace regex will do.

Was it helpful?

Solution

Well, this looks exciting.

Creating a "special paste mode"—which would have to involve a custom plugin and regular expressions anyway—sounds awfully complicated when you can just use a snippet substitution. You can even bind your shiny new snippet to a key combination, as explained both in this forum post and in this article (scroll down).

Make a new snippet with this as its content:

<snippet>
<content><![CDATA[
    ${SELECTION/(^[\d]+) (.+) ([\d]+)/['$1', '$2$3'],/g}
]]></content>
</snippet>

Save the snippet with whatever name suits your fancy. Now, after pasting, select all of your recently pasted data, split it into lines with Ctrl+Shift+L, then open the Command Palette with Ctrl+Shift+P and type in the snippet name you chose. Press Enter, et voilà! Your mysterious data table has been formatted as an array.

Of course, you'll have to edit the regular expression segment of the snippet ((^[\d]+) (.+) ([\d]+)) in the case that your input isn't as rigorous as your example text is. Regexpal can help.

OTHER TIPS

Here is one more generic solution that splits up an array on space.

<snippet>
<content><![CDATA[
[${SELECTION/\s*(\S+)\s*/'$1',/g}]
]]></content>
</snippet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top