vim deleting backward tricks

Viewed 67611
  • How does one delete a word to the left? In other words, delete the word when the cursor stands at the end of it.
  • How does one delete characters to the beginning of the line?
  • How does one delete to the first whitespace to the left?

  • Any other tricks involving word deletion?

11 Answers

UPDATE: Seeing that many liked some of the additional tricks I've mentioned here, be sure to check the "Other (cool) boundaries section" at the bottom

I feel that none of the answers is complete:

In general, you usually start a delete operation using d<motion>, and seldom using x.

Note: When N is not specified, vim behaves as if N=1 (deletes a single char)

Discrete characters:

<N>x - Delete N chars to the right

d<N><left-arrow> - Delete N chars to the left

d<N><right-arrow> - Delete N chars to the right

Word boundaries:

Note: The 1st preceding/succeeding word is the one under the cursor

d<N>b - Delete from the beginning of the preceding N-th word to the current position

d<N>e - Delete from current position to the end of the succeeding N-th word

d<N>w - Same as d<N>e but including trailing whitespace

diw - Delete the entire word under the cursor

daw - Same as diw but including trailing whitespace

Line boundaries:

d0 - Delete from the beginning of the line to the current position

d^ - Delete from the first non-whitespace char to the current position

d$ - Delete from the current position to end of line

Other (cooler) boundaries:

These are tricks even veteran vim users usually don't know of, but are probably some of the most powerful tools in vim.

di( or di) - If you are located anywhere inside a parenthesis scope, i.e. (int a, int b, int c), deletes everything between the parenthesis, and leaves (). Very useful when changing function prototypes, conditions, loops, etc.

di{ or di} - Same as the one above, but deletes everything inside a curly brace scope. Very useful for changing a method implementation easily.

di[ or di] - You get the point, right? ;)

dit - Delete contents of a tag. For example, given <a href="..."><img src="smiley.gif"></a>, if you place you cursor within the <a...> tag and press dit, it will delete the entire <img...> part (Thanks @Eric for mentioning that).

Other important hint[s]:

Just in case you didn't know that, every sequence that starts with d can be replaced with c to delete and then go straight into insert mode!

db Deletes backward to the end of the word, but it does leave the character under the cursor. So if your cursor is at the last character of the word, db will delete all of it except the last character.

To also delete the character under the cursor, you can remap db into ldb, which moves the cursor right before deleting, thus also catching that last bit.

nnoremap db ldb

It works great, unless there is a space after the cursor's original position (this space will be left).

Another option is using dge that deletes backward to the end of word - but since it's an inclusive motion, it also deletes the 1st character of the previous word.

daw Handles all the spaces and surrounding words well, but will always delete the entire word you're on - even if not on the last character.

The best solution I have found is writing my little script which smartly handles things:

" Map db and dB to using the function
" Allows a smarter db
nnoremap db :call DeleteBackWord(0)<CR>
" Allows a smarter dB
nnoremap dB :call DeleteBackWord(1)<CR>

function! DeleteBackWord(capital_mode)
  " Gets the character after the cursor
  let l:next_char = getline(".")[col(".")]
  " Check if it's a whitespace
  if l:next_char !~# '\S'
    " whitespace, delete around word
    if a:capital_mode == 0
      call feedkeys('daw', 'n')
    else
      call feedkeys('daW', 'n')
    endif
  else
    " NOT whitespace, go 1 char right, then delete backward
    if a:capital_mode == 0
      call feedkeys('ldb', 'n')
    else
      call feedkeys('ldB', 'n')
    endif
  endif
endfunction

This code even differentiates deleting words and WORDS.

Two of my favorite vi delete commands are:

dt<char> deletes up to the dT<char> deletes backwards up to the

These are particularly useful for working with punctuation but can also be used for pretty quick word deletion by using dt<space> or dT<space>.

Was looking for this, great answers, here are two more tricks guys:

  1. vbd or vbx - visual back delete
  2. vbc - visual back change
Related