Aaron Toponce
Aaron Toponce

Reputation: 557

Vim syntax conceal passwords

I am creating a Vim syntax highlighting file for PGP-encrypted passwords using the format of this post: http://pig-monkey.com/2013/04/password-management-vim-gnupg/. Namely, the syntax is as follows:

Super Ecommerce{{{
    user:   foobar
    pass:   g0d
    Comments{{{
        birthday:   1/1/1911
        first car:  delorean
    }}}
}}}

Already folding is working with {{{ and }}} to prevent shoulder surfing. However, when an entry is expanded, I would also like to conceal the password with red background and red foreground text, to continue to prevent shoulder surfing when expanded.

Currently, I am solving this by wrapping the password in ((( and ))) blocks. This way, I can control which passwords are hidden, and which passwords are not. My Vim syntax for solving this is:

set conceallevel=3
syntax region gpgpassPasswords start="\v\(\(\(" end="\v\)\)\)"
highlight link gpgpassPasswords Conceal
highlight gpgpassPasswords ctermbg=red ctermfg=red

Our "Super Ecommerce" block would then look like:

Super Ecommerce{{{
    user:   foobar
    pass:   (((g0d)))
    ...

And (((g0d))) would then print as red foreground text on a red background, thus effectively concealing the password from view.

However, I think I would rather just conceal all passwords following ^\s*pass:\s* without ((( and ))). Unfortunately, I cannot seem to get the syntax correct for concealing just the password, without also concealing everything else on the line.

How can I conceal just passwords without extra region characters?

Upvotes: 2

Views: 580

Answers (2)

Christian Brabandt
Christian Brabandt

Reputation: 8248

I think the following should work:

syn match MyPassword /\%(^\s*pass:\s*\)\@<=\S\+/ conceal cchar=*
setl conceallevel=2 concealcursor=nv

This however depends a little on your other syntax rules. With newer Vims you can also just use a matchadd() function call.

Upvotes: 3

Marth
Marth

Reputation: 24812

Use \zs (match start) and \ze (match end) to limit the matched area:

:syntax match gpgpassPasswords /^\s*pass:\s*\zs.*\ze$/

Upvotes: 2

Related Questions