I use ssh-keys to access the remote git server (e.g. github/gitlab). My ssh-key is protected by a password. Thus when I call a git command with the remote, it will require me to enter the password for my key.

An alternative, is to use an ssh-agent. Within a given shell, I could do

eval `ssh-agent -s`

This will require my password, but then all the git commands following would not require to enter it anymore.

Now, all of the above works equally well within a vim-terminal. The draw-back, and hence my question, is that if I close that terminal, I would need to call a new ssh-agent within the next terminal.

System-wide ssh-agent would cover it, but in this case, I am essentially limited to an access with a single shell.

How can I get an ssh-agent active for all the vim-terminal for a given session?

  • So far, I am solving it with tmux, one shell running vim, another to run all the git commands. Jun 8, 2022 at 9:22

2 Answers 2


I have a little ssh-reagent function in my zshrc:

# Restart ssh-agent after restarting WM or x11 (may be old env var in tmux).
ssh-reagent() {
    export SSH_AUTH_SOCK=$(echo /tmp/ssh-*/agent.*)
    ssh-add -l >/dev/null || ssh-add

This will pick up on any existing ssh-agent processes in /tmp; it kind of assumes there's just one running, which is true for my system. After setting SSH_AUTH_SOCK run ssh-add -l to list any keys, and run ssh-add if there are none yet.

It's intended to "reagent" old tmux terminals after restarting X11 (which also restarts ssh-agent) which have the old/wrong SSH_AUTH_SOCK set, but you can also use it in your case. You can add it to your shell startup file maybe; you can use $VIM_TERMINAL to check if you're running it from a Vim terminal:

if [ -n ${VIM_TERMINAL:-} ]; then
    ssh-reagent  # Or just put the code here.

This does not exactly answers the question as it is, but it provides a work-around.

In a shell, run

eval `ssh-agent -s`

The ssh-agent will be effective from within vim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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