Delete word under cursor

In VIM you can do this : http://stackoverflow.com/questions/1379198/how-to-delete-a-word-and-go-into-insert-mode-in-vim

Delete word under cursor. Can I do the same with KM and do it really fast?

If here :

Pressing the hotkey would do this :

If here :

Pressing hotkey would do this :

If here :

Pressing hotkey, would do this :

Thank you for any help.

You don't really need KM.

If the cursor is in the middle of the word, or at the end, like this:
here is a wo|rd in a ilne
here is a word| in a ilne

Then OPT LEFT-ARROW, SHIFT OPT RIGHT-ARROW
selects the word, and of course DELETE deletes it.

If the cursor is at the START of a word, like this:
here is a |word in a ilne

Then SHIFT OPT RIGHT-ARROW selects the word.

Of course, it is easy enough to write a KM Macro using "Type a Keystroke" to automate this, if you really want to.

Also, double-click on the word selects it.

Honestly, unless you really need to do this many times a day, I don't think it is worth the effort to automate. I try not to type words that I want to delete. :wink: Just my opinion.

1 Like

Oh yeah. I forgot about these handy hotkeys. Thank you. :heart:

Here is the macro I wanted to use nearly complete :

What do you think the best solution to solve this case would be. Right now it works perfectly, only misses that one case.

Since your hands are already on the keyboard, just arrow-right one char.

If you want to automate, select one char to the left, copy, and test to see if it is a space, or other word-boundary character.

How can I check for a space?

Also this seems to be quite inefficient and would take time but I guess there is no other way for KM to know that it is at the beginning of the word. :frowning:

@nikivi, this is what I mean about not being worth the effort.
I started to write a RegEx that would check for any character that you might consider a word boundary character (more than just spaces). So it gets complicated in a hurry.

You're looking at the word. Just move the cursor if it is at the beginning of the word, or just press SHIFT OPT RIGHT-ARROW.

There are going to be many exceptions that will NOT work with your macro.
For example, characters like -[]{}* etc are NOT considered "word" characters by most systems. But you might. So if you want to delete a hyphenated word, it will fail most likely.

For example, I would consider "RIGHT-ARROW" to be one word, but the Mac OS does NOT.

Sorry, but I just don't have time right now to develop this for you. :wink:
As @peternlewis sometimes reminds me, "This is a fool's errand."
No offense meant -- it is just an expression meaning likely will not work out.

If you're lucky, one of the RegEx gurus will jump in and help.

1 Like

Donā€™t use Copy to Named Clipboard", just use Copy. No named clipboard required (and note that Default Clipboard is a Named Clipboard, different to the System Clipboard).

Use test condition

If The clipboard is not " " (just type a space).

But honestly, I would not do this - messing around with the clipboard etc just to work in one case is unlikely to be beneficial. Just accept that the macro works only when the cursor is within* the word and use it appropriately.

It is a shame that there is no Apple API to get the current selection, but that is just something you have to accept when automating on the Mac.

@peternlewis, this is a constant source of confusion. It confused me for a long time.
I think most users interpret "Default Clipboard" as what we know as the "System Clipboard".
Use of the word "Default" is a bad choice of words.

I suggest that you rename this to something like "Default KM Named Clipboard", if, in fact, it is really necessary to have this Named Clipboard at all. Why not just eliminate it?

1 Like

I am aware.

1 Like

####For two of your three cases there is already a one-keystroke key combo:

a line with| words

āŒ„āŒ«

a line |with words

āŒ„āŒ¦

Note that you donā€™t have to select the word before hitting Delete.
If your keyboard does not have the Forward Delete key you can get it by typing fnāŒ«

####The cursor-in-the-middle case requires two keystrokes:

a line wi|th words

āŒ„ā‡¢ + āŒ„āŒ«

However, this is not a big thing, since your left finger can rest on the āŒ„ key.
Or, you could easily write KM macro for it.

2 Likes

I donā€™t think this is a Mac-specific thing. For example in Regular Expressions a Word Character (\w) is defined as [A-Za-z0-9_], that is, the underscore is a word character, the hyphen is not.

But in many apps you can configure what are word characters and what not.
ā€Œ

Besides that, there is one especially annoying thing with word characters:

A dot by itself (= not followed by a space) is ā€“ by system default ā€“ not treated as word break. In many cases this can be very inconvenient:

rangeOfFourItems.firstValue
www.example.com

If you double-click one element the whole line will be selected. (Except in editors which are overwriting the global behavior.)
ā€Œ

I learned that there are two ways to change this:

  • defaults write .GlobalPreferences AppleTextBreakLocale en_US_POSIX

orā€¦

  • go to System Preferences > Language & Region and set the Region to United States (Computer):

(It is possible that the two options do not exactly the same, but I havenā€™t investigated further.)

This quick macro should cover all three cases (cursor at beginning of a word, at the end, or in the middle):
(ā€œquickā€ = ā€˜not thoroughly testedā€™, ā€˜may delete your dataā€™)

Delete Word.kmmacros (4.6 KB)

Set the Regex match to your personal set of word delimiters.

The ā€œDeleteā€ in ā€œSimulate keystroke āŒ„Deleteā€ is the āŒ« key (Backwards delete).
ā€Œ

Please note the Abort and Timeout settings for the Copy action:

This didnā€™t seem to work for me.

What exactly doesnā€™t work? (Try it in TextEdit; I didnā€™t test it in other apps.)

Just tried it in textedit too. It just goes to end of word. :frowning:

Set the timeout of the Copy action to 1s or more (just for trying). Maybe your computer is so slow that the action times out before anything was copiedā€¦

Almost sounds as if the Delete action (the penultimate action of the macro) doesnā€™t work for you. I donā€™t know whether the Type Keystroke actions are keyboard-dependent. I donā€™t think so, but try to set up that action again on your computer. As said, it must be Option-Backspace. (Backspace = Delete to the left)

Yeah I think that was the issue. My option + delete was rebinded to something else. Still though it doesnā€™t seems to work.

Itā€™s okay Tom, my implementation works great for me too. If I am in front of the word, I will just move one arrow to the right. This is a pretty useful macro for me, hope anyone else agrees with me. :slight_smile:

I am curious do you use any of these text manipulation macros that you care to share with the world? :slight_smile:

A pity that you give up :wink: Itā€™s certainly a very tiny detail that prevents it from working on your computerā€¦

I am curious do you use any of these text manipulation macros that you care to share with the world?

This one probably not. Simply because I like to keep things simple, and a simple āŒ„āŒ« is comfortable enough for me :wink:. I just was curious to see how it could be realized with KM.

Thanks for posting those keystrokes, @Tom. I had seen them before, but forgot about them until now.
Very simple and elegant.