简体   繁体   中英

Populate snippets from a list of values in Sublime Text 2

I have a list of values I'd like to use as an input for snippets

  • I have created a basic snippet which has placeholder variables

  • I have a list of values as Python list (can be a list of strings, or list of dicts if more complex values are used)

What would be the most pain-free way to generate output text based by running the snippet against each value in the list with Sublime Text 2? What ways there are programmatically invoke snippets with input data?

You can use run_command to execute any action visible in the UI:

view.run_command("insert_snippet", {"name": "Packages/etc/my.sublime-snippet"})
view.run_command("insert", {"characters": "field 1"})
view.run_command("next_field")
view.run_command("insert", {"characters": "field 2"})
view.run_command("next_field")
view.run_command("insert", {"characters": "field 3"})

A good way of finding command names and arguments is to record a macro with Tools -> Record Macro, and look at the generated JSON.

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