Reputation: 44648
How does one delete to the first whitespace to the left?
Any other tricks involving word deletion?
Upvotes: 183
Views: 89335
Reputation: 47
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
Reputation: 614
Was looking for this, great answers, here are two more tricks guys:
vbd
or vbx
- visual back deletevbc
- visual back changeUpvotes: 1
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 ifN=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
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
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
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.
This is basically a simple line join in VIM.
kJ
does the trick (watch below)
Upvotes: 100
Reputation: 15882
In general, d<motion> will delete from current position to ending position after <motion>. This means that:
Read this to learn all the things you can combine with the 'd' command.
Upvotes: 241
Reputation: 143279
In insert mode:
Otherwise:
Upvotes: 37
Reputation: 72716
db
(if the cursor is after the word) or bdw
d0
(or d^
if you want to delete to the first non-blank character)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:
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
Reputation: 104125
In command mode:
bdw
, back delete word.d^
(to the first non-blank), d0
(to the first character)BdW
(go to first whitespace delete to next whitespace)(Community wiki, feel free to hack.)
Upvotes: 29
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