Implementing a "setting the mark" like command in Emacs in Keyboard Maestro

In Emacs, by presing ctrl+space one can set the mark, which takes the text field into a selection mode; this is an alternative way of selecting the text without using the mouse (where we select the words while pressing the left mouse button and moving the cursor and so on). For example, once the mark is set, pressing left arrow will keep selecting the text to left, pressing right arrow will keep selecting the text to the right and so on until the mark is deactivated by pressing ctrl+g.

I would like to achieve a "setting (or unsetting) the mark" like command in Keyboard Maestro, where I press some key combination which takes the text to the selection mode, then I modify the text (cut, copy the selected text etc) and then finally press some another key combination to get out of the selection mode. I am new to Keyboard Maestro, so any tips/suggestions on how to achieve such a "setting and unsetting the mark" like command will be much appreciated.

KM can achieve some amazing things, but sometimes it's limited by what the app (either its user interface or its AppleScript support) or the macOS GUI permit.

Are you trying to make this work only inside the "KM Editor" app, or in some other app? It's not clear to me where you want this to work. If you want it to work in other apps, you probably need to name the apps.

There are some wizards on this forum who might prove me wrong, but in general I don't think this is possible with the macOS GUI. If it works in Emacs, that's possibly because Emacs is using undocumented APIs for keyboard input and text selection. KM limits itself to documented API calls only.

Thanks for your input @Airy . Yes, I am trying to make it work only in document processors for latex such as TeXstudio, TeXmaker etc.

I agree with @Airy; I'm not sure this one is solvable with Keyboard Maestro. One thought is that you'd have the macro hold down the Shift key once you invoke the macro. Then, in theory, each time you pressed the arrow key, another character would be selected.

But the reality is that simulated keys are not real keys, and it doesn't work that way, so all that happens is the cursor moves but no selection is made.

The other big problem is that the location of the text cursor is typically invisible to other apps, including Keyboard Maestro. You could create a mark based on the mouse location if clicked at that point, but now you're back to using the mouse.

As a general comment, though, Shift and the arrow keys should work almost like Emacs' "setting the mark" command. You can't use it for non-contiguous selections, but other than that, it selects backwards and forwards from the moment you held the Shift key down, effectively making it work like a mark. Does that not work in the apps you use?

-rob.

I see, okay that could be a nice solution thanks @griffman ! Is there a way in KM where I press some key combination and it corresponds to the Shift key beings pressed, while I select the text? In that way, I could just select the text, without needing to keep pressing Shift? And then may be after the selection, copy/cut etc done, I press another key which stops Shift being pressed. That would more than suffice for my purpose. Probably this is a very trivial question, but I am very new to KM hence asking.

Unfortunately, no - that's what I said above: A simulated keystroke is not the same as an actual keystroke. You can have an action that says "Press and hold the Shift key," but even with that in place, then pressing the arrow key won't select the text.

-rob.

There are at least several third party tools enabling some system-wide Vim mode (sorry, not emacs) with the possibility also of mouse-free text modifications (like https://kindavim.app/).

Here's one way you could do this with Keyboard Maestro.

You basically have a Macro Group which has 4 Macros in it. 2 of those Macros simply replicate ⇧→ and ⇧← when you press the single arrow keys. The other 2 Macros just toggle those Macros on and off (enabling and disabling the "set the mark" mode you are after).

Pressing βŒƒSpacebar enables the "set the mark" mode.

So, in any App you press βŒƒSpacebar and then can use β†’ or ← to select single letters forward or single letters backwards, which I think is what you are after.

Pressing βŒƒG gets you out of the mode.

I've no idea how practical this would be in real-world use, but it seems to work.

Set the Mark v01 Macros.kmmacros (7.0 KB)

One of the "mode" Macros looks like this:

Click to Show Image of Macro

And the Macro it is enabling looks like this:

Click to Show Image of Macro

5 Likes

Yes, this works great for my purpose, especially set the mark on and off macros. Thanks very much @Zabobon !

1 Like

@Zabobon one side question: is there a way I can simulate arrow keys in a similar manner in KM when it comes to selecting the text after I have set the mark using macro by @Zabobon? For example, I implemented the following in KM: ctrl+i, ctrl+k, ctrl+j, ctrl+l to simulate up, down, left, right arrow keys respectively. I was able to implement them in KM as follows

and so on. They seem to work.

However, when I tried the macro by @Zabobon to set mark, these keys do not work to select text. However, if I press the left and right arrow keys on the keyboard directly (without simulating them via the macros that I just defined), the macro by @Zabobon works as intended. Am I doing anything wrong? Any tips will be much appreciated.

I am a bit confused by what you are asking. Presumably if you want to select text you need to simulate Shift + Left Arrow rather than just Left Arrow?

Sorry for not clearly explaining the issue. So, once I set the mark (using the macro Set the Mark v01 Macros.kmmacros ), if I press left arrow or right arrow key directly on the keyboard, it selects text on left and right side as expected. Now, I have also created key combinations on KM to simulate the arrow keys (eg ctrl+j to simulate the left arrow), so I was expecting that once the mark has been set, rather then pressing the left arrow key on the keyboard, if I press ctrl+j, it should select text on the left via KM. However, setting the mark, and then pressing ctrl+j does not select text (whereas pressing left arrow key directly on the keyboard does it). Hope I was able to explain the issue and please let me know your thoughts @Zabobon ! Thanks again.

Ah okay. So, when "Set the mark" is enabled you want to be able to use either← or βŒƒJ to select left? And eitherβ†’ or βŒƒK to select right?

The simplest way to do that is to include them as alternative triggers in the Macros.

(You mention Ctrl but you actually are using ⌘ in your screenshot so, I have also included ⌘J and ⌘K in the new example.)

Disable or delete the original v01 Group of Macros and try this:

Set the Mark v02 Macros.kmmacros (8.0 KB)

But is it not simpler just to get used to using ⇧← and ⇧→ to select text left or right without needing any Macros or any special modes?

Yes, this works well for me, thanks @Zabobon . You are right, perhaps it is better just get used to using shift+arrow keys to select text left and right. Thanks again!

1 Like