DasOhmoff San
DasOhmoff San

Reputation: 975

Vim - delete until previous word (excluding the previous word)

Is there a way to delete until the previous word in vim (without deleting parts of the previous word itself)?

For example, so that the result is as follows (the pipe character '|' is the cursor position):

before: one   two   three   |four
after:  one   two   three|four

Another example:

before: one   two   three   fo|ur
after:  one   two   three|ur

Is there a way to make this happen?

Upvotes: 3

Views: 704

Answers (4)

klokop
klokop

Reputation: 2410

Combination of

  • db (delete to beginning of current word) and
  • diw (delete inner word, (also all spaces between words))

Upvotes: 2

SergioAraujo
SergioAraujo

Reputation: 11800

Using 'T' option

dTe ........... delete until (before) 'e'

Upvotes: 2

DasOhmoff San
DasOhmoff San

Reputation: 975

Peter Rincker gave me the following idea:

"My custom text objects (move until one character before word)
nnoremap <silent> [ ?\S\zs\s<cr>
nnoremap <silent> ] /\s\S<cr>
vnoremap <silent> [ ?\S\zs\s<cr>
vnoremap <silent> ] /\s\S<cr>
nnoremap <silent> d[ d?\S\zs\s<cr>
nnoremap <silent> d] d/\s\S<cr>
nnoremap <silent> c[ c?\S\zs\s<cr>
nnoremap <silent> c] c/\s\S<cr>

Meanings:
? - search backwards
/ - search forwards
\S - search for non white space character
\s - search for white space character
<cr> - press enter (finishes the command)
\zs - places cursor to this position. Lookup ´help \zs´, for more accurate info.

Example:

nnoremap <silent> [ ?\S\zs\s<cr>

When pressing the [ button, search backwards (?), for a non white space character (\S) followed by a white space character (\s). Put the cursor in between the white space and non white space character (\zs). Enter the command (<cr>).

Upvotes: 0

Peter Rincker
Peter Rincker

Reputation: 45097

I would like to use ge, however it is inclusive and not quite right for this application. Instead use a search, ?, with the delete command.

d?\><cr>

This deletes (d) backwards (exclusively) by searching (?) until it finds an end of word boundary (\>).

For more help see:

:h ?
:h exclusive
:h /\>

Upvotes: 3

Related Questions