The GNU screen utility has a neat feature for binding keys to screen
commands or macros. The stuff
command is particularly handy to expand some abbreviation to some other string as your typing.
For example if I'm in screen
and I use my meta key ([Ctrl-A] by default) followed by :bindkey -t #@@ stuff "set -o vi; bind C-l:clear-screen C-i:complete"
... then ##@
becomes a quick way to set change settings of a bash
shell to my preferences even when I using a shared account (for example when I've used sudo
to get to the root shell on a server while trying to troubleshoot or fix it).
That's great and you can add those sorts of bindings into your ~/.screenrc
file.
But if you want to make a macro for something more sensitive ... a password for example ... then you might want to add a key binding to your running screen
session without storing the contents in any file, anywhere.
How do you do that?
You'd think it would be easy using the screen -X
(capital X) switch which takes a command. But the obvious attempts to do that fail:
screen -X 'bindkey -t #@p stuff mysecretpasswordhere'
Then the screen
status bar with briefly light up with an error like -X: unknown command ...
Why is that error message so unhelpful?
I have figured this out a couple times now. First time was a few years ago and was something I used for a long time. Then I had to figure it out AGAIN this morning. That's why I'm posting it here. So can more easily find it next time I need it, if I don't remember better.
The problem is that -X doesn't accept and parse a single argument as the whole command. It requires that you pass each of the elements of the command as a separate argument to the
screen
command line.So this works:
... and this is how I use it from a shell script:
Then when I start a new GNU
screen
session (once every few months, typically) then I simply run~/bin/setpw
to store my password in memory without ever writing it to any files anywhere.(Yes the actual key sequence to which I'm binding it has been sanitized for this posting. Duh! Also, yes, I am very consistent about using screen locking on my systems and the password is long, strong and thus rather inconvenient to be typing into
sudo
prompts all day).