问题
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?
回答1:
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
回答2:
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
回答3:
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
回答4:
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
回答5:
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
回答6:
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
回答7:
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.
来源:https://stackoverflow.com/questions/19430200/how-to-clear-vim-registers-effectively