
You can use the “erase” method to similar effect (with the third parameter not being included). The “replace” method receives the edit token (passed as the second parameter in your run definition), followed by the variable of our region (allcontent) and the string we wish to replace it with. The last thing I need to do is replace my region contents using the following line: (edit, allcontent, 'Hello, World!') We store this region in a new variable called “allcontent.” This is achieved by using “()” which returns the size of the current sublime view being manipulated. So when I want to select all content, I feed it 0 for the start and then the length of all the content as the end. For those of you familiar with cutting a string into pieces by position, it’s the same concept. It is fed two variables: start and end position. “Region” is Sublime’s built-in function that you use to select an area. Let’s look at the following function: allcontent = sublime.Region(0, ()) To do this you will use region selection to define a region for manipulation.
#Sublime text 3 plugin how to#
The example inserts a line at the very beginning of the document instead and doesn’t show how to select. This definitely takes time to get used to, but is very powerful once mastered.įor my example, I was simply trying to select all text and replace it. Even more baffling: ST3 has “multiple cursors” in which you can select multiple areas at the same time and carry out the same task on them. The main confusion here (for me) was how you defined where text is going to be inserted / removed / edited. This is by far one of the more confusing parts of the article, but bear with me, it’s in your best interest. You will see these parameters mentioned throughout the API, so take note as you will be using them again. “Self” is our default sublime object and “edit” is the edit token which allows our interaction with ST3 through Python. You’ll notice two parameters being passed: def run(self, edit): For my project, I also needed some others for handling time, json objects and url parsing.Īt this point you should also take a moment to look at the default “run” method being created. These two imports are required for interaction with ST3 and will be the object that you perform interaction with when using the ST3 methods defined in the documentation. These declarations go at the beginning of your plugin and you will see two of them already there: import sublime
#Sublime text 3 plugin code#
They are how you include other code libraries to perform tasks that you don’t want to manually write on your own. Net or other similar languages, you will be familiar with imports. I also recommend downloading package control and a decent Python Linter to help you troubleshoot easy errors. Fortunately, ST3’s syntax highlighter is very helpful in this regard. Knowing the above will help you clear syntax hurdles faster, especially if you are used to developing in PHP or other, more forgiving languages.
