Hi Antonio
Here is some code that I wrote for myself to do a multiple find and replace. I’m sorry it doesn’t have any comments (shame on me!), but I think it should be pretty self explanatory. Basically, if there is a selection, use it, if not, select the whole document. The key is to reverse the selection regions and process them backwards. When you find/replace, the offsets of the selections change and it is pretty hairy to keep them straight. Working backwards avoids that problem.
view = self.view
if view.has_non_empty_selection_region():
self.operation_regions = view.sel()
else:
self.operation_regions = [sublime.Region(0, view.size())]
operation_regions = ]
for operation_region in self.operation_regions:
operation_regions.append(operation_region)
operation_regions.reverse()
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()
for region in operation_regions:
region_as_string = view.substr(region)
if region_as_string.find(find) > -1:
view.replace(edit, region, region_as_string.replace(find, replace))
view.end_edit(edit)
try:
self.operation_regions.clear()
except:
pass