crazybilly
crazybilly

Reputation: 3092

Is there a succinct way to delete to the end of the word or the next punctuation mark in Vim?

In Vim, I regularly use dw to delete from the cursor to the end of the word and daw to delete an entire word.

Now that I'm editing SQL all the time, I'd love to be able do something similar for all the table and field names, which use underscores as delimiters.

Is there a succinct hotkey in Vim to delete from the cursor to the end of the word, or to the next punctuation mark? I don't want to have to spend the mental energy to decide "is the end I'm trying to delete up to an underscore, or is it the line end? Or is it a period between the table and field names?"

I'm sure I could do something like df(_|>), but if I'm typing that many characters, I might as well just hit the delete key 8 times.

Upvotes: 1

Views: 357

Answers (2)

gauauu
gauauu

Reputation: 46

If another way to rephrase the question is "how do I make vim consider _ to be a word delimiter so that I can use dw and other word-related commands with it?", then you can use

:set iskeyword-=_ 

Vim will then consider _ to be a word separator, allowing you to use dw to delete the text before the underline.

Some other options are mentioned in the answers in this post: Customising word separators in vi

Upvotes: 3

romainl
romainl

Reputation: 196826

use dw to delete from the cursor to the end of the word

The actual meaning of dw is "delete from the cursor to the next word". It certainly can "delete from the cursor to the end of the word", but only in some cases. de, which actually means "delete from the cursor to the end of the word" seems more appropriate.

Now, Vim has the notion of :help 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>).

and :help WORD:

A WORD consists of a sequence of non-blank characters, separated with white
space.

For example, in the sample below:

package main


import "fmt"

func main() {
    fmt.Println("hello world")
}

all these would be considered as "words", covered by lowercase word motions:

fmt
   .
    Println
           ("
             hello 
                   world
                        ")

while these would be "WORDS", covered by uppercase word motions:

fmt.Println("hello 
                   world")

And then there is the way w and W handle leading/trailing whitespace to take into account.

See :help word-motions.

I don't want to have to spend the mental energy to decide "is the end I'm trying to delete up to an underscore, or is it the line end? Or is it a period between the table and field names?"

All that to say that Vim can't be expected to read your mind with regard to what you exactly want to delete. Instead, it comes with mildly opinionated defaults and :help 'iskeyword', that you can adjust as you wish.

You are the only one who can decipher the riddle above so yes, you have to spend that mental energy.

Alternatively, you could provide precise and exhaustive requirements so we can help you figure out a way out of this.

Upvotes: 0

Related Questions