Home Download Buy Blog Forum Support

find and replace (py)

find and replace (py)

Postby cawx on Tue Feb 05, 2013 4:55 pm

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.

Code: Select all
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)
cawx
 
Posts: 2
Joined: Tue Feb 05, 2013 4:35 pm

Re: find and replace (py)

Postby sapphirehamster on Tue Feb 05, 2013 7:03 pm

You can manually create a region that encompasses the entire view:
Code: Select all
r = sublime.Region(0, self.view.size())
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm

Re: find and replace (py)

Postby cawx on Wed Feb 06, 2013 1:05 pm

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

Code: Select all
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)
cawx
 
Posts: 2
Joined: Tue Feb 05, 2013 4:35 pm

Re: find and replace (py)

Postby sapphirehamster on Wed Feb 06, 2013 9:00 pm

Other than your indentation being off on the last 3 lines, it worked for me.
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests