Sometimes, there is a need for a mapping to yank something without a trace so it has to save and restore the relevant registers.
The problem is that there is no way to set register " without modifying register 0. So if, for example, we have the following:
:reg 01"
--- Registers ---
"" deleted text^J
"0 yanked text^J
"1 deleted text^J
Now we want to do a yank as part of a mapping, this mapping will have to restore register " with the deleted text. But now the original content of register 0 (the yanked text) is lost.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
Have you tried saving and restoring with the :let
command, by means of an auxiliary variable (possibly a script-local variable)? (see :help script-variable
, :help let-register
and :help expr-register
)
Best regards,
Tony.
If you meant :let @"='text', then yes -after this register 0 contains the same text.
I think this has been discussed before, but I don't find it. I think I even made a patch back then, but Bram didn't want to change this behaviour, as this might have unexpected results.
And getreg() fails to fix the problem as well: whether I have '"'
or '0'
at the end, the result is the same value in both.
—
You are receiving this because you commented.
By the way, I once written a function which may serve as a replacement to normal! gvy
followed by getreg('"', 1, 1)
: https://gist.github.com/fa35a5128886c0ecbca8f7c14fbcdde1. Usage to just yank is
let data = selection#modrange('b', 42, getpos("'<"), getpos("'>"), 0)
You may see there that handling blockwise selection is tricky and you will need to somehow get width, and while just y
equivalent which is not using any registers for characterwise selection is easy, handle replacement is not so.
—
You are receiving this because you commented.
Fixed by 8.2.0924 which provides the getreginfo()
function.
The latter outputs a dictionary providing everything you need to save and restore the unnamed register:
regcontents
keyregtype
keypoints_to
keyBefore the patch, you could try (and fail because it didn't point back to the same register) to save/restore it like this:
let save = ['"', getreg('"', 1, 1), getregtype('"')]
" do something which alters the unnamed register
call call('setreg', save)
Now, you can do it like this:
let save = getreginfo('"')
" do something which alters the unnamed register
call setreg('"', save)
Broken code before the patch:
vim -es -Nu NONE -i NONE -S <(cat <<'EOF'
call setline(1, ['yanked text', 'deleted text', 'line deleted by mapping'])
norm! yyjdd
pu=['registers before mapping'] + [execute('reg \"01')]
nno cd :call Func()<cr>
fu Func()
let save = ['"', getreg('"', 1, 1), getregtype('"')]
norm! 2Gdd
call call('setreg', save)
endfu
au VimEnter * call feedkeys('cd', 'xt')
\ | $pu=['', 'registers after mapping'] + [execute('reg \"01')]
\ | exe 'g/^registers/t.|norm! Vr='
\ | 2,$p | qa!
EOF
)
registers before mapping
========================
Type Name Content
l "" deleted text^J
l "0 yanked text^J
l "1 deleted text^J
registers after mapping
=======================
Type Name Content
l "" deleted text^J
l "0 deleted text^J
l "1 line deleted by mapping^J
Working code after the patch:
vim -es -Nu NONE -i NONE -S <(cat <<'EOF'
call setline(1, ['yanked text', 'deleted text', 'line deleted by mapping'])
norm! yyjdd
pu=['registers before mapping'] + [execute('reg \"01')]
nno cd :call Func()<cr>
fu Func()
let save = getreginfo('"')
norm! 2Gdd
call setreg('"', save)
endfu
au VimEnter * call feedkeys('cd', 'xt')
\ | $pu=['', 'registers after mapping'] + [execute('reg \"01')]
\ | exe 'g/^registers/t.|norm! Vr='
\ | 2,$p | qa!
EOF
)
registers before mapping
========================
Type Name Content
l "" deleted text^J
l "0 yanked text^J
l "1 deleted text^J
registers after mapping
=======================
Type Name Content
l "" deleted text^J
l "0 yanked text^J
l "1 deleted text^J
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Closed #2345.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.