Question

I have stored plain text password in config file. Is there any way blur or replace password with XXXXX.

when I open config file in vim I want to see

password = XXXXX

Was it helpful?

Solution

The easiest and most secure way of obscuring the password in the config file is

DON'T PUT A PLAINTEXT PASSWORD IN A FILE

Any solution otherwise using vim for what you describe will only obscure the password for you. Other people can open/read the file without vim, seeing the password.

If, for some reason, this is the use case you want (say, for projecting a presentation, and you are for some ungodly reason using a password that the audience could use to exploit something live (Why the hell are you doing that), you can write an autocmd that will obscure everything after the equals sign.

autocmd BufRead * silent! %s/=[^=]*$/=XXXX/

But, note that this is only for a presentation: the text is actually rewritten and you would not want to save this file.

Your other choice would be using 7.3's syntax highlighting to mark the text as concealed.

OTHER TIPS

Yes, with the conceal feature introduced in Vim 7.3, you can replace matches with one replacement character. So you could condense the entire password to a single *, or replace every character to a * (by matching each password character individually).

Concealing is implemented with syntax highlighting. As long as you don't have any existing syntax for your password files, this is just writing a simple syntax/password.vim syntax script and doing

:setlocal syntax=password

Integrating into other syntaxes is more difficult.

See :help :syn-conceal for details. This help page also has all the information about writing a syntax. You'll especially need :help :syn-match.

The nice thing about conceal is that you can still yank the original password text (e.g. to the clipboard) without revealing it. The 'conceallevel' and 'concealcursor' options control that.

Simpler alternative

If you just want to avoid other people reading your passwords from over your shoulder, you could define folds for all individual login entries (/ paragraphs), start with all folds closed, and only briefly open the single current fold for access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top