Sublime Forum

Find and replace (py)

#1

Hello together,

I try to write a Python code, which find all the stinrgs and replace it with an other. This code works, but I must select all the strings manually. The best solution will be, I push my defined key and the Python script find and replace all my defined strings.

Please can some one help me with some examples or snippets.

import sublime, sublime_plugin, re

class DmVarReplCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        REPL_STRING = "PAC"
        for region in self.view.sel():
            content = self.view.substr(region)
            content = content.replace('SEARCH_STRING', REPL_STRING)
            self.view.replace(edit, region, content)
0 Likes

#2

You can manually create a region that encompasses the entire view:

r = sublime.Region(0, self.view.size())
0 Likes

#3

I try this, but it don’t work, is the that code snippet correct?

import sublime, sublime_plugin, re

class DmVarReplCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        REPL_STRING = "PAC"
        region = sublime.Region(0, self.view.size())
            content = self.view.substr(region)
            content = content.replace('SEARCH_STRING', REPL_STRING)
            self.view.replace(edit, region, content)
0 Likes

#4

Other than your indentation being off on the last 3 lines, it worked for me.

0 Likes