Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

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" 
share|improve this answer
add comment

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.

share|improve this answer
add comment

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 v :tab drop $MYVIMRC

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 %

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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