5

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.

Dread Boy
  • 161
  • 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 06: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 06:33
  • "those questions" has no antecedent. I did not talk about any other questions than this one. – JdeBP Oct 24 '18 at 06:40
  • Now that the subject of other questions has been raised by someone else, see https://unix.stackexchange.com/questions/7788/ from 7 years ago. – JdeBP Oct 24 '18 at 07:33
  • why not: v3w "_dd p ? – JJoao Oct 24 '18 at 07:54

2 Answers2

9

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()
Ingo Karkat
  • 11,864
1

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

"ay3w

this will yank 3 words in named buffer a and

"ap

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

"2p

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.

Drako
  • 119
  • Why not use VIM's black hole register? – JdeBP Oct 24 '18 at 06: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 06: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 06:36
  • @JdeBP read here: http://blog.dreasgrech.com/2010/06/vims-black-hole-register.html it seems that I have had partially wrong assumption that it the same, but essentially it's same with expanded functionality, that definitely is not needed here; also I try to stick to vi not vim features as often working on systems where vim is not available; but admit that it's not exactly the same - sorry for my bad comment – Drako Oct 24 '18 at 06:46
  • 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 07:31
  • @JdeBP Thanks, I just don't see how using black hole here would improve solution in this use case, specially the second, where by the way additionally instead of deleting and pasting from register 2, in VIM could y3w and "0p - that would be VIM feature to use here - that last yank is put in 0 register so no complex solution necessary. – Drako Oct 24 '18 at 08:45
  • 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
  • That's clear @muru but I still don't see big value of that, because when I want to keep something I put it in named register, deletion go auto in numbered, also I often work with vi on solaris, hp-ux where sometimes even have version of vi where moving around cursor can't be done with arrows, so I personally try not to become too addicted to new specific use-case features that does not give clear benefit - because I don't see real benefit from not saving my deletion - even opposite I like to be able to revert my error from numbered register; but agree - it can be useful in specific use-cases. – Drako Oct 25 '18 at 06:26