简体   繁体   中英

Commands Not Showing in Command Palette with RegReplace (Sublime Text 3)

I'm trying to run a series of commands with the RegReplace plugin in Sublime Text 3 but I cannot get the command to load and I cannot get the keybindings to work either. I have no clue what's wrong.

Steps Taken:

  • Installed RegReplace
  • Opened the Command Palette
  • Searched for "RegReplace: Create New Regular Expression"
  • Modified the Rule to the following

    """ If you don't need a setting, just leave it as None. When the rule is parsed, the default will be used. Each variable is evaluated separately, so you cannot substitute variables in other variables. """ # name (str): Rule name. Required. name = "extract_variables" # find (str): Regular expression pattern or literal string. # Use (?i) for case insensitive. Use (?s) for dotall. # See https://docs.python.org/3.4/library/re.html for more info on regex flags. # Required unless "scope" is defined. find = r".*\\[(.*[^(<|>)]*?)\\].*" # replace (str - default=r'\\g<0>'): Replace pattern. replace = r"\\1" # literal (bool - default=False): Preform a non-regex, literal search and replace. literal = None # literal_ignorecase (bool - default=False): Ignore case when "literal" is true. literal_ignorecase = None # scope (str): Scope to search for and to apply optional regex to. # Required unless "find" is defined. scope = None # scope_filter ([str] - default=[]): An array of scope qualifiers for the match. # Only used when "scope" is not defined. # # - Any instance of scope qualifies match: scope.name # - Entire match of scope qualifies match: !scope.name # - Any instance of scope disqualifies match: -scope.name # - Entire match of scope disqualifies match: -!scope.name scope_filter = None # greedy (bool - default=True): Apply action to all instances (find all). # Used when "find" is defined. greedy = None # greedy_scope (bool - default=True): Find all the scopes specified by "scope." greedy_scope = None # format_replace (bool - default=False): Use format string style replace templates. # Works only for Regex (with and without Backrefs) and Re (with Backrefs). # See http://facelessuser.github.io/backrefs/#format-replacements for more info. format_replace = None # selection_inputs (bool -default=False): Use selection for inputs into find pattern. # Global setting "selection_only" must be disabled for this to work. selection_inputs = None # multi_pass (bool - default=False): Perform multiple sweeps on the scope region to find # and replace all instances of the regex when regex cannot be formatted to find # all instances. Since a replace can change a scope, this can be useful. multi_pass = None # plugin (str): Define replace plugin for more advanced replace logic. plugin = None # args (dict): Arguments for 'plugin'. args = None # ---------------------------------------------------------------------------------------- # test: Here you can setup a test command. This is not saved and is just used for this session. # - replacements ([str]): A list of regex rules to sequence together. # - find_only (bool): Highlight current find results and prompt for action. # - action (str): Apply the given action (fold|unfold|mark|unmark|select). # This overrides the default replace action. # - options (dict): optional parameters for actions (see documentation for more info). # - key (str): Unique name for highlighted region. # - scope (str - default="invalid"): Scope name to use as the color. # - style (str - default="outline"): Highlight style (solid|underline|outline). # - multi_pass (bool): Repeatedly sweep with sequence to find all instances. # - no_selection (bool): Overrides the "selection_only" setting and forces no selections. # - regex_full_file_with_selections (bool): Apply regex search to full file then apply # action to results under selections. test = { "replacements": ["extract_variables"], "find_only": True, "action": None, "options": {}, "multi_pass": False, "no_selection": False, "regex_full_file_with_selections": False }

This code Generates the following in AppData\\Roaming\\Sublime Text 3\\Packages\\User\\reg_replace_rules.sublime-settings

{
    "replacements":
    {
        "extract_variables":
        {
            "find": ".*\\[(.*[^(<|>)]*?)\\].*",
            "name": "extract_variables",
            "replace": "\\1"
        }
    }
}

And then I created the following command under the same directory with filename Default.sublime-commands

[   
    { 
        "caption": "Reg Replace: Extract ERS Variables",
        "command": "extract_ers_variables",
        "args": {
            "replacements": [
                "extract_variables"
            ]
        }
    }
]

After saving all of this, I still do not see the command in the command palette and it didn't show when I tried to save it as a keymap either.

Any help is much appreciated

The reason this doesn't work for you is that you have the command wrong in your Default.sublime-commands file. In particular, the command extract_ers_variables does not exist, so the entry for it in the command palette is hidden because selecting it wouldn't do anything. Visually speaking, if this command was in a sublime-menu file, the entry in the menu would appear disabled.

If you select Preferences > Package Settings > RegReplace > Quick Start Guide from the menu and follow through the example that's displayed, note that when it comes to the part about creating the command entry in Default.sublime-commands , it tells you to use reg_replace as the command, and the name of the replacements argument is what tells the command which replacement to do.

As such, your entry should look more like:

[   
    { 
        "caption": "Reg Replace: Extract ERS Variables",
        "command": "reg_replace",
        "args": {
            "replacements": [
                "extract_variables"
            ]
        }
    }
]

Came here with my own troubles and may as well document my dumb mistakes. I know nothing of JSON.

When adding two replacements used together going by the examples at the developer's site , I could not get the command to show up in the Command Palette. I could get a keybinding to work, but it gave error messages that the first replacement could not be found…after having successfully used it. The culprit was a malformed reg_replace_rules.sublime-settings file:

//Wrong
{
    "replacements":
    {
        "rep_one":
        //stuff
    },
    "replacements":
    {
        "rep_two":
        //other stuff
    }
}

//Correct
{
    "replacements":
    {
        "rep_one":
        //stuff, comma
        "rep_two":
        //other stuff
    }
}

Fixing that cleared up the error message, but the command still would not appear in the Command Palette. The problem there was more bad JSON, this time in Default.sublime-commands .

//Wrong
{
    "caption": "My Command",
    "command": "reg_replace",
    "args": {"replacements": ["rep_one", "rep_two"]}
}

//Correct
[
    {
        "caption": "My Command",
        "command": "reg_replace",
        "args": {"replacements": ["rep_one", "rep_two"]}
    }
]

This is probably obvious to people who have learned JSON properly and use it regularly, and perhaps one day I will be one of those.

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