20

Apologies if "composite" is not the technical term. I mean the following:

Hi, I-am-a-composite-word and we are not

I would like to delete only the composite word. In command mode, if I move the cursor to I, then repeating dw a few times, or better typing dw once, followed by a few . presses, will do the trick. However, for various reasons I find myself doing this quite often during the day, thus I was wondering if there's any simpler way.

PS I would NOT consider the command d9w to be a simpler way. Counting a long sequence of words and dashes is not my idea of "simpler".

33

What you are calling a composite word is actually a WORD (by opposition to a word). Reading :h word and :h WORD should be helpful:

                            *word*
A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space (spaces,
tabs, <EOL>).  This can be changed with the 'iskeyword' option.  An empty line
is also considered to be a word.
                            *WORD*
A WORD consists of a sequence of non-blank characters, separated with white
space.  An empty line is also considered to be a WORD.

What you are looking for here is dW when you are on the I or diW when you are anywhere in the word.

Maybe in the future you will also need to read :h 'iskeyword'.

  • 1
    thanks for the answer: it works. Apropos of the definition: A WORD consists of a sequence of non-blank characters, separated with white space. Separated with white space means that the WORD is separated by other words or WORDS with white space, right? – DeltaIV Nov 5 '18 at 10:22
  • 2
    @DeltaIV Yup in-this-sentence there are four"WORDs the 4 WORDs are in-this-sentence, there, are and four"WORDs. It's easy to see when you use w and W or b and B motions for example. Note that a whitespace can be a "regular" space or a tab character. – statox Nov 5 '18 at 10:29
  • 1
    This answer would be more immediately useful if it started with "What you are looking for here is dW .." rather than forcing the reader to wade through to the middle paragraph to find the solution. – Mark Meuer Nov 6 '18 at 19:52
9

Another more general solution is to delete up to the whitespace character using dt (with a space after the t), which means "delete to [character]. This often is useful for things like dt: and similar as well.

  • what does dt: do? – DeltaIV Nov 5 '18 at 15:13
  • 1
    When I posted the same answer, I learned that you can use <code>dt </code> to include a space on the end. – BurnsBA Nov 5 '18 at 15:16
  • 2
    @DeltaIV Actually what you really need is to read :h motion.txt and to use vimtutor, that will greatly help you to get the basics of Vim – statox Nov 5 '18 at 15:18
  • @statox I used to be skilled at VIM...10 years ago :-) vimtutor sounds very interesting. I'm not sure it's installed on the remote server (it looks like not even the man utility is installed). However, I'll check: if it's automatically installed together with the vim package, then chances are high that I will be able to use it. – DeltaIV Nov 5 '18 at 17:44
  • 1
    @BurnsBA Thank you, that's really useful for this site. Btw. you can edit posts from others and even get +2 reputation if your edit makes it through some peer review process. But this way, I learned something new as well :-). – allo Nov 6 '18 at 9:02
4

If you want to keep using dw and the like, and also want to let it work with double-clicking a word, you can add

set iskeyword+=-

to your .vimrc, which adds - as a word character.

Example:

Double-clicking any character in ab-cd visually highlights ab-cd.

  • I'm fine with switching to dW, but your solution is also pretty cool because it affects visual mode too, I guess. I mean, by modifying the iskeyword, double-clicking and then pressing d I should be able to delete ab-cd. Right? – DeltaIV Nov 5 '18 at 15:16
  • Or maybe I should switch to visual mode with v, rather than double-clicking. Right? – DeltaIV Nov 5 '18 at 15:18
  • 1
    @DeltaIV by modifying iskeyword, you can use any method with word objects (including iw or aw, significantly simpler than using a mouse or visual mode) – D. Ben Knoble Nov 6 '18 at 13:19
4

Don't forget the f (find) and t (to) commands. I'd probably just dt or df (note the space at the end).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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