1

The surprising extensibility of Sublime Text

 2 years ago
source link: https://benfrain.com/the-surprising-extensibility-of-sublime-text/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The surprising extensibility of Sublime Text

05.09.2022 0 comments
0 days since last revision. Content should be accurate.

I’ve been using Sublime Text since 2012. I’ve also, fairly recently, released a video course on it, ‘Ultimate Text Editing Productivity with Sublime Text’, so I feel fairly confident using Sublime effectively.

I am however, a complete newbie when it comes to extending the capability of Sublime beyond installing packages I want and tweaking the settings.

Since 2014, I’ve also spent long periods using Vim, and later Neovim. I find both Vim/Neovim and Sublime Text incredibly powerful and effective text-editors, and personal development environments. When I switch from one to the other, there are some things I miss in Neovim that Sublime has, and vice versa.

Things I missed in Sublime

When I switched back for a stint on Sublime recently, there were only really two little things I really missed; the ability to amend the shape of a selection, and the ability to skip through text in markdown files sentence by sentence.

Let me show you what I mean. Here I am, selecting an object in Sublime, but I overshoot how far I want to go as I expand out and want to tweak my selection. With my cursor at the bottom end of the selection, there is no way back to tweak the selection bounds at the top.

Well, it turns out, when you know what you are doing with Python, its pretty simple to get what you need. I asked about this on the Sublime Text Discord and someone provided the following.

import sublime
import sublime_plugin

class ReverseSelections(sublime_plugin.TextCommand):
    def run(self, edit):
        for reg in self.view.sel():
            self.view.sel().add(sublime.Region(reg.b, reg.a))

So, all you have to do, is go Tools > Developer > New Plugin, and you will get a file with some scaffold code. Replace that with this snippet, save it as ‘reverseSelection.py’ in the default location it goes to when you save (your ‘User’ folder in Sublime).

And that’s all you need. To make use of this, add a keymap that references it:

{
    "keys": ["ctrl+alt+shift+o"],
    "command": "reverse_selections"
},

Note that in Sublime, the naming of the function in python that is ‘snake case’ is referenced all lower case, with an underscore between the words so ReverseSelections = reverse_selections.

Now when you make a selection, if you overshoot on one end, you can hit your shortcut, and the cursor is at the opposite end of the selection!

Sublime-select2

A more difficult problem

I then got it into my head I would solve the next problem myself [narrator, “He didn’t”].

The short version of the problem it that when editing text and markdown files, I would like to be able to skip from sentence to sentence. This is table-stakes stuff for emacs/Vim but I miss it in Sublime.

So I asked for advice on the Sublime Text forum on how to go about it.

Turns out I was able to adapt another existing Python file to do what I needed. Here was the file in question: https://github.com/STealthy-and-haSTy/SublimeScraps/blob/master/plugins/pattern_navigate.py.

It’s well commented so even for a non-Python user, I could understand, broadly, what it was doing. I get to define a regex and pass that to the pattern_navigate function, and that then becomes the manner in which then plugin jumps forward or backward through the text.

For my use case of wanting to go backwards and forwards through sentences, after a half hour or so building up a suitable regex it was then a case adding a key binding and passing the regex to it:

{
    "keys": ["alt+pagedown"],
    "command": "pattern_navigate",
    "args": {
        "pattern": "[.?!]\\s|\\n|\\Z",
        "forward": true,
        "ignorecase": false,
        "literal": false,
    }
},
{
    "keys": ["alt+pageup"],
    "command": "pattern_navigate",
    "args": {
        "pattern": "[.?!]\\s|\\n|\\Z",
        "forward": false,
        "ignorecase": false,
        "literal": false,
    }
},

And now I also have the ability to skip back and forth through text. Obviously you can tweak this to your own use-case.

Where to find out more

There is lots of info on creating plugins at Odatnurd’s Youtube playlist on plugin development.

You can also read the API at the Sublime site.

It’s an old post (over 10 years) but this is still very insightful post by Will Bond of packagecontrol.io fame: https://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin–net-22685

Summary

Sublime Text remains one of the most polished pieces of software I have used in my career. For accessibility and performance I think there is no better proposition and it’s a shame its extensibility is somewhat under-reported and documented. There are plenty of resources around to extend it’s capability without sacrificing the performance that makes it so enamoured by its users.

If this post was useful, Say thanks with a coffee. Every donation helps keep me going! 🙏


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK