VIM-like "Select Inner" with KM?

Can anyone think of a way to create a text selection that begins and ends with the word* that is "under" the I-Beam.

tl;dr:

Mirror VIM's "Select Inner" motion (via the concept of "Text Objects") to create a text selection.

(*Not necessary a single word, but at least, the characters between a start and end space character.)

Text Objects and Motions in VIM:

Why?:

I already have a macro which takes the currently selected URL (in say, a text file), copies it and is passed to the Open URL KM action.

I was trying to feed my laziness* by having Keyboard Maestro automatically recognize the start/end of the URL (that surrounds the I-Beam) for me and create the selection between those points.

(*With the tacit acknowledgment that I will likely spend more time investigating how to accomplish this than I will save.)

As a result, the I-Beam could be anywhere within the URL string, and I could go to that URL with a single keystroke trigger.

I can imagine this type of functionality being useful for any macro that relies on grabbing some text before Keyboard Maestro does some work on it.

Also: I've been getting into VIM and this is a fundamental (and brilliant) motion.

What I've tried:

  • I experimented with control-right/left arrow keys. Which could conceivably work with single words. But it won't work with a complex URL.

  • I looked into various RegEx ideas (matching URLs, using word boundaries, etc.). But I don't think any of those will work, because to use them in KM, you have to first feed it some text via a selection & copy. And what I'm looking to do is the step just before that. Essentially Step 0.

  • I searched the forum and couldn't find anything similar.

  • In Sublime Text 3, command-d (select word) and command-shift-space (select words enclosed in quotes) basically do it:

    • https://stackoverflow.com/a/11712061
    • command-shift-space seems to do a great job with URLs!
    • But this only works in Sublime Text 3. Not, for example, TextEdit.
    • Maybe since ST3 is able to accomplish this, it's a good indication that Keyboard Maestro would be able to as well?

Misc:

  • VIM allows you to go further and specific the "boundary" characters. It would be good to be able to select boundary characters other than a space. But it's not completely necessary to be able to do that in real-time via KM. A macro with predefined boundary characters would be fine.

  • I can imagine this being something Keyboard Maestro could handle (either it already can or as a feature request). But I could be wrong.

  • If anyone knows of a standalone utility that mirrors VIM commands like this, or something built into the OS that does this, I'd love to know about it.

  • Sublime Text and other text editors have VIM modes or plugins that seem like they might be able to accomplish this (like what I found above). But then it would only work in those editors. Not terrible, but it would be best if it was system wide.

  • Implementation idea: Move cursor to X character

    • I know @peternlewis likes Keyboard Maestro's actions to be fundamental (so they combine well with others and don't overlap), so a select inner word action is probably too specific.
    • Maybe the following is better:
      • Move cursor to closest next \s (using RegEx "space" meta-character)
      • Move cursor to closest previous \s (using RegEx "space" meta-character)
    • Or even simpler: Move cursor + closest next/previous tokens ??
    • But I'm probably getting ahead of myself. I wouldn't be surprised if either of these is outside of the scope of KM. It was just a thought.

Thanks!

2 Likes

BBEdit's Balance Tags will select everything between the tags that contain the cursor.

Thanks for this!

I don't normally use BBEdit but it looks cool. Although from this video and reading the docs, it doesn't look like it would match to space as a boundary. Only text wrapped in quote, angle bracket, curly braces, etc.

I tried it in TextWraggler, on a plain text file and html, and it wouldn't stop beeping at me :slight_smile: I couldn't get it to work.

But thank you!

I was thinking about this some more and I'm probably very wrong. ST has access to all of the text because it lives inside of its buffer. Which gives it a meta understanding of the characters in a way that Keyboard Maestro can't.

This is probably why VIM can do this (and a million other things) too.*

(*And by "too", I mean: "About 20 years before Sublime Text did it.)

1 Like

Beeping in BBEdit is SUPPOSED to mean that there is no closing tag for the tag that's left of the cursor. Are you sure there was a closing tag when you got the beep?

Yes, I tried a few different things like that. None worked. It's ok though. I can try to figure out what's going on with that later. I don't use BBEdit/TextWrangler that much :slight_smile:

Thanks!

One of the reasons I like BBEdit is that it has integration with Keyboard Maestro, not that I’ve used that yet.

Hi @ar-km. Did you find a solution for this? I'm trying to get KBM to do vim and emacs like wherever I write. I can't do "go to next sentence" as well, for the same reason I guess that you can't do "inner element". I would really love to get this done.

Hey Yuval,

It's unlikely we'll ever have this ability system-wide, because Apple doesn't provide granular hooks into the text engine of macOS – and then there're the apps that don't use Apple's text engine.

macOS does provide support for EMACS keybindings up to a point, although you have to want it to go the the trouble of using it.

It's not well documented at all...

https://www.google.com/search?q=macos+emacs+keybindings&oq=macOS+emacs

-Chris

Hey Chris @ccstone ,
Thank you for that info. You probably saved me a lot of time and heartbreak trying to do impossible stuff.

2 Likes

Sorry for not replying sooner.

Thanks for your reply. It's nice to know when other people are looking for something similarly odd :slight_smile:

I haven't unfortunately.

Sometime after my original post I had the idea:

What if the entire OS was accessible via Vim-like keyboard control.

I started to do some research into it and this is what I found:

https://news.ycombinator.com/item?id=16395379

I haven't been able to test those yet, but I'm looking forward to it.

If you get a chance to play with them, please report back what you think :slight_smile:


I wonder what Apple's thinking about this would be.

  • On one hand: "Hell naw! Vim is WAY too advanced to be grafted onto the macOS UI"
  • On the other: "Yes! This would be an amazing way to cross a Power-User feature with an Accessibility feature. Win-win."

Speaking of that, they announced "Vim mode" for Xcode 13 at WWDC 2021 .

Vim mode
Many common key combinations and editing modes familiar to Vim users are supported directly > within the code editor, using the new bottom bar to show mode indicators.

Via: Xcode 13 Overview - Apple Developer

Quick video demo:

Obviously this is an easier sell (within Apple) because this is a developer's tool. And it basically just brings Xcode in line with Sublime Text, BBEdit, etc. Since they have Vim bindings. But with the same limitations I mentioned originally:

I agree with all of this.