3

I've read that Vim automatically stores previously yanked texts into registers from 1 to 9, being 1 the previous yank, 2 the second previous, and so on...

But this is not working for me. Here is a screencast showing that registers 1-9 are not being populated: https://streamable.com/rmyxhl

How can I configure vim to keep previous yanked texts?

2

You can write a code to do this. Registers 1-9 would be used.

Register 0 gets override with the current yank, so you need to save the last yank.

function! SaveLastReg()
    if v:event['regname']==""
        if v:event['operator']=='y'
            for i in range(8,1,-1)
                exe "let @".string(i+1)." = @". string(i) 
            endfor
            if exists("g:last_yank")
                let @1=g:last_yank
            endif
            let g:last_yank=@"
        endif 
    endif
endfunction 

:autocmd TextYankPost * call SaveLastReg()
| improve this answer | |
  • 1
    Thanks, I was already thinking of writing a function for this just didn't had the time yet. It is better than install a plugin with features I don't use, IMO. :) – paulodiovani Aug 20 at 1:12
  • yep, I actually hoped for a few days that someone else would write. And the plugins seemed cumbersome. – eyal karni Aug 20 at 16:46
5

Vim automatically stores previously yanked texts into registers from 1 to 9

That's not accurate. Registers 1 through 9 are actually used for the previously delete or change commands.

Yank commands store their results in register 0, unless a named register is explicitly specified.

See :help "1 (emphasis mine):

Vim fills these registers with text from yank and delete commands.

Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with "x.

Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). [...]

(And then it goes on to explain exceptions when a delete is less than one line, but it uses "1 instead of "- anyways...)

How can I configure vim to keep previous yanked texts?

There's no native configuration for this feature, so you'd have to use a plug-in.

A quick search led me to find vim-yoink and YankRing.vim, among a few others, which implement such a feature (not necessarily using the same yank command or the existing numbered registers.) You might want to consider taking a look at some of these if you're interested in such a feature.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.