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.

  • 2
    This question is conflating the unnamed register in VIM with an X11 clipboard selection, accessible in VIM via a selection register. – JdeBP Oct 24 '18 at 6:22
  • @JdeBP for advanced VI user it might be conflicting because we have knowledge of Vi, but for novice it makes sense as he is not aware of both neither named nor unnamed registers in Vi; but pointing from here to those questions might be good addition, if you have exact links feel free to add to my answer. – Drako Oct 24 '18 at 6:33
  • "those questions" has no antecedent. I did not talk about any other questions than this one. – JdeBP Oct 24 '18 at 6:40
  • Now that the subject of other questions has been raised by someone else, see unix.stackexchange.com/questions/7788 from 7 years ago. – JdeBP Oct 24 '18 at 7:33
  • why not: v3w "_dd p ? – JJoao Oct 24 '18 at 7:54

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

    return '"_' . l:cnt . 'dd'
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

    return l:keys
function! DeleteCurrentAndFollowingEmptyLinesOperator( type )
        " 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()
        if exists('l:save_selection')
            let &selection = l:save_selection
nnoremap <expr> dD <SID>DeleteCurrentAndFollowingEmptyLinesOperatorExpression()
| improve this answer | |

you can copy that part that you want to use in named buffer and paste from there, for example:


this will yank 3 words in named buffer a and


would paste from named buffer a later; also you could first delete 3 words and then delete all line and later paste with


this would paste 2nd from last deletion from deletion buffer; also following suggestions on comments as this is VIM tagged question - there is native VIM solution for this (does not work on Vi):

y3w then in new place "0p

VIM has native feature to keep last yank in 0 registry.

| improve this answer | |
  • Why not use VIM's black hole register? – JdeBP Oct 24 '18 at 6:24
  • @JdeBP are you serious? Did you even read the answer - second option in my answer is exactly that - I only did not name it black hole register - some what old style naming - deletion registry but essentially same thing numbered buffer from 1-9 keeping track of last changes in file – Drako Oct 24 '18 at 6:28
  • The second option in your answer is not the black hole register. The black hole register is not a numbered register. – JdeBP Oct 24 '18 at 6:36
  • 2
    I don't need to read that, as I already know about the black hole register, and that it is not essentially the same as a numbered register, it being essentially different in fact. I've given you a suggestion for making your answer better, to a question that definitely is about VIM, labelled as such in both tags and title. You've refused it. Don't be dismayed then if someone else comes along, reads my suggestion, and posts an answer giving the simple black hole register way of doing this that has no extra side-effects. I gave you the opportunity to make a good comprehensive answer. – JdeBP Oct 24 '18 at 7:31
  • 1
    The difference is that pasting from the black hole register returns nothing. Using any of the other named registers will. The black hole register is where you put stuff of you don't want it affect later pasting at all. – muru Oct 24 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.