Sublime Text 3 API : Get all text from a file
Asked Answered
R

1

14


I am trying to create a plugin for sublime text 3.
For now I only get possible to select all text in a window and copy it in another window.
Code :

import sublime, sublime_plugin

class PrintCodeCommand(sublime_plugin.WindowCommand):
    def run(self):
        # for each caracter, add it to a string with the substr method
        s = ""
        for x in range(0,self.window.active_view().size()):
            s += self.window.active_view().substr(x)
        newFile = self.window.new_file()
        newFile.run_command("test",{"textBuffer": s})

class Test(sublime_plugin.TextCommand):
    def run(self, edit, textBuffer):
        self.view.insert(edit, 0, textBuffer)

Do you know a better/simpler method to do so ?
Thanks!

Redraft answered 24/11, 2013 at 22:37 Comment(0)
J
32

You can get the contents of the current doc with:

contents = self.view.substr(sublime.Region(0, self.view.size()))
Jenson answered 24/11, 2013 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.