62

Registers in vim are a great feature to store text snippets and even to run commands on the text stored within them. However, I'm a tidy person and tend to clean things up when I'm done.

I know that if I wanted to clear register a, I can use qaq.

I can also execute the following command:

:let @a = ''

However, these solutions seem like a mere workaround to the problem. When I execute :registers, the list still displays register a (with an empty value), while registers that have otherwise never been used are not displayed.

Is there a way to clear a register with the side-effect of removing the register from this list?

And if so, is there also a way to clear all registers at once, i.e., to reset that list of registers?

dreftymac
  • 27,818
  • 25
  • 108
  • 169
Robin Klose
  • 725
  • 1
  • 5
  • 7

7 Answers7

31

Since that venerable answer on the mailing list, linked by @romainl, we have setreg('a', []) that clears the register.

Thus, the code could become:

let regs=split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-"', '\zs')
for r in regs
  call setreg(r, [])
endfor
Community
  • 1
  • 1
Luc Hermitte
  • 29,719
  • 7
  • 60
  • 74
  • 4
    Wow - this actually removes it from the `:registers` list! Thanks!! ^u^ – Jason Cemra Oct 28 '16 at 05:30
  • 2
    Not for me; `:exec setreg('a', [])` gives me `E730: using List as a String` and doesn't remove nor empty the register. – Walf Nov 16 '16 at 06:42
  • 1
    At some point in time, vim has started to reject `:let a =1` + `:let a = {}`. During last year, this was reverted. I suspect you have a vim version compiled at some point in between those two patches. – Luc Hermitte Nov 16 '16 at 07:42
  • What does this do with the Capitalised letters? (`ABC...`). My understanding is that there is no `A` register - instead using `A` appends to the `a` register. – dgmstuart Feb 06 '17 at 12:27
  • 1
    @dgmstuart, your understanding is correct. As a consequence, it seems that appending an empty list to a register leaves it unmodified. – Luc Hermitte Feb 06 '17 at 13:43
  • @LukeDavis, with `setreg('a', '')`, the register _a_ still appears in `:registers`. With `setreg('a', [])`, it is removed, which answers OP's question. – Luc Hermitte Jul 01 '18 at 23:20
  • This is exactly what I want and this is the best answer to the question.We should vote it up. – Lane Aug 14 '18 at 07:18
  • Hi there, just tested `:exe setreg('a', [])` and for `vim` this does indeed erase the register `a` completely, but in `nvim`, it replaces its content with `^J` (equivalent of ``) instead. Can someone reproduce this ? If so, how to achieve the wanted behavior of deleting the register in neovim ? – Atralb May 21 '20 at 00:13
  • 1
    @Atralb Note: it's not `:execute`, it's `:call`. With `exe`, you try to execute the result of `setreg()` which should be 0 (a function with no value returned will return 0 in vim script), This means that this will execute `:0`, IOW, it'll move the cursor to the first line. Regarding nvim, I have no idea. Try to see what `:h setreg()` returns in case there is some specific parameter combinations with nvim. May be you should try to ask on nvim mailing list -- I guess there is one. – Luc Hermitte May 21 '20 at 02:36
18

AFAIK you can't use built-in commands/functions to make registers disappear from the list. That seems to be doable only by removing them from your ~/.viminfo which sounds a bit extreme.

this thread on the vim mailing list has a command that clears every register but it doesn't remove them from :reg:

let regs='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-"' | let i=0 | while (i<strlen(regs)) | exec 'let @'.regs[i].'=""' | let i=i+1 | endwhile | unlet regs
romainl
  • 158,436
  • 18
  • 236
  • 264
  • Hmm. That same strategy does work to clear the search register. `:map :let @/ = ""` Shame it doesn't work for what the OP wants. – jpaugh Sep 16 '19 at 14:50
16

Put this in your .vimrc:

command! WipeReg for i in range(34,122) | silent! call setreg(nr2char(i), []) | endfor

and clear every register with :WipeReg

If you would like that to happen every time you start Vim also add:

autocmd VimEnter * WipeReg
laktak
  • 47,916
  • 15
  • 112
  • 150
9

Remove .viminfo file or delete the register line in the .viminfo file.

You can get the details from Here:

The viminfo file is used to store:
- The command line history.
- The search string history.
- The input-line history.
- Contents of non-empty registers.
- Marks for several files.
- File marks, pointing to locations in files.
- Last search/substitute pattern (for 'n' and '&').
- The buffer list.
- Global variables
Marslo
  • 2,650
  • 3
  • 22
  • 34
7

Another option is to never load any registers. As others have said, registers are loaded from .viminfo. The -i flag is used to specify what viminfo file to use. If you specify NONE, no viminfo, and therefore no registers will be loaded.

vim -i NONE
DJMcMayhem
  • 6,593
  • 3
  • 35
  • 52
  • This is good but of course you lose the benefit of the .viminfo files full stop for this. That's unfortunate. Still a valid way to go about it. – Pryftan Sep 28 '20 at 15:36
5

It is possible to set a value for each used register, similar to romainl's approach:

function! ClearRegisters()
   redir => l:register_out
   silent register
   redir end
   let l:register_list = split(l:register_out, '\n')
   call remove(l:register_list, 0) " remove header (-- Registers --)
   call map(l:register_list, "substitute(v:val, '^.\\(.\\).*', '\\1', '')")
   call filter(l:register_list, 'v:val !~ "[%#=.:]"') " skip readonly registers
   for elem in l:register_list
      execute 'let @'.elem.'= ""'
   endfor
endfunction

This avoids including additional register on the output of :registers

mMontu
  • 8,343
  • 4
  • 31
  • 51
3

For the sake of completeness, I'll note that while setting a register to contain an empty string doesn't remove the register from the output of the :registers command, Vim does not save registers which have been cleared in this way to the .viminfo file.

Therefore, one other quick-and-dirty alternative for removing specific registers from the list is to clear them using either of the commands you suggest, and then restart Vim.

Rich
  • 6,129
  • 4
  • 30
  • 51