Sublime Forum

Command that fills find and replace fields

#1

Hi, I’ve got a lot of small regex expressions in a file I use to convert various latex math equation environments and such. I’d like to make a plugin where I can search through them in the command palette and fill the “find” and “replace” fields in the replace panel similar to the “slurp_find_string” and “slurp_replace_string”. An additional bonus would be if the “Regular Expression” option would enable itself on its own.

I think the most universal approach would be make a command that takes any two regex or strings and fills the fields. Then one could just make their own .sublime-commands file according to their needs and potentially assign a keybinding to them.

My plugin making skills are very rusty and I have bigger fish to fry at the moment but if some else was also interested in such plugin or could potentially help me with it a bit, I’d certainly bump it up in my list of priorities.

Just as an example, here are a few of the slurps.

MathSlurps: \begin{equation} \end{equation} -> $$ $$
\\begin{equation}\s*\n\s*(.*)\s*\n\s*\\end{equation}
\$\$ \1 \$\$

MathSlurps: $$ $$ -> \begin{equation} \end{equation}
^(\s*)\$\$\s*(.*)\s*\$\$
\1\\begin{equation}\n\1\t\2\n\1\\end{equation}

MathSlurps: $ $ -> ```math ```
\$\$\s*([^\$]*)\s*\$\$
```math\n$1\n```
0 Likes

#2

Does this package do what you want ? https://packagecontrol.io/packages/RegReplace

0 Likes

#3

Seems to me like a bit of an overkill for my needs, but thanks for the suggestion

0 Likes

#4

It should be quite easy to create a plugin that does just what you want, but given your post I’d thought an existing similar package maybe the quickest route.

0 Likes

#5

Build 4123 now adds pattern & replace_pattern arguments to the show_panel command to fill find & replace fields respectively, so this should be easy now. Prior to this build, there was no way to set the replace field.

0 Likes

#6

Now that is exactly what I was looking for. Thank you for the suggestion, I usually don’t read through the changelog.

Made myself a commands’ file and it works like a charm.

[
	{
		"caption": "MathSlurps: \\ẅ -> \\ddot{w}",
		"command": "show_panel", "args": {
			"panel": "replace",
			"pattern": "(\\w)",
			"replace_pattern": "\\\\ddot{\\1}",
			"regex": true
		}
	},	
	{
		"caption": "MathSlurps: ```math ``` -> \\begin{equation} \\end{equation}",
		"command": "show_panel", "args": {
			"panel": "replace",
			"pattern": "```math\\n([^`]*)\\n```",
			"replace_pattern": "\\\\begin{equation}\\n\\t\\1\\n\\\\end{equation}",
			"regex": true
		}
	},
]
``
0 Likes