Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created December 21, 2020 07:28
Show Gist options
  • Save OdatNurd/7d1feb497bf40753377700e615d34925 to your computer and use it in GitHub Desktop.
Save OdatNurd/7d1feb497bf40753377700e615d34925 to your computer and use it in GitHub Desktop.
Sample plugin showing how to edit, replace and erase text in a Sublime Text plugoin
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
class SecondExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
span = sublime.Region(0, len(self.view))
self.view.replace(edit, span, "Hello, World!")
class ThirdExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
pt = self.view.sel()[0].b
line = self.view.line(pt)
# line = self.view.full_line(pt)
self.view.erase(edit, line)
class FourthExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
spans = self.view.find_all('Command')
for span in spans:
self.view.replace(edit, span, 'Cmd')
class FifthExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
adjust = 0
diff = len('Command') - len('Cmd')
for span in self.view.find_all('Command'):
new_span = sublime.Region(span.a - adjust, span.b - adjust)
self.view.replace(edit, new_span, 'Cmd')
adjust += diff
@OdatNurd
Copy link
Author

OdatNurd commented Dec 21, 2020

This is the example plugin from Lesson #19 in Plugin 101 for Sublime Text, [https://youtu.be/8OYtWOdw3TY](How do plugins modify the text in a file?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment