Thursday, October 25, 2012

Beware when using unnamed register as scratchpad

I just discovered a side effect of using the unnamed register as a
scratchpad register for mappings.

I have several mappings similar to this one,

nnoremap <silent> * :<C-U>
\let old_reg=getreg('"')<bar>
\let old_regmode=getregtype('"')<cr>
\yiw/\<<C-R>"\><CR>
\:call setreg('"', old_reg, old_regmode)<cr>

I thought that by saving the unnamed register before using it and
restoring it afterward (someone else's idea), that I wouldn't lose
anything saved in that register before using the mapping and
wouldn't even be able to tell that a register had been used.

The saving and restoring actually does work, BUT I also have in my
~/.vimrc,

set clipboard^=unnamed

which causes anything saved to the unnamed register to copied to the
* register and the * register to be used as the source for implicit
unnamed pastes.

Consequently, I found that when I yanked a line (with Y), then used
the * command to search for the place to put the line, then used p
to put the line, the word I had just searched for was put instead.

The solution I used was to find all the mappings where I had used
the unnamed register as a scratchpad and changed them to use some
other register instead. For example, I changed the mapping above to
use the v register.

nnoremap <silent> * :<C-U>
\let old_reg=getreg('v')<bar>
\let old_regmode=getregtype('v')<cr>
\"vyiw/\<<C-R>v\><CR>
\:call setreg('v', old_reg, old_regmode)<cr>

Regards,
Gary

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: