Replace 升华-多字替换

Replace 升华-多字替换,replace,sublimetext,Replace,Sublimetext,我正在尝试使用升华的“在文件中查找”将多个单词替换为多个文件中的另一个单词,例如: Dog -> Cat Hat -> Shoe Farm -> City 在记事本++中,我可以使用以下查找和替换条件来执行此操作: Find: (Dog)|(Hat)|(Farm) Replace: (?1Cat)(?2Shoe)(?3City) 此解决方案在Mac电脑上,但我确信PC的解决方案是相同的。如果您在Sublime中拖放或打开目录: 在Mac电脑上,它的快捷键是shift+cm

我正在尝试使用升华的“在文件中查找”将多个单词替换为多个文件中的另一个单词,例如:

Dog -> Cat
Hat -> Shoe
Farm -> City
在记事本++中,我可以使用以下查找和替换条件来执行此操作:

Find: (Dog)|(Hat)|(Farm)
Replace: (?1Cat)(?2Shoe)(?3City)

此解决方案在Mac电脑上,但我确信PC的解决方案是相同的。如果您在Sublime中拖放或打开目录:

在Mac电脑上,它的快捷键是shift+cmd+f,它位于
Find->Find in Files…
下,但在查找和替换中输入:

单击“替换”,系统将提示您:

单击“替换”按钮后,它应将
foo
替换为
bar

对于单个实例中的多个查找和替换,我不记得Sublime在开箱即用的情况下这样做,但有一个您可以参考的方法可以实现您所做的:

# This script allows you to do multiple find/replace operations in one pass. The find/replace
# pairs are defined in a file (defaults to ~/.st2_multiple_find_replace) using the format
#
# find <separator> replace

# By default, the separator is the hash-pointer (=>), but it can be changed if
# necessary (see below). To use multiple find and replace, save this code to a file in
# your Packages/User folder using whatever name you like. I use multiple_find_replace.py.
# Edit your user key bindings and pick whatever key combinations you want to trigger 
# these operations. On Mac OS X, I chose COMMAND+OPTION+M E to edit and COMMAND+OPTION+M R
# to replace. The actual key bindings are:
#
# { "keys": ["super+alt+m", "f"], "command": "multiple_find_replace", "args": {"operation": "find"} },
# { "keys": ["super+alt+m", "r"], "command": "multiple_find_replace", "args": {"operation": "replace"} }
#
# When you issue the multiple_find_replace command, if the file does not exist, it will
# be created and opened for editing. If the file does exist, it will be parsed for 
# find/replace pairs and the current file will be the operation's subject. In order to
# edit your find/replace pairs, issue the edit_multiple_find_replace command, which
# will either open or activate the search term file.
#
# To change the separator used in the search term file, include a comment at the top of
# the file like this:
#
# separator: ==
#
# changing the double equal to whatever ever separator you want to use. The script will
# then use that separator to split the line when parsing the find/replace pairs.
#
# All find/replace operations are done in a single edit, so a single undo returns the
# subject text to its original state. Also of importance is the pairs are processed
# in the order they appear in the file.
#
# At present, the find/replace operations work against the entire buffer. A future
# enhancement would be to operate against a selection only.

import sublime, sublime_plugin
import os

class MultipleFindReplaceCommand(sublime_plugin.WindowCommand):
    def run(self, operation):
        self.file_name = os.path.expanduser('~/.st2_multiple_find_replace')
        self.separator_key = 'separator:'
        self.separator = '=>'
        self.lines = None

        self.ensure_multiple_find_replace_file()

        if operation == 'find':
            self.find()
        else:
            self.replace()


    def ensure_multiple_find_replace_file(self):
        if not os.path.exists(self.file_name):
            contents = "# Put one find/replace pair on each line, separated by a separator. For example:\n"
            contents += "\n"
            contents += "ReplaceThis => WithThis\n"
            contents += "\n"
            contents += "# The default separator is =>. To change it, put the following line at the top\n"
            contents += "# of this file, replacing the double equal with whatever you want the separator\n"
            contents += "# to be:\n"
            contents += "\n"
            contents += "# separator: ==\n"

            with open(self.file_name, 'a') as the_file:
                the_file.write(contents)


    def find(self):
        self.window.open_file(self.file_name)


    def replace(self):
        self.load_file()

        view = self.window.active_view()
        edit = view.begin_edit()

        for line in self.lines:
            if line.startswith('#'):
                continue

            try:
                find, replace = line.split(self.separator)
            except:
                continue

            find = find.strip()
            replace = replace.strip()

            matches = view.find_all(find)
            matches.reverse()

            for region in matches:
                view.replace(edit, region, replace)

        view.end_edit(edit)


    def load_file(self):
        input_file = open(self.file_name, 'r')
        self.lines = input_file.readlines()
        input_file.close()

        for line in self.lines:
            if line.startswith('#'):
                pos = line.find(self.separator_key)
                if pos > 0:
                    junk, separator = line.split(self.separator_key)
                    self.separator = separator.strip()
                    break
#此脚本允许您在一次过程中执行多个查找/替换操作。查找/替换
#使用以下格式在文件中定义对(默认为~/.st2\u multiple\u find\u replace)
#
#查找替换
#默认情况下,分隔符是散列指针(=>),但如果
#必要时(见下文)。要使用多个查找和替换,请将此代码保存到中的文件中
#您的包/用户文件夹使用您喜欢的任何名称。我使用multiple_find_replace.py。
#编辑您的用户密钥绑定并选择您想要触发的任何密钥组合
#这些行动。在MacOSX上,我选择COMMAND+OPTION+ME进行编辑,并选择COMMAND+OPTION+MR
#取代。实际的密钥绑定是:
#
#{“keys”:[“super+alt+m”,“f”],“command”:“multiple_find_replace”,“args”:{“operation”:“find”},
#{“keys”:[“super+alt+m”,“r”],“command”:“multiple_find_replace”,“args”:{“operation”:“replace”}
#
#当您发出multiple_find_replace命令时,如果文件不存在,它将
#可以创建并打开以进行编辑。如果该文件确实存在,将对其进行解析
#查找/替换对,当前文件将成为操作的主题。为了
#编辑查找/替换对,发出edit_multiple_find_replace命令
#将打开或激活搜索词文件。
#
#要更改搜索词文件中使用的分隔符,请在
#文件如下所示:
#
#分离器:==
#
#将double更改为您想要使用的任何分隔符。脚本将
#然后在解析查找/替换对时使用该分隔符拆分行。
#
#所有查找/替换操作都在一次编辑中完成,因此一次撤消将返回
#使文本服从其原始状态。另一个重要的问题是对数据进行处理
#按它们在文件中出现的顺序排列。
#
#目前,查找/替换操作针对整个缓冲区。未来
#增强将仅针对选定对象进行操作。
导入升华,升华插件
导入操作系统
类MultipleFindReplaceCommand(升华插件.WindowCommand):
def运行(自身、操作):
self.file\u name=os.path.expanduser(“~/.st2\u多个\u查找\u替换”)
self.separator\u key='分隔符:'
self.separator='=>'
self.lines=无
self.确保\u多个\u查找\u替换\u文件()
如果操作==“查找”:
self.find()
其他:
self.replace()
def确保多个查找替换文件(自):
如果不存在os.path.exists(self.file\u名称):
contents=“#在每行上放置一个查找/替换对,用分隔符分隔。例如:\n”
内容+=“\n”
contents+=“用此替换此=>\n”
内容+=“\n”
contents+=“#默认分隔符为=>。若要更改它,请将以下行置于顶部\n”
contents+=“#此文件,用您想要的分隔符替换双等号\n”
内容+=“#将要:\n”
内容+=“\n”
内容+=“#分隔符:==\n”
以open(self.file_name,'a')作为_文件:
_file.write(内容)
def查找(自我):
self.window.open\u文件(self.file\u名称)
def更换(自):
self.load_文件()
视图=self.window.active\u视图()
编辑=查看。开始编辑()
对于self.line中的行:
如果line.startswith(“#”):
持续
尝试:
查找、替换=行.split(self.separator)
除:
持续
find=find.strip()
replace=replace.strip()
匹配=查看。查找所有(查找)
匹配。反向()
对于匹配中的区域:
视图.替换(编辑、区域、替换)
视图.结束\u编辑(编辑)
def加载_文件(自):
输入文件=打开(self.file文件名'r')
self.lines=input_file.readlines()
输入_文件。关闭()
对于self.line中的行:
如果line.startswith(“#”):
pos=行查找(自分隔键)
如果位置>0:
垃圾,分隔符=line.split(self.separator\u键)
self.separator=separator.strip()
打破
看一看。下面是一个示例,描述了配置和使用此软件包的步骤


我需要同时对多个单词执行此操作。我想替换1000多个文件中的100多个单词。你有没有找到方法?