Sublime Forum

RegReplace Plugin

#35

Finally finished this.

Version 1.6

  • Save under selection added (limits searches to selections if and only if selection exists)
  • Cleanup “highlight on save regions” when performing other regex searches

Replace Only Under Selection(s)
Sometimes you only want to search under selections. This can be done by enabling the selection_only setting in the settings file. By enabling this setting, regex targets will be limited to the current selection if and only if a selection exists. Auto replace/highlight on save events ignore this setting. If you have a command that you wish to ignore this setting, just set the no_selection argument to true. Highlight style will be forced to underline under selections if find_only is set to ensure they will show up.

0 Likes

#36

Is it possible to fold (rather than replace) on_save?

0 Likes

#37

check the first post for details.

0 Likes

#38

Though RegReplace does support fold, unfold, mark, unmark, I am not sure it is all supported via “on_save”. A matter of fact, I am pretty sure it isn’t…but I could add it. RegReplace is a plugin that I plan on spending some more time on some time soon. It was thrown together pretty quick to fulfill my basic needs, and while it works well for what I use it for, it has some gaps in features etc. and has some areas that could use better polish.

If you hop on github and create an issue for allowing for fold on save, I will try and add it sometime soon.

0 Likes

#39

I added an issue to github. Totally no rush.

I was very surprised to find RegReplace supported folding matches.

0 Likes

#40

[quote=“quodlibet”]I added an issue to github. Totally no rush.

I was very surprised to find RegReplace supported folding matches.[/quote]

I probably use terms like “soon” too freely…it is in my queue though :wink:.

I wanted to be able to fold all comments, so I added the fold/unfold a while ago…

0 Likes

#41

I’m always hesitant to ask you for new features – it feels like enabling an addict :smile:

(I am referring to the insane speed with which you respond to both messages and bugs & suggestions.)

[quote=“facelessuser”]
I wanted to be able to fold all comments, so I added the fold/unfold a while ago…[/quote]

I’m tinkering with a todo list (of all things!) and I want to fold some meta-data, but the gist is the same. Because I have a “nervous” Ctrl+S, it seemed reasonable (?) to use “on_save” as a trigger for this. This is such a niche thing.

0 Likes

#42

I’ve created the following, but nothing happens.

In the “reg_replace.sublime-settings”:

// CONVERT TO LESS - STEPH
“lessrewrite_width”: {
“find”: “(\t*)^-]\bwidth\b: (-?[0-9]*%|-?[0-9]px|auto|-?[0-9]);”,
“replace”: “$1.w($2);”
}

in the “Default.sublime-commands”:

// Convert rewrite CSS to LESS - Steph
{
“caption”: “Reg Replace: CSS to LESS”,
“command”: “reg_replace”,
“args”: {“replacements”: “lessrewrite_width”]}
},

I thought I did it correctly, but what am I missing?

0 Likes

#43

Please give me a snippet of example code and the description of what you want to happen.

0 Likes

#44

I want to use the RegEx in the replace to change this:

 [code]width: 250px;[/code]

to this:

[code].w(250px);[/code]

Once I get one working, my goal is to chain a number of them together so that I can convert a whole document instead of spending time inputing all the RegEx piece by piece.
I’m attaching the LESS document I’m trying to run this on, and the RegReplace files I modified.
RegReplace.zip (8.15 KB)

0 Likes

#45

Two things. Python uses \1 instead of $1. And your regex is in a string so escape literal backslashes.

[pre=#2D2D2D] // CONVERT TO LESS - STEPH
“lessrewrite_width”: {
“find”: “(\t*)^-]\bwidth\b: (-?[0-9]*%|-?[0-9]px|auto|-?[0-9]);”,
“replace”: “\1.w(\2);”
}[/pre]

0 Likes

#46

Thanks for the info about the RegEx, I didn’t realize it was Python.

So I updated my code with your suggestion, but nothing happens when I run it from the Command Palette, or am I supposed to run it from somewhere else?

0 Likes

#47

I verified the code before I posted; that is why I asked for an example.

I did not run it on the ads_ca-fr_home.less you provided because there are no width attributes that need to be converted in it; therefore, it would do nothing. So if that is the file you are testing on, you are wasting your time because width is already converted in every instance in that file.

0 Likes

#48

Also, if you are still having issues, I may need some more info. It should work fine from command palette. But if it isn’t, you may be doing something odd, so I would need to know how you installed the package, are you editing the default version of the settings file, or did you copy it to user and edit that one. I may need to see your settings file to verify what you have done. Based on what you gave me, it should work, and did for me. If it isn’t working for you, then something else isn’t right on your end.

0 Likes

#49

I just found your plugin, and it looks really helpful!

For my needs, I’m trying to simply get a command mapped to a key binding, and have it fold all functions (and only functions) in PHP files, which are structured like:

/**
 * Docblock for function.
 */
function example_function($param) {
  return $param;
}

The built-in code folding folds all comments, too, which is annoying in my workflow. Therefore I’m trying to add the command “fold_functions”, and I think I’m getting hung up somewhere; it’s not clear if I need to define the ‘fold’ action in the command itself, and then how to run the “fold_functions” command without the key binding (which I might also have set up incorrectly).

Here’s my customizations:

Inside Default.sublime-commands (which Sublime opened when I chose Sublime > Preferences > Package Settings > Reg Replace > Commands - User):

{
    {
        "caption": "Code Folding: Fold All Functions",
        "command": "fold_functions",
        "keys": "shift+super+0"],
        "args": {"replacements": "fold_functions"], "action": "fold"}
    },
}

Inside reg_replace.sublime-settings (which Sublime opened when I chose Sublime > Preferences > Package Settings > Reg Replace > Commands - User):

{
    // Fold all functions.
    "fold_functions": {
        "find": "^function\\b(.*?(\\n))+.*?\\}",
        "greedy": false,
        "case": false
    }
}

It’s a little confusing to me, but I’ve also only started using Sublime a few weeks ago, so I’m trying to understand the system.

0 Likes

#50

I have the following usecase:
find hex colors with uppercase letters: #([A-F\d]+)
replace uppercase letters with lower ones: #\L$1\E
I expect to find #FF00AA and replace it with #ff00aa, which works as expected when i test it from sublime’s Find and replace dialog.
Given the following settings I get a literal replace with #\L$1\E

"css_lowercase_hex_colors": {
  "find"    : "#([A-F\\d]+)",
  "replace" : "#\\L$1\\E",
  "greedy"  : true,
  "case"    : true,
  "literal" : false
}

Is this Oniguruma feature supposed to be working with RegReplace?

0 Likes

#51

RegReplace uses the python re engine (mainly because the ST find API is very awkward for RegReplace to access due to the way RegReplace works, and I didn’t want to use the ST API in half of the code, and re in the other half).

[quote=“vitaLee”]I have the following usecase:
find hex colors with uppercase letters: #([A-F\d]+)
replace uppercase letters with lower ones: #\L$1\E [/quote]

RegReplace, since it uses re, requires groups to be specified as \1 not $1. So, #\L\1\E would be more appropriate (in a string you would obviously escape the slashes).

But you still won’t get your desired effect because re does not support Oniguruma.

I can see the desire to use Oniguruma though…maybe down the line I can implement this with a third party package like github.com/mitsuhiko/ponyguruma, or at the very least create some kind of system to allow users to write scripts to manipulate the regex groups format on replace.

Hopefully, that answered your questions, and while I don’t have an immediate fix to allow you to do what you want, maybe down the line. Feel free to make this suggestion on Github at the RegReplace repo.

0 Likes

#52

I guessed that could be the reason for this.
Thanks anyway.

0 Likes

#53

What are my options for running a RegReplace replacement on a batch of 360 files?

0 Likes

#54

Right now? Probably something like sublimes built in batch find/replace, or some other kind of batch search and replace tool, I use grepWin on windows.

0 Likes