I recently discovered -p and --remote-tab. Since I edit loads of files at the same time, sometimes with over 50 gvim instances open (and I am not a fan of buffers), I could use a bit of decluttering. However, at the same time, I don't want to push them all in a single instance. In other words, I want to open files in gvim tabs, but keep the ability to have multiple instances and control in which one I open any new file(s) I decide to open. That way I could group files in gvim instances the way I see fit.

My intended approach was to make two simple aliases in .bashrc:

alias gvim="gvim -p"
alias gvimt="gvim --remote-tab"

What I expected was that gvim paths/to/some/files* would open a new instance with those files in tabs, and that works as expected. However, I also expected gvimt paths/to/some/files* to do the same in my last active gvim instance, but that did not happen. Instead, it seems to pick some default instance (possibly the first one I opened; not sure).

How do I make gvimt, either through that alias or through vimrc settings (but, ideally, without vim plugins), open files in tabs of the last active instance ("window", in window manager terms) instead of whatever is the default?


For completeness, the following maps work nicely with the accepted solution below:

" Ctrl+Tab and Ctrl+Shift+Tab
map <C-Tab> gt
map <C-S-Tab> gT
map! <C-Tab> <C-O>gt
map! <C-S-Tab> <C-O>gT

These make Ctrl+Tab and Ctrl+Shift+Tab behave as they usually do in editors (next/previous tab, respectively).

1 Answer 1


Write the server's name to a file whenever Vim gets input focus

function NameToFileWhenActive()
    redir! > ~/.vim.active| echon v:servername | redir END
autocmd FocusGained * silent call NameToFileWhenActive()

The redir line simply writes the server's name to the file. It needs to be in a function if we want to silence it (otherwise it pops up in the last line every single time).

The alias

alias gvimt='gvim --servername "$(cat ~/.vim.active)" --remote-tab'

In Bash you can use its native construct "$(<~/.vim.active)" to avoid spawning a new process.


The documentation says FocusGained is only certain to work in Gvim, which you are using. But console Vim users have good alternatives, such as CursorMoved and TextChanged.


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.