vehomzzz
vehomzzz

Reputation: 44648

vim deleting backward tricks

Upvotes: 183

Views: 89335

Answers (13)

pavel.brilliant
pavel.brilliant

Reputation: 47

  • How does one delete a word to the left? In other words, delete the word when the cursor stands at the end of it.

Almost no one on the Internet mentions that there is a ready solution for this trouble that many people try to find a trick for by either remapping or custom scripting.

:help forced-motion

dvb
cvb

Upvotes: 2

ZK_
ZK_

Reputation: 564

try this:

 inoremap <C-w> <C-o>diw

Upvotes: 0

H4CKY
H4CKY

Reputation: 614

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

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

Upvotes: 1

Daniel Trugman
Daniel Trugman

Reputation: 8511

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!

Upvotes: 21

Sean Chou
Sean Chou

Reputation: 1289

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>.

Upvotes: 4

Tomer Gazit
Tomer Gazit

Reputation: 31

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.

Upvotes: 3

Randy Morris
Randy Morris

Reputation: 40947

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

Upvotes: 50

Abdo
Abdo

Reputation: 14061

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

Upvotes: 100

Lucas Oman
Lucas Oman

Reputation: 15882

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.

Upvotes: 241

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143279

In insert mode:

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

Otherwise:

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

Upvotes: 37

DrAl
DrAl

Reputation: 72716

  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/\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?\sEnter to delete to the previous white space character.

See:

:help motion.txt
:help WORD

Upvotes: 16

zoul
zoul

Reputation: 104125

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.)

Upvotes: 29

brettkelly
brettkelly

Reputation: 28245

/ <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.

Upvotes: 2

Related Questions