4

Let's say I have the following text and my cursor is on the "d"

someCamelCaseWord
                ^

I want to delete "Word" so that the text reads "someCamelCase"

What's the fewest number of keystrokes required to achieve this?

5
  • 1
    is it specifically to delete Word or you have a generic names like sampleVarNameAbc where you would like to delete last sequence of letter beginning with uppercase (Abc in this case)?
    – Sundeep
    Commented Aug 20, 2016 at 7:13
  • @spasic The more general case Commented Aug 22, 2016 at 19:13
  • Highlight with mouse, x.
    – Antony
    Commented Aug 24, 2016 at 13:27
  • @Antony, Highlight with mouse is the first thing you would want to avoid using vim.
    – Shahbaz
    Commented Nov 29, 2016 at 4:08
  • FYI: I am now using a plugin that adds a new text object and motion for camel-case sub-words. I configured the plugin such that the text object is described by the key-sequence gw. If I want to delete a camel-case sub-word, I just type digw (instead of diw, which would delete the entire word). The plugin: github.com/chaoren/vim-wordmotion An alternative plugin: github.com/bkad/CamelCaseMotion Commented Nov 29, 2016 at 20:28

4 Answers 4

7

In this specific instance, the best I could come up with is 4, including holding the shift key. There are several possibilities.

xxxx - delete the character under the cursor.
3Xx - delete 3 characters to the left, delete the character under the cursor.
3hdw - Move 3 characters to the left, delete to the end of the word.
3hD - Move 3 characters to the left, delete to the end of the line.

Other tries, with 5 keystrokes[1], that doesn't use repeated commands or movement counts:

dFWx - delete, moving left to the 'W', then delete the remaining character, since the 'd' does not delete the character under the cursor, when moving left.
FWdw - move left to the 'W', then delete to the end of the word.

1 - I only count them as 5 due to holding the shift key down for consecutive keystrokes.

5
  • FWD is only three letters. Commented Aug 20, 2016 at 5:23
  • But 4 keystrokes, counting the shift or caps lock key.
    – Herb
    Commented Aug 20, 2016 at 7:46
  • 2
    FWdw should be FWde and 3hdw should be 3hde. 3hD assumes a bit to much.
    – romainl
    Commented Aug 20, 2016 at 9:07
  • 1
    Also vFWd or v3hx and vFWc if you want go to insert mode. Commented Aug 20, 2016 at 17:53
  • Thanks for counting Shift as a key stroke. I think I'll stick to FWdw, where W can vary based on the word. It is 5 key strokes instead of 4 like the others, but I think it is has the least cognitive friction since I don't have to count how many characters the word consists of, which would be difficult if the word I'm trying to delete is long. Commented Aug 22, 2016 at 19:25
1

One.

Press x and hold. Wait until it deletes 4 characters, then release it.

6
  • Having mixed feelings about this solution. I'd say OK in this case. But what if ThatIsAVeryLongCamelCaseName which can happen ? Plus it's kind of like using backspace in other editors. Commented Aug 20, 2016 at 9:12
  • 6
    I'm not feeling that the 'press and hold' is a great solution, it minimizes the number of different keystroke, but anything that require "holding" a key seems nonoptimal.
    – nobe4
    Commented Aug 20, 2016 at 9:22
  • Of course press and hold isn't a great way to delete characters. It's a dreadful way to do it. But the request wasn't for a sensible way to do it, it was for the shortest way to do it, purely in terms of keystrokes. And one keystroke is going to be hard to beat. Commented Aug 21, 2016 at 10:55
  • It's not the shortest if your SSH link is flaky and you have to clean up after it.
    – Kevin
    Commented Aug 22, 2016 at 23:45
  • If you delete one or two characters too many or too few, you can press u or x two or three times, and still do as well as or better than any of the 'sane' options. (defining better only in terms of number of keystrokes of course). Oddly, it's easier to get it right first time if you are working on ThatIsAVeryLongCamelCaseName because once it starts deleting you have longer to work out when to lift your finger. Again, this isn't a recommendable way to delete 4 characters, just one that uses the minimum possible key strokes. Commented Aug 23, 2016 at 3:07
1

As you said, your cursor is positioned at d. Thus with ?W you can jump the cursor position to W and with cw or dw you can remove Word.

In total this is 4 keys pressed, but this saves time on counting characters needed for something like multiple x keypresses or 4h. Especially will be useful for targeting the capital letter of camelcase words. For instance , with public static void PaymentsSubmittedToday string and cursor at y , we can just do ?S then dw, and the string becomes public static void Payments with cursor at s

-1
dFW

Search backwards for the fist W, and delete everything up-to and including it. d means delete and FW is a motion command that means move the cursor backwards to the first encountered W character.

1
  • This doesn't work, as it will not delete the character under the cursor, in this case the final 'd'
    – Herb
    Commented Aug 22, 2016 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.