简体   繁体   中英

How do you use a replace command in a Sublime Text 2 snippet?

I have defined a snippet in Sublime Text 2 as follows:

<snippet>
    <content><![CDATA[
<cfqueryparam cfsqltype="cf_sql_${1:integer}" value="$SELECTION">
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

This works correctly to create a cfQueryParam tag around the selected text, and highlights the part of the cfSqlType that I may need to change for different data types.

However, when using this for strings I need to first click on each side of the value, delete the single quotes, then select the value and hit my keybind for the snippet. This requires two mouse clicks, a double-click, and three keypresses, in addition to more precise mouse aiming. With integers I require only a double click and one keypress. As I have several thousand files to go through doing these replaces, this will make the difference of many hours of work.

Is it possible to put something in the snippet that will cause it to remove a particular character, in this case single quotes? I assume some sort of replace is possible, but I cannot find anything in the docs.

The official docs are kind of sparse, but the community-developed unofficial documentation is much more complete. Boost PCRE-style regexes are supported in snippets, see here for information. I'll leave it up to you to develop the regex for removing the quotes :)

Alternatively, there are a couple of plugins that could help you: Expand Selection to Quotes and Unquote . Each time you run the Expand Selection to Quotes command, it expands the selection to the next set of quotes:

Start with this:

run command once to select string inside quotes:

run command again to select quotes:

then run Unquote command to delete quotes, leaving selection:

I'd suggest creating a macro that runs expand_selection_to_quote twice, unquote once, and then your snippet. Bind this to a different key binding than your original snippet, so you can use the snippet just for individual words, and the macro for strings.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM