在 Vim 中删除但不剪切行

在 Vim 中删除但不剪切行

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

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

答案1

您正在寻找黑洞寄存器: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()

答案2

您可以复制要在命名缓冲区中使用的部分并从那里粘贴,例如:

"ay3w

这将在命名缓冲区 a 中提取 3 个单词,并且

"ap

稍后将从命名缓冲区粘贴;您也可以先删除 3 个单词,然后删除所有行,然后粘贴

"2p

这将从删除缓冲区中粘贴上次删除的第二个;还遵循评论建议,因为这是 VIM 标记的问题 - 有针对此问题的本机 VIM 解决方案(不适用于 Vi):

y3w then in new place "0p

VIM 具有将最后一次复制保留在 0 注册表中的本机功能。

相关内容