← back to latest

Delete up to the next comma without counting words

NORMALdt,Delete up to the next comma without counting words

anatomy

d
The delete operator. Waits for a motion and deletes the text the motion covers.
t
Till motion. Moves forward on the current line to the character immediately before the next occurrence of the character you type. Exclusive: the target character itself is not included.
,
The target character. Any single character works here: a comma, a quote, a closing paren, a space.

In the buffer

Before:

connect(host="db.internal.example.com", port=5432, retries=3)
        ^

After typing dt,:

connect(, port=5432, retries=3)
        ^

The first argument is gone, up to but not including the comma. The cursor sits on the comma, ready for you to type a replacement with i or to keep going.

When you would reach for it

You need to remove the first argument of a function call, and it is a string full of dots, quotes, and punctuation. Counting words for d5w means knowing how vim splits "db.internal.example.com" into words, which nobody knows offhand. You do know where the argument ends: at the comma. t lets you name the landmark instead of counting the distance, and any operator can use it.

Gotchas

  • t only searches the current line. If the target character is on the next line, the motion fails, vim beeps, and the operator deletes nothing. Use d/, with a search when the target might be further away.
  • t is exclusive and f is inclusive. dt, leaves the comma; df, takes it. When you mean to remove a whole argument including its separator, df, (followed by x for the space) is usually what you want.
  • If the target character is the very next character, dt, has nothing to cover and does nothing. The cursor is already “till” the comma.
  • After t,, pressing ; in Vim jumps to before the next comma rather than staying put. In strict vi, or with cpo containing ;, it stays stuck. Use 2t, or f, if you need the same result everywhere.

Variants

connect(host="db.internal.example.com", port=5432, retries=3)
                                                   ^
connect(host="db.internal.example.com", port=5432, retries=5)
                                                           ^

ct) then retries=5 and <Esc>: change everything up to the closing paren, leaving the paren in place.

connect(host="db.internal.example.com", port=5432, retries=3)
                                                 ^
connect(, retries=3)
        ^

dT(: the backward version. Deletes from the cursor back to immediately after the opening paren. The character under the cursor stays, because backward motions are exclusive of the starting position.

connect(host="db.internal.example.com", port=5432, retries=3)
        ^
connect(, retries=3)
        ^

d2t,: a count picks the second comma, so two arguments go in one command.

lineage

The f, F, t, and T motions, along with ; and , to repeat them, were part of Bill Joy's original vi and are specified by POSIX. Vim added one refinement: since Vim 7.3.235, repeating a t motion with ; skips past the character it is already sitting in front of, unless the cpo flag ';' is set to restore the old behavior.

-- INSERT --vim-hacks.com032-032-delete-up-to-a-character.html
utf-81,1All