:nnoremap <leader>dd mzv0"zy?\V\^<C-R>z<CR>V'zj"_D`zj:nohlsearch<CR>
如果没有出现奇怪的字符(特别是反斜杠),这应该对您的行进行前缀重复数据删除,并将您的前缀保留在z
寄存器中;您可以扩展映射以切换到另一个缓冲区并将其粘贴,或者将其 shell-cat 到文件,或任何您想要的。它会覆盖z
标记和z
寄存器。
:nnoremap <leader>ds v0"zy?\V\^<C-R>z<CR>j:nohlsearch<CR>
这应该让您到达第一行,而不是以前缀开头。覆盖z
寄存器,不会弄乱标记。
编辑解释:
mz - set the mark `z` (on the character `c`)
v - start character-wise visual mode
0 - jump to start of the line
"zy - yank the visual contents (`./a/b/c`) into the `z` register
? - start backwards search
\V - enable very no magic mode (`:help /magic` - basically, only backslash is
special)
\^ - start of line in very no magic mode
<C-R>z - paste the contents of the `z` register (`./a/b/c`)
<CR> - perform the search (we end up on the *last* mention of `./a/b/c` at the
start of the line)
V - start linewise visual mode
'z - jump to the line where the `z` mark is (the line where we started)
j - down one line
"_D - cut the visual selection into the black hole register so we don't
pollute more than necessary (we delete every line starting with
`./a/b/c` except the first)
`z - jump to the character where the `z` mark is (`c` in the first line)
:nohighlight<CR> - remove the search highlight
类似地用于\ds
映射。
为防止反斜杠扰乱搜索,您需要在执行搜索之前将所有反斜杠替换为双反斜杠。
EDIT2:是的,即使使用反斜杠也可以:
:nnoremap <leader>dd mzv0"zy?\V\^<C-R>=escape(@z, '\\')<CR><CR>V'zj"_D`z:nohlsearch<CR>
添加位的说明:
<C-R>= - starts the paste of result of expression evaluation
escape(@z, '\\') - escapes the backslashes in `z` register
<CR> - ends the expression and pastes it in the search box