274

I understand that vi has shortcut keys to delete characters, words and lines with various options.

However, I could not find this:

  • delete from the cursor to the next specified character

For example, I might type du" expecting the editor to "delete until the next " character is found"

The closest I know is d9w where 9 is the number of words to delete.

Does anyone know if this is possible?

icc97
  • 709
kctang
  • 2,843

8 Answers8

452

Use dtc, where c is any character, e.g. for you, you want dt"

This will delete upto but not including c.

If you had:

delete until exclamation point!

And the cursor was at the first space and you typed dt!, you would get:

delete!

Also dfc.

This will delete upto and including c.

Using df! on the same example above would give you:

delete

Just about any "motion" can be used for the d, c, y and similar commands.

icc97
  • 709
Arcege
  • 5,418
  • 13
    Presumably the mnemonic is t for "till"? Nice answer. – overthink Mar 05 '15 at 17:01
  • 27
    You can also delete to the previous instance of a character with the capitalized version of the movement commands. Maybe more useful for commas -- dT, and dF,. – joelostblom Apr 28 '15 at 14:53
  • 4
    This doesn't work over multiple lines. – 0xcaff Dec 29 '15 at 22:41
  • Anyone know of a nice summary of these commands? What do c and y do? – fraxture May 06 '16 at 07:21
  • 1
    You can look at some of the cheat sheets. One good page seems to be http://www.cse.psu.edu/~deh25/cmpsc311/Instructions/vi.html; c changes/replaces lines, 'y' yanks/copies lines, 'p' prints/pastes. – Arcege May 07 '16 at 00:04
  • Thank you for teaching me the df, or "including" the last char. – Hoang Tran Jun 29 '17 at 15:18
  • 4
    Can this be done over a range of lines? – Jim Aug 03 '17 at 17:17
  • 3
    For doing something similar over a range of lines, a usual search (as in zpletan's answer) can be performed: d/word searches up to word. It must be followed by enter (to close the search). – Eric O. Lebigot May 21 '18 at 11:15
  • one way to do this over a range of lines is to use a macro; for example, suppose you want to delete from the beginning of the line (0 to go to beginning of line) up to the first comma for 10 lines starting with the current line; step (1) make the macro, named 'a', (q starts and stops macro input, can contain many vi commands) type this: qa0dt,q step(2) run the macro 'a' for 10 lines, type this: @10a – jmarina Feb 04 '19 at 07:31
  • So easy:

    ctJ = "change till 'J'" – erases from the cursor position through the first 'J' on the line and leaves you in input mode to type its replacement

    – Dave Land Oct 28 '20 at 22:56
87
  • To delete forward up to character 'X' type dtX

  • To delete forward through character 'X' type dfX

  • To delete backward up to character 'X' type dTX

  • To delete backward through character 'X' type dFX

waltinator
  • 36,399
Mark Wong
  • 881
  • You can also add a number after the d character to delete through multiple instances. For example with the cursor at column 0 d2f! whould delete forward up through the second instance of the ! character on the line. – dcg Mar 14 '22 at 20:39
30

The input dt# (not a :command, use it like a movement like G)

will delete from the cursor until but not including the #. You can substitute any char for #.

Jessie
  • 401
  • 4
  • 2
21

It looks like @Arcege already answered the question, but I did d/l to delete until the character l; other characters would work as well.

zpletan
  • 3,373
7

w moves to the following word. l moves to the following charactor.

So it's d9l to delete the next 9 characters.

Shun Zhang
  • 71
  • 1
  • 1
  • 3
    Close, l is Vim's right arrow key, so you're actually deleting right. Conversely, d9h would delete 9 characters left. – Walf May 30 '16 at 02:24
3

I suggest use delete with pattern,

d/<pattern>

d is action delete

/ is vim searching sign

for example, the following is a MySQL connection string. I want to change to the password redhat to new one

root:redhat@tcp(localhost:3306)/?parseTime=True

In command mode , I click w(skip to next word), the cursor move to ":", click w again will move the cursor to first char r of password redhat. now click d/@ will delete the any characters until @ that in this case will remove the redhat. d/@tcp is better if the string has more than one @, the pattern more paticular is more precise.

2

Nobody mentioned about that so I thought I would share this as well.
If you want to delete until a character you can use dt and type the desired character. If you want to delete until a character but the character is also in the middle for instance (hell(o) world) and you want to delete from hell(o) until the last ) and your cursor is on the h you can type a number, in this case 2dt) this will jump the first ).

You can also use de (delete end), here by "end" you have to think "end of the word (not line)" so if you want to delete for instance .world in (hello.world) and your cursor is on the dot, you can type de.

Hope it helps

vdegenne
  • 205
0

You can also do ct" instead of dt" do delete until " and also enter insert mode, that is, dt"i == ct"

arnle
  • 101