3

Over the course of the day, I often open many tabs/windows open in MacVim, I forget exactly which files I've opened etc.

Then when I try to open a file I have already opened somewhere, I get something like the following:

enter image description here

So I have to go look for where I opened the file before, which is annoying (I have to go through all windows one-by-one to find its tab).

Is there a way to make MacVim just close the tab where I last opened the file automatically when I try to open the file again?

2

You can remap tabnew to tab drop so you will be switched to already opened files or directed to newly created file.

cnoreabbrev <expr> tabnew getcmdtype() == ":" && getcmdline() == "tabnew" ? "tab drop" : "tabnew" 
0

You can use the SwapExists autocmd to take an action when this situation arises. If you wanted to just ignore the message and edit the file anyway you could add the following to your ~/.vimrc

autocmd SwapExists * :let v:swapchoice='e' 

Or if you wanted to automatically recover what was in the swap file

autocmd SwapExists * :let v:swapchoice='r'

See the help (:help SwapExists) for more information on your options. They are the equivalent of clicking the options in the dialog that MacVim presents.

Typically a swap file is created when a separate vim process has the file open with edits. It may not be possible to track down that buffer in that other process, but if it were possible you can have this autocmd trigger the function or command to do it.

0

This is not a complete answer, but combined with @claytron you may be able to get it working.

Look up the drop command, e.g. :help drop. I use it in conjunction with tab (tab drop) to open my vimrc in a new tab, or to switch to that tab if it is already open:

nmap <leader>v :tab drop $MYVIMRC<CR>

So you should be able to do the following (NOT TESTED: I don't know if % will pick up the filename you are attempting to open, or the file name of the current buffer, but you get the idea):

autocmd SwapExists * :tab drop %<CR>

The only thing that remains is to test the difference between a swap file that exists because this instance of vim owns it, or because another instance of vim owns it, or because of a crash. In cases 2 and 3 you will have an infinite loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .