Select word at current text insertion point

I’m trying to create a macro that will select the word at the current text insertion point, then perform an action on that word.

The only way I know how to do this is to double-click at the text insertion point.

Is there a way to read the position of the text insertion point so I can direct Keyboard Maestro to double-click there?

I won’t necessarily be at the beginning or ending of a word.

1 Like

Hey Dave,

In what app?

Just in general?

Do you mean the pointer — or the actual cursor (insertion point) in the text?


Best Regards,
Chris

One possibility is the key sequence: Option-Left Arrow, Option-Shift-Right Arrow.

I have had success with the very macro Peter suggests, but I also add a simple AppleScript to it that invokes PopClip. I trigger this macro using my otherwise unused eject key on my apple wireless keyboard. The AppleScript is really easy:

Tell application "PopClip"
appear
end tell  

I got the AppleScript idea from Brett Terpstra on one of the Mac Power Users episodes.

In general.

The blinking cursor or insertion point.

Thanks, I’m familiar with those keystrokes, but my insert cursor won’t necessarily be at the front or back of a word. In other words, the cursor might be at the beginning or ending or in the middle of a word. I want to select the entire word where the cursor is sitting, the same as if I double-clicked that spot.

Hey Dave,

OSX does not offer hooks to allow a utility such as Keyboard Maestro to see where the text-cursor is in any given text editing area.

So there is no way to do what you want except to brute-force the user-interface given a very specific set of initial circumstances — similar to what Peter suggested.

-Chris

Being a Sublime user I got used to being able to press cmd+D and select a word.

Problem was previously solved with David’s solution

A couple of weeks ago I nearly squealed when I discovered that it could be made possible with a little tweak to OS X

Using a text editor create the following file if it doesn’t already exist

~/Library/KeyBindings/DefaultKeyBinding.dict

In that file paste

{
  "~w"  = (selectWord:);
  "~p"  = (selectParagraph:);
  "^~w" = (selectWord:, cut:);
  "^~p" = (selectParagraph:, cut:);
}

You might need to restart Finder, or the app you want to use (for testing I was using Mailplane)

Then you can use:

alt + w to select word
alt + p to select paragraph

alt + ctrl + w to cut word
alt + ctrl + p to cut paragraph

If you don’t want all four, then edit as required.

The shortcut doesn’t work well with non native Mac apps (Photoshop being one of them)

As usual, there might also be conflicts with other shortcuts/apps and Keyboard Maestro hotkeys

For a whole lot more of these bindings have a look at http://osxnotes.net/keybindings.html

Enjoy

5 Likes

The Option-Left Arrow, Option-Shift-Right Arrow will work.

If you look the Option + Left Arrow bit just moves to cursor to the end of the word (no matter what position it is in within it)

Then adding Shift into the Option + Shift + Right Arrow will select it

… but i guess it won’t work if you are at the end.

Hey Sam,

I looked at keybindings, but I guess I didn’t look far enough.

Thanks!    :smile:

-Chris

It also won’t work if the cursor is already at the start of a word. Thanks.

Cool. I’ll check that out. Thanks.

The simple solution would be "Don't do that" and just make sure you are not at the start of the word.

But if you really want to be thorough, you could do something like:

Shift-left arrow
Copy
If Clipboard is <space>
    Right Arrow, Option-Shift-Right Arrow
Else
    Option-Left Arrow, Option-Shift-Right Arrow

Personally I would see that as overkill, and likely to be a bit slow, and pollute your clipboard.

Here is my solution to this problem based on Option-Shift-<Arrow key> combination Keyboard Maestro 8.2.4 “Select a word” Macro

Thanks! This is great!