Sublime Forum

Word boundary definition

#1

I would like to be able to customize word boundaries for the behavior of ctrl+right, etc. Where can I write something to do this? I’m perfectly happy to write regex or python to do it if needed, I just can’t figure out where to do that. I have already edited the word_separator variable and it had no effect on what I’m trying to do.

I want ctrl+right to move me an entire word, not counting extra word boundary characters. Examples:

  • In “abc - def”, I want the first press to move past “abc”, and the second to move past “def”; I do not want to need a third press to move one character past the “-”.
  • If my cursor is in the middle of several empty lines, I want ctrl+right to move me to the next word, not to the next empty line.
  • In a list of quote strings, I want ctrl+right to move past each word and not also stop on each quotation mark separately

I found the following post here asking more or less the same thing, but no one answered:

Google is useless because I only get results about word counts and spellcheck. ChatGPT either talks about word separators, or basically says “that’s complicated do it yourself”.

0 Likes

#2

For something like this you would (as far as I am aware) require a custom plugin. That would be something that implements a TextCommand and takes arguments that specify what direction to move. When the command is invoked it can examine the content of the buffer and move the selection to the required place; you then replace the bindings on the current keys with your own command.

An example of such a thing is the following; I don’t know if it will suit directly for the purpose but it’s an demonstration of a command that does something like this.

As a side note, there can be an arbitrary number of cursors (or selections, in Sublime parlance); so to be fully workable you would want to either make sure that the command updates all of the selections appropriately, or that you adjust the key bindings to apply only when there is a single selection.

0 Likes

#3

Thanks for the reply! I’m sure I can figure out the rest of what I need from here once I get the plugin to do anything at all. But I have tried everything I can think of or find online, and there seems to be no way to actually get a plugin to run, in any form.

  • I’ve replaced the entire contents of run with print('hi') to make sure that’s not the issue. Once I get that ‘hi’ to appear a single time, my entire problem is solved.
  • I’ve put the file in each of Packages, Installed Packages, and Packages/User (both in turn and all at once, restarted ST in between changes).
  • When I save the file, I get “reloading python 3.3 plugin WordDefinition”, and when I introduce syntax errors, ST yells about them. So ST is definitely seeing the file in some way.
  • view.run_command("WordDefinition ForwardWordJump") in the console does not print, nor any variant of the command name I could think of. It doesn’t error either, just does literally nothing.
  • Adding the command to a keybind also does not print anything, just does nothing.
  • I’ve updated to the latest version and tried running ST as administrator. No differences.
  • If I, in the ST console, manually import WordDefinition and WordDefinition.ForwardWordJump.run(), then I get the ‘hi’ to print. So the code exists and can be run. But it apparently cannot be run from a keybind or view.run_command no matter what arrangement of the names I try.

What simple step did I overlook to make the easiest part completely impossible? I’m not sure if this is better as a new thread, but I figured it won’t be very helpful to anyone reading in the future so I’d just put it here; I can move it to a new topic if that would be better.

0 Likes

#4

See the videos below for more details (second video skips directly to how command names work), but in a nutshell, your main issue at this point is that the name of a command is a snake_case version of SnakeCaseCommand ; so the reason you can’t trigger the command is because the name you’re using to do it is not correct.

0 Likes

#5

That was it, thanks. I want to emphasize that I appreciate the fast and direct replies, and the videos are clear and to the point. I just wish documentation didn’t explicitly steer me in the wrong direction

0 Likes