Search code examples
securityvimpasswordsneovim

Editing passwords in Vim/Neovim: How do I disable swap, backup, undo, etc files from command line


From time to time I need to edit sensitive files, for instance files that contain passwords or certificates. To avoid Vim leaking sensitive information, is there a way invoke Vim/Neovim so that Vim only touches the edited file during that invokation? No changes or creation of backup, swap, undo or other files?


Solution

  • To disable backup, undo and swap files as well as the backupcopy functionality, you can (un)set those option from the command line as follows:

    vim +"set nobackup nowritebackup noundofile noswapfile viminfo=" a_sensitive_file
    

    See :help 'backup', :help 'undofile', :help 'writebackup', :help 'swapfile' and :help 'viminfo' for reference and the options' implications. See :help -c for information on how to execute Ex commands from the command line (in this case, the :set command).

    You may also be interested in :help encryption - not in the encryption feature itself - there are hints on how to edit files containing sensitive information.

    You can create an alias for the command above or put them into a vimrc file, e.g. secure.vim and do

    vim -u /path/to/secure.vim a_sensitive_file
    

    This will :source only the options in secure.vim to initialize Vim (see :help -u). You may be left without your favorite colorscheme and plugins.

    Disclaimers:

    1. I may have missed something, of course. If one of those Bond villains gets hold of the nuclear codes, it's still your fault!
    2. Depending on what else is in your Vim setup (I'm thinking of plugins, mainly), there may be other options you will have to switch off. We have GitHub Copilot for Vim these days, what better way to keep your data save than to broadcast it to an AI?