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.

  • 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. Aug 29 '14 at 18:25
  • 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
    Aug 29 '14 at 18:35
  • 1
    Why do you want MacVim to quit on :q? Why don't you simply keep it running?
    – romainl
    Aug 30 '14 at 22:32
  • @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
    Sep 1 '14 at 2:54
  • 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
    Sep 1 '14 at 2:56

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

  • 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
    Aug 30 '14 at 17:05
  • 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
    Aug 30 '14 at 17:10
  • @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
    Aug 31 '14 at 0:26
  • 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
    Sep 1 '14 at 2:56
  • 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 Feb 24 '16 at 21:27

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...

  • Not a very good solution since I always have multiple MacVim windows open.
    – cmcginty
    Nov 17 '16 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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