View Full Version : Cut and paste in snippet

I want a snippet that puts the selected text if any in Python triple quotes. This was my attempt:
<% var scimoz = ko.views.manager.currentView.scimoz; scimoz.cut(); %>
<% scimoz.paste() %>
"""This does not work as expected. Starting with selected text "Hello World" I get:
Hello world
"""So what is to be done?

scimoz.paste() operates directly on the scimoz buffer the moment it is called. It is not aware of the snippet template that it is called from, so the output makes no sense.
Here is an EJS snippet that works:
<%= ko.views.manager.currentView.scimoz.selText%>
"""EDIT: Another way of looking at snippets: the body of the snippet is evaluated apart from the insertion. The paste() call would have worked if the evaluation and insertion were handled line-by-line. But in fact the paste() call happened during evaluation and before insertion -- it just cut and pasted the text in the same place (evaluation phase), then inserted the triple quotes (insertion phase).

Two things, scimoz.selText will grab all selected text on the page (multi cursor stuff). Just something to be aware of. You can use scimoz.multipleSelection to see if there is multiselection.
Also, please use the new API, var scimoz = require("ko/editor").scimoz();


The scimoz() and scintilla() functions are helper methods, and while they are unlikely to change they should not be relied on.
By the way you do not need to use any EJS for this snippet at all, Snippets have the "Selection" mechanic built-in. Just press the little arrow next to the input field.
Example snippet for your use-case: