I wrote my first Sublime Text 3 Plugin! Granted, it's nothing overly complicated, but it will save me a lot of time. I spend a lot of time copying and pasting text from Photoshop mockups, or from e-mails, or whatever it may be. I would then always forget to double check that the apostrophes, quotation marks, and ampersands were replaced with the proper plain text or html code versions. So many times I was getting e-mails back asking me to fix that. So instead of manually changing it by hand every time, I decided to learn how to automate it for me.
Description
To get started, I followed this tutorial. It says it's for Sublime Text 2, but it still works for at least the basics. Basically, I just used that to figure out where to save the plugin and get the basic outline. I also used the end of the tutorial to figure out how to do menu entries and key bindings. It also refers you for where to go to distribute the package.
So here's all the code (up to this point) for my entire plugin:
So it's really simple. It gets all the data in self.view.sel()
, the sel()
being the selection. Then in that data, self.view.lines(r)
splits it into lines. That's not editable text though, so we convert it to text with the text = self.view.substr(line_r)
. Then, we replace each of those characters that may show up in the line. You'll notice that with the ampersand and the percent sign they are only replaced if they are immediately followed by a space. Otherwise, they would be replaced if you had highlighted a URL or something like that. After replacing all the characters, we replace the edited line with the previous line. The self.view.replace(edit, line_r, text)
lets us do that. The edit
part allows for undos in Sublime Text 3. After finishing the task, a status message is placed to let you know it worked.
Overall, a very easy process. It's not a big plugin, doesn't do a lot, but it does save a lot of time by automating the replacing of these characters. The next step was to put the command to run the plugin in a menu. It was pretty easy. You make a file in the project called Main.sublime-menu. I just wanted to put it in the edit menu at the top of the screen, so this is what I needed:
[
{
"id": "edit",
"children": [{ "id": "wrap" }, { "command": "ascii_replacer" }]
}
]
That puts an entry in the Edit menu at the top of the screen with your command name. When it's clicked, the plugin runs. Simple as that. You can also set a key binding for the command to run. Make sure that there isn't one already assigned to the key binding you choose. You need four files: Default (Windows), Default(OSX), and Default(Linux).sublime-keymap, and Default.sublime-commands. In the sublime-keymap files, you need the following:
// Mac
[
{ "keys": ["super+ctrl+a"], "command": "ascii_replacer" }
]
// Windows/Linux
[
{ "keys": ["ctrl+alt+a"], "command": "ascii_replacer" }
]
That sets the key binding for each of the three operating systems. In the last file, it specifies your keymap files and allows them to be run without the user needing to specifically set them. Here's a copy of mine:
[
{
"caption": "Preferences: Ascii Replacer Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/AsciiReplacer/Default (Windows).sublime-keymap",
"platform": "Windows"
}
},
{
"caption": "Preferences: Ascii Replacer Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/AsciiReplacer/Default (OSX).sublime-keymap",
"platform": "OSX"
}
},
{
"caption": "Preferences: Ascii Replacer Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/AsciiReplacer/Default (Linux).sublime-keymap",
"platform": "Linux"
}
},
{
"caption": "Preferences: Ascii Replacer Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
}
},
{
"caption": "Preferences: Ascii Replacer Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
}
},
{
"caption": "Preferences: Ascii Replacer Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
}
}
]
Conclusion
Overall, not too difficult of a process. after getting all that code done, fork this repo and follow the directions they have there. Once they approve your changes, and accept your pull request, your package will show up in the Package Manager in Sublime Text. You can see my repo for this plugin on my GitHub page, right here. Let people know about this if they need a plugin to do this!