vim deleting backward tricks

Vim

Vim Problem Overview


  • 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?

Vim Solutions


Solution 1 - Vim

In general, d<motion> will delete from current position to ending position after <motion>. This means that:

  1. d<leftArrow> will delete current and left character
  2. d$ will delete from current position to end of line
  3. d^ will delete from current backward to first non-white-space character
  4. d0 will delete from current backward to beginning of line
  5. dw deletes current to end of current word (including trailing space)
  6. db deletes current to beginning of current word

Read this to learn all the things you can combine with the 'd' command.

Solution 2 - Vim

I have been in this scenario many times. I want to get rid of all the spaces in line 10 so it would join with line 9 after the comma.

enter image description here

This is basically a simple line join in VIM.

kJ does the trick (watch below)

Vim Join Lines

Solution 3 - Vim

To answer point #3, diw and daw are excellent.

Solution 4 - Vim

In insert mode:

  • ^w
  • ^u
  • can't answer out of my head ;-)

Otherwise:

  • dw
  • v0x
  • can't answer out of my head ;-)

Solution 5 - Vim

In command mode:

  1. bdw, back delete word.
  2. d^ (to the first non-blank), d0 (to the first character)
  3. BdW (go to first whitespace delete to next whitespace)

(Community wiki, feel free to hack.)

Solution 6 - Vim

  1. db (if the cursor is after the word) or bdw
  2. d0 (or d^ if you want to delete to the first non-blank character)
  3. dE or dtSpace to delete to the first space or d/</kbd>sEnter to delete to the next white space character.

Edit

Since the question has been changed such that 3 is delete to the first whitespace character to the left, my answer should change to:

  1. dB or dShiftTSpace to delete back to the first space or d?</kbd>sEnter to delete to the previous white space character.

See:

:help motion.txt
:help WORD

Solution 7 - Vim

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 not specified, 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

Solution 8 - Vim

/ <CR>x

(search forward for a space, hit enter to go there, x to delete)

There may be a more magic way of doing it, but I don't know of one.

Solution 9 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvehomzzzView Question on Stackoverflow
Solution 1 - VimLucas OmanView Answer on Stackoverflow
Solution 2 - VimAbdoView Answer on Stackoverflow
Solution 3 - VimRandy MorrisView Answer on Stackoverflow
Solution 4 - VimMichael Krelin - hackerView Answer on Stackoverflow
Solution 5 - VimzoulView Answer on Stackoverflow
Solution 6 - VimDrAlView Answer on Stackoverflow
Solution 7 - VimDaniel TrugmanView Answer on Stackoverflow
Solution 8 - VimbrettkellyView Answer on Stackoverflow
Solution 9 - VimTomer GazitView Answer on Stackoverflow