Você está procurando o registro de buraco negro ( :help quote_
). Se você prefixar "_
em um comando delete, o conteúdo simplesmente desaparecerá. Então, para apagar e manter as próximas três palavras, e então se livrar da linha inteira, você usaria d3w"_dd
.
Mapeamento avançado
Esse caso de uso de manter uma parte da linha enquanto remove a linha completa é comum; Eu escrevi um conjunto de mapeamentos para isso:
"["x]dDD Delete the characters under the cursor until the end
" of the line and [count]-1 more lines [into register x],
" and delete the remainder of the line (i.e. the
" characters before the cursor) and possibly following
" empty line(s) without affecting a register.
"["x]dD{motion} Delete text that {motion} moves over [into register x]
" and delete the remainder of the line(s) and possibly
" following empty line(s) without affecting a register.
"{Visual}["x],dD Delete the highlighted text [into register x] and delete
" the remainder of the selected line(s) and possibly
" following empty line(s) without affecting a register.
function! s:DeleteCurrentAndFollowingEmptyLines()
let l:currentLnum = line('.')
let l:cnt = 1
while l:currentLnum + l:cnt < line('$') && getline(l:currentLnum + l:cnt) =~# '^\s*$'
let l:cnt += 1
endwhile
return '"_' . l:cnt . 'dd'
endfunction
nnoremap <expr> <SID>(DeleteCurrentAndFollowingEmptyLines) <SID>DeleteCurrentAndFollowingEmptyLines()
nnoremap <script> dDD D<SID>(DeleteCurrentAndFollowingEmptyLines)
xnoremap <script> ,dD d<SID>(DeleteCurrentAndFollowingEmptyLines)
function! s:DeleteCurrentAndFollowingEmptyLinesOperatorExpression()
set opfunc=DeleteCurrentAndFollowingEmptyLinesOperator
let l:keys = 'g@'
if ! &l:modifiable || &l:readonly
" Probe for "Cannot make changes" error and readonly warning via a no-op
" dummy modification.
" In the case of a nomodifiable buffer, Vim will abort the normal mode
" command chain, discard the g@, and thus not invoke the operatorfunc.
let l:keys = ":call setline('.', getline('.'))\<CR>" . l:keys
endif
return l:keys
endfunction
function! DeleteCurrentAndFollowingEmptyLinesOperator( type )
try
" Note: Need to use an "inclusive" selection to make '] include the last
" moved-over character.
let l:save_selection = &selection
set selection=inclusive
execute 'silent normal! g'[' . (a:type ==# 'line' ? 'V' : 'v') . 'g']"' . v:register . 'y'
execute 'normal!' s:DeleteCurrentAndFollowingEmptyLines()
finally
if exists('l:save_selection')
let &selection = l:save_selection
endif
endtry
endfunction
nnoremap <expr> dD <SID>DeleteCurrentAndFollowingEmptyLinesOperatorExpression()