For conditional remapping, you can use an expression mapping (:help map-expr
). I don't know if this already works for you, but it should get you started:
:vnoremap <expr> p (getregtype() ==# 'v' && col("'>") == col('$') ? '"_dp' : '"_dP')
Это связано с / продолжением раздела Как заменить вставленный текст в vim, не удаляя удаленные строки?
В итоге, vnoremap p "_dP
используется для вставки визуально выделенного текста без перезаписи "*
регистра замененным текстом. Таким образом, последующие визуально отобранные пасты остаются неизменными.
Этот переназначение работает, как и ожидалось, за исключением случаев, когда визуальный выбор за символом идет до конца строки . Я пытаюсь найти способ справиться с этим условно, чтобы:
"_dp
"_dP
Я имею в виду vnoremap p
функцию vimscript, которая проверяет этот конкретный случай, т. Е. «Если визуальный выбор - символьный, а курсор находится в конце строки», и выполняется соответствующим образом.
meta - если vimscript является ответом, может быть, я должен опубликовать в StackOverflow?
Обновить
Работает с:
vnoremap <expr> p (getregtype() ==# 'v' && col(".") == col("$") - 1 ? '"_dp' : '"_dP')
For conditional remapping, you can use an expression mapping (:help map-expr
). I don't know if this already works for you, but it should get you started:
:vnoremap <expr> p (getregtype() ==# 'v' && col("'>") == col('$') ? '"_dp' : '"_dP')
Using the black hole register can be a solution to two problems: you want to keep your numbered registers to only contain explicitly yanked text or you want to be able to paste the same text multiple times.
For the first problem, using "_d
instead of d
is the right tool because none of what you delete will appear in any register.
For the second problem, combining "_d
and p
or P
inevitably creates problems because of cursor position and the orientation of p
and P
.
Ingo's answer seems to be a perfect compromise.
But if you don't care about the state of your numbered registers, an alternative is to use the "0
register which always contains the latest yanked text and is not impacted by c
or d
?
Test yank: foo
, yanked with yiw
.
Test line: Lorem [i]psum dolor sit amet.
, with the cursor on the i
of ipsum
.
Test selection: v$
.
"_dP Loremfoo <-- trailing whitespace and mashed text, bad "_dp Lorem foo <-- good "0p Lorem foo <-- good
Test selection: vee
.
"_dP Lorem foo sit amet. <-- good "_dp Lorem foosit amet. <-- bad "0p Lorem foo sit amet. <-- good