Sublime Forum

Run a method on the content of a file buffer

#1

Hi,

I do not really want to go in plugin development, but I have some python scripts that I would
like to execute on the content of a file in a window by pressing a combination of keys. In Textmate this was rather easy, the
content of a window could be passed through an environment variable and the output could be redirected to a new
window. I am puzzled how to
do this in Sublime. Can this only be done through a plugin? The API looks interesting but daunting
at the same time. Could someone complete the missing
lines below? Or forward me to a tutorial?

from sublime, sublime_plugin 

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

	     s = *content of window*
             replace content by f(s) where f is a function written in Python by me

thanks,
Frank

0 Likes

#2

…maybe something like this (untested):

from sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
   def run(self, edit):
        region = sublime.Region(0, self.view.size())
        file_contents = self.view.substr(region)
        corrected_text = your_function(file_contents)
        self.view.replace(edit, region, corrected_text)
0 Likes

#3

Thanks! This works perfect.

0 Likes