Truncate Clipboard

Hey Keyboard Maestros!

I ran into a problem, that I was unable to solve myself - it’s a pretty simple task.

I want to have a specific number of characters truncated from the clipboard.

For example:

Clipboard: “Exampletext”

something like “truncate three letters from end”

leads to “Examplet”

Any idea how to solve that? Is that possible in Keyboard Maestro at all?

Thanks much!!

Julian

Hey Julian,

See the Substring of Variable or Clipboard action.

Make sure to familiarize yourself with all the different options.

-Chris

2 Likes

Amazing! Thanks so much. That’s perfect

Just checked them all out, it works great. I tried to play with “delete to” and was wondering, if it’s also possible to delete up to a certain character instead of a character count.

Something like

text: " abcdef" -> delete to “d” -> output: “ef”

just typing in the text or a variable didn’t do the trick

Hey Julian,

To do that you really need to search/replace a variable or the clipboard.

Using a regular expression with case-insensitive:

The input (excluding the quotes):

" abcdef"

Find:

^.*d(.+)

Replace:

$1

RegEx → Test KM Find & Replace RegEx.kmmacros (3.2 KB)

-Chris

1 Like

Thanks Chris for your fast response! It works great! Is there any documentation about what exactly is going on?

^.*d(x+) and $1 is pretty mysterious to me.

Besides “d” in ^.*d(x+) which seems to be the last one that gets deleted I have no idea what the rest means. What for example, if you want to do the same, but from the back of the string?

Thanks so much!

Hey Julian,

Sure. A couple of hundred books on regular expressions.   :smile:

^.*d(.+)
^  ==  Beginning of line
.  ==  Any character
*  ==  Zero or more of the previous token
d  ==  A literal character “d”
(  ==  Start Capture-Group
.  ==  Any character
+  ==  1 or more of the previous token
)  ==  End Capture group.
$1  ==  Represents the 1st (and only) Capture-Group

So I'm finding any characters from the beginning of the string until “d”, and I'm capturing the rest.

Then I replace-all with the captured text.

See an analysis here:

BBEdit/TextWrangler RegEx Cheat Sheet (Syntax mostly compatible with Keyboard Maestro.)

Keyboard Maestro's regular expression syntax is available via a menu item in the editor's help menu.

-Chris

1 Like