在 Vim 中删除但不剪切一行

Dre*_*Boy 5 vim

这不是vi删除行的重复,它在问不同的问题。我想删除一行而不剪切它(将它放在剪贴板中)。

我想复制行的一部分,删除一行,然后将该行的该部分粘贴到其他地方。使用v3w,dd然后p粘贴整行。

Ing*_*kat 9

您正在寻找黑洞寄存器( :help quote_)。如果您添加"_到删除命令之前,内容将消失。因此,要删除并保留接下来的三个单词,然后删除整行,您可以使用d3w"_dd.

高级映射

在删除完整行的同时保留部分行的用例是常见的;我为此编写了一组映射:

"["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()
Run Code Online (Sandbox Code Playgroud)