MacVim always return focus to terminal upon leaving

3

1

I'm running Mavericks and I recently started playing around with sshfs to try to boost my productivity by using MacVim (I've been using in-terminal vim for a few years now). I really like MacVim.

I only have one issue so far. I've configured MacVim to hide after closing, which is perfect. Suppose I want to edit main.cpp, I simply launch mvim main.cpp from the cmd line and MacVim opens up. When I'm done with main.cpp, I close MacVim (via :wq) and my terminal regains focus right away. Perfect.

The only issue I face is that if, while editing main.cpp, I focus to another window, say, Google Chrome to look something up, then continue editing main.cpp. After closing main.cpp, Google Chrome would regain focus rather than my terminal.

Is there any way to force MacVim to remember from where it was launched and always regain focus to that place? I will always launch MacVim from a terminal.

Amit

Posted 2014-08-29T17:59:27.253

Reputation: 371

MacVim's Vim can be run in a terminal, which would allow you to use sshfs there. All you need to do is find mvim in your path, and then make a symlink to the same target there but call it vim. That will override the Apple-provided Vim, giving you all of MacVim's features in the terminal. – echristopherson – 2014-08-29T18:25:13.043

Agreed, although one of the things I like most about MacVim GUI is the fact that I do not lose a terminal window to Vim. For example, up until I started using MacVim, I would have side-by-side terminal windows, one where vim would be open editing files, and another where I would run bash commands. Using GUI vim, I do not have to sacrifice a terminal window, it just opens. I would prefer to keep that functionality. – Amit – 2014-08-29T18:35:37.543

1Why do you want MacVim to quit on :q? Why don't you simply keep it running? – romainl – 2014-08-30T22:32:31.427

@romainl: If you leave it running in the background, then I'll have to cmd+tab into Terminal since it won't focus on it automatically. – Amit – 2014-09-01T02:54:53.480

I've +1'd your comment since it was a combination of your comment and @Fdinoff's answer that helped me fix it. Thanks! – Amit – 2014-09-01T02:56:40.817

Answers

5

You can add the following to your gvimrc or wrap in a has("gui_running") check in your vimrc

autocmd VimLeave * :!open -a Terminal

to bring Terminal to the front when MacVim quits. This uses the open command to open a new Terminal Window or bring the current one to the foreground.

Change Terminal to iTerm2 if you use iTerm2

FDinoff

Posted 2014-08-29T17:59:27.253

Reputation: 1 623

This didn't work for me. I added if has("gui_running") autocmd VimLeave * :!open -a Terminal endif to my .gvimrc and it still focuses on Chrome upon leaving. Interestingly, the same effect is not observed if I switch to Word and back to MacVim, then it goes to Terminal. It seems that only if I focus to Chrome and then back to MacVim, it'll go back to Chrome after. – Amit – 2014-08-30T17:05:49.877

I should also mention that this is especially annoying since I usually run my Chrome on a different "Desktop" than my Terminal & Macvim (in the OS X Mavericks sense, what was before Expose I believe). This means that when I close MacVim (if I had lost focus to Chrome), my entire screen changes to Chrome whereas I had wanted it to stay on the same screen and switch back to Terminal. Thought I'd add a little more clarification. – Amit – 2014-08-30T17:10:39.107

@Amit You don't need the has("gui_running") if placing it in your gvimrc. I can replicate the behavior with Terminal. I can't with iTerm2. You might consider looking at an osascript solution if you want to stick to Terminal. – FDinoff – 2014-08-31T00:26:10.023

The right answer was actually a combination of your answer and the suggestion made by @romainl in the comments above. If I set MacVim to continue running in the background after exiting using :q, then the autocmd will execute and will focus to Terminal. Before, when it had MacVim hide after :q, it wasn't focusing because apparently the autocmd wasn't executing for some reason? Thanks for everyone's input here! – Amit – 2014-09-01T02:56:06.607

The name of iTerm2 is actually iTerm.app, so in order to have it return to iTerm2, you have to do: autocmd VimLeave * :!open -a iTerm – Nayan Hajratwala – 2016-02-24T21:27:00.070

4

This might be obvious already, but if you change MacVim's preferences item "After last window closes:" to "Hide MacVim", then I think you'll get something close to the behavior you want: when you start mvim from the terminal, and then finish a file, focus will return to the Terminal as long as you don't have other MacVim windows open. At least, this gets me the behavior I prefer...

dnoor

Posted 2014-08-29T17:59:27.253

Reputation: 41

Not a very good solution since I always have multiple MacVim windows open. – cmcginty – 2016-11-17T02:58:09.783