Clear all registers and macros in Vim

Vim is my favorite text editor and while it is lightweight in most respects, it tends to store a lot of information between edits about recently used registers (for macros and yanking/pasting), undo information and much more. These are great features I’ve come to rely on a lot but they can be annoying if one is working with sensitive information. Therefore I have two vim configurations: One feature-rich with lots of plugins and bling and one absolutely basic for editing sensitive files like password files or e-mail.

Today I accidentally used the full profile for lots of password and configuration editing and sure enough the registers (:reg) contained things I don’t want in my history. I could have just replaced the offending registers with something harmless interactively (to clear the register f: qfq) but I wanted a general solution.

Tim Chase provided a solution for just this problem in 2005. I split that one-liner up into a function, put it in my ~/.vimrc and just have to type :ClearRegisters in command mode whenever I used the wrong vim profile or paranoia hits 🙂

function! ClearRegisters()
    let regs='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-="*+'
    let i=0
    while (i<strlen(regs))
        exec 'let @'.regs[i].'=""' 
        let i=i+1
    endwhile
endfunction

command! ClearRegisters call ClearRegisters()
Tagged with:
Posted in Vim

Leave a comment