0

Is there a way to hide all text that is being entered, just as passwords are hidden? so that the cursor moves but the output is æquivalent to spaces, until the function is turned off? Variants of this function might leave the letter or word under the cursor visible.

6
  • I doubt it. But, with gVim, you can have a function/command/key combo that changes the font (set guifont=...) to some symbols, wing-dings, non-latin alphabet - and back.
    – VanLaser
    Oct 31 '15 at 18:54
  • Since the password (or whatever) would be stored in plaintext in the file itself, how would that function be useful?
    – Wildcard
    Oct 31 '15 at 19:53
  • Did you want this in Insert mode, or in the command area? I've answered with a proof of concept for Insert mode. Oct 31 '15 at 20:00
  • 1
    @Wildcard - I only presumed the OP wants to hide text from people looking at his monitor.
    – VanLaser
    Oct 31 '15 at 20:26
  • @VanLaser Why change the font? Isn't changing the colour enough? Oct 31 '15 at 22:02
2

In Insert mode, we can intercept each inserted character, and replace it with a *. Here is a quick proof of concept:

autocmd InsertCharPre * let v:char='*'

But that one-liner is undesirable, because now it's difficult to turn off that autocommand. You need to restart your Vim to get back to normal!

The tidy way:

augroup MyPassListener
  autocmd!
  autocmd InsertCharPre * if s:RecordCharacter(v:char) | let v:char='*' | endif
augroup END

You will need to implement your own RecordCharacter function, and perhaps build up a secret string:

let s:pass = ""

function! s:RecordCharacter(c)
  let s:pass .= c
  " Maybe do something special here if the character is a carriage return
  return 1
endfunction

When you are finished, and want to go back to normal, clear the event listener group you defined:

augroup MyPassListener
  autocmd!
augroup END

Notably the technique above won't store the typed characters in the buffer. A load of *s will be added to the buffer instead. The secret characters will be available only in the s:pass variable.

If you want the characters that were typed to exist in the buffer, you will need to hide them, rather than replace them. Two possible options are:

  1. Set the foreground and background highlighting to the same colour. This should make the text invisible (provided the characters are not highlighted in bold, which can render differently in terminals). (There might also be issues when the cursor is hovering over a character.)

  2. Use the conceal feature to hide or overlay the characters in the buffer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.