How to clear vim registers effectively?

冷暖自知 提交于 2019-12-02 17:23:36
Luc Hermitte

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

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

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

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!