How to? Select text between a set of brackets

Hey all, long time user, first time caller here. I'm trying to do a thing that I feel must be possible with KM but can't wrap my brain around how.

Background: I use Obsidian as well as The Archive for dealing with markdown files. As a result, I have a lot of things in various places that are links using timestamp based uids, that look like this:

[[202308300430]]

There's a bunch of things that would be a lot easier if I could figure out how to grab out just the numbers from between those brackets and put them into a variable.

Conditions:

  • I can guarantee the string will always be the same length, 12 digits
  • I can guarantee the surrounding string will always be double square brackets
  • I cannot guarantee where in a line this occurs; they are scattered liberally throughout documents.
  • Here's the hard part: I would like to be able to do this from any cursor position within the digits. I have a solution for if I'm before the first two brackets (lots of emulated arrow keys), But I would like to be moving my cursor around through the text with my keyboard, find myself in the middle of one of these links, and extract the useful info from it.

So: any suggestions for how to proceed?

Thanks in advance!

Howdy!

Here’s one way of doing it. Basically (assuming your cursor is inside the brackets), it positions the cursor behind the brackets, then selects the brackets, then extracts the digits only, displaying them in a window.

Try it out and see if it works for you. If it doesn’t for some reason, let me know and we can go from there.

Download Macro(s): Select digits inside brackets.kmmacros (5.3 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.5
  • Keyboard Maestro v10.2

-Chris

1 Like

If the curser is within the digits just double-click with the mouse and the numbers without the square brackets will be selected.

If you don't want to use the mouse, then make a macro that does a double-click, and a copy to clipboard.

EXAMPLE Double Click at Cursor Position and Copy.kmmacros (2.9 KB)

3 Likes

Thank you! This... works, mostly? In that it does in fact extract the digits and display them, but after the fact the selection in the document is expanded out to include the words on either side (and if it's at the beginning of a line, all the blank lines between it and the word in the previous paragraph). So, it works, but I'm a little concerned about the selection after the fact being potentially dangerous if I'm moving too quickly. I tried dropping in an ESC after and that kind of works, but it's not going to play well with, say, vim.

Update: The above is the behavior in The Archive. When I try to implement this in obsidian thusly:

for some reason the search results are set to the number plus the brackets, but if I then do a regular paste, get just the digits (this is likely an obsidian issue and outside the scope the the question, so I don't expect you to fix this bit, I just thought you should know, or if it was something obvious in KM that I'm missing). And also the entire document is selected.

Regardless, this is fantastic, thank you.

Thank you. I like this approach, but it's not going to work for me, as I work so keyboard centric that my mouse is always on some random part of the screen. I appreciate the effort though.

I’m not quite sure I follow what you mean... anything beyond getting the digits is outside the scope of my demo macro. But obviously you could just add a simple simulate keystroke action at the end of the macro, and simulate the right arrow to move to the end of the currently selected text.

Also, you mentioned vim... are you talking about vim on the command line?

I sometimes use vim on the command line, sometimes in a gui, so I'd like the option of being able to expand this for use there. But the most immediate use is in obsidian, where I want to be able to search for the UIDs (without getting into too much detail: The Archive does "linking" as just a search for the string in all files, which gets you everything that mentions that file as well, I want to emulate this behavior in obsidian).

And, of course, emulating a right arrow would work, this is what I get for responding before coffee. Thanks!

1 Like

Been there. Done that. Many times. :laughing:

Command line interfaces often don’t work well with keyboard shortcuts, so this may or may not work for that purpose. But at least it gives you the RegEx that you can implement elsewhere. :+1:t2:

1 Like