MacOS – How to easily select the word the cursor is currently inside

keyboardmacos

I'm looking to be able to select the current word my text cursor is inside on OSX (10.7.3).

For people who have used vim before, I want to be able to do a similar thing to the 'ci_' command, where '_' can be 'w', '(', '[', etc. to specify deleting the word or contents of a parens/bracket and enter insert mode.

The best way I've figured to do this is Option+Left then Shift+Option+Right to first navigate to the left end of the word, then select to its right end.
Is there a way I can create a macro to do both of these at once?

Best Answer

This has been covered in a screencast on the Ask Different Blog. I encourage you to watch the video, but I'll summarize here.

First, create a little AppleScript that presses the two keyboard shortcuts. Open AppleScript Editor (in /Applications/Utilities) and paste this in:

tell application "System Events"
    key code 123 using option down
    key code 124 using {shift down, option down}
end tell

Save that as a script in ~/Library/Scripts (create the folder if it doesn't already exist).

Now, install the app FastScripts. It's free for up to 10 scripts.
Open the FastScripts preferences, find your Select Word script, and assign a keyboard shortcut to it.

Now you can press your chosen shortcut in any app and the current word will be selected.