Delete but not cut a line in Vim

vim

This is not duplicate of delete line in vi, it's asking different question. I'd like to delete a line without cutting it (placing it in clipboard).

I'd like to copy part of line, delete a line and then paste just that part of line somewhere else. Using v3w, dd and then p pastes whole line.

Best Answer

You're looking for the black hole register (:help quote_). If you prepend "_ to a delete command, the contents will just be gone. So, to delete and keep the next three words, and then get rid of the entire line, you'd use d3w"_dd.

Advanced mapping

That use case of keeping a part of the line while removing the complete line is a common one; I've written a set of mappings for that:

"["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()
Related Question