This is a very quick tutorial on how to auto-type
passwords
from KeePassXC
to a CLI
application. The document is mostly for myself to remember how to do it, but feel free to add your comments and improvements.
I use KeePassXC as my main password manager. About a couple of months ago I discovered I don't need to copy passwords from KeepassXC via clipboard. I found out about the Auto-Type
feature in the KeepassXC User Guide. According to the user guide, Auto-Type "acts like a virtual keyboard to populate data from your entries directly into the corresponding websites or applications that you use".
This is great for websites since I don't need any browser plugin. Instead I just press CTRL-Option-A (in my case) and let the user and password fields be auto-completed using KeepassXC. in order to do this correctly, you can configure a Window-Association
. This is usually the title of a website as far as I know.
Recently I wanted to have this password auto-type feature also be available on the shell. So I started to research.
I use iTerm2
as my terminal app on Mac OS X. My default shell is zsh
and I also use tmux
. So quite a few layers to reconfigure in order to make this work, but luckily it only requires a few changes.
My concrete use case is the following:
- I do have a shell script
./my-backup-script.sh
to start my backups (based on restic) - restic asks for a password to encrypt the backup
So instead of copy pasting this from keePassXC, I wanted the password to be auto-typed.
In order to make this work, I changed the title of iTerm2 to the current executed command.
For this you need to select Job (Name)
as Title
in iTerm2 (go to Preferences->Profiles->Genereal->Title).
Also select the Applications in terminal may change the title
option.
Then you type a command in iTerm2, e.g. ./my-backup-script.sh
this will become the title of iTerm2.
You can now configure a custom Window Association
in KeepassXC using the name of the script ./my-backup-script.sh
.
The following picture shows how I configured Auto-Type to be used for the ./my-backup-script.sh
script.
So far so good, but I want this to work on tmux as well. So I need a way to pass the name of the current executable from tmux to iTerm2. For this we can use the following option:
set-option -g set-titles on
set-option -g set-titles-string "#T"
This sets the terminal title to the tmux pane title. To set this persistently, configure this in your tmux config (~/.tmux.conf):
# source: https://superuser.com/questions/1098077/how-to-keep-terminal-window-title-in-sync-with-tmux-window
set -g set-titles on
# use the tmux pane_title
set -g set-titles-string "#T"
Next we write the tmux pane title using printf
. We do this before a new command is going to be executed via a zsh hook preexec
. Put this into yout zshrc (~.zshrc)
# Always set the execed command as tumx pane title
# source: https://stephencharlesweiss.com/zsh-hooks
autoload -Uz add-zsh-hook
add-zsh-hook preexec custom_func
function custom_func() {
# https://stackoverflow.com/questions/9747952/pane-title-in-tmux
printf '\033]2;%s\033\\' $1
}
Short summary:
- Tmux
pane title
is used to set the iTerm2 title - Tmux pane title is set by
printf
command using thepreexec
zsh hook.$1
is the name of the to be executed command.
This it it :)
Thank you for this! It set me down my own path.
A question though - I'm able to replace your "Tmux Integration" and "Zsh Config" sections with adding just this to .tmux.conf:
This doesn't require the zsh preexec, and I don't need to set my pane title - it just sets my terminal title directly.
I'm very new to this, so I'm wondering if I'm missing something? Or perhaps this was just written before this was an option in tmux?