52

Is there a way to set up MacVim to open a new file in the current window in a running MacVim instance? I currently have the MacVim preference "Open new files in a new tab in the current window" set, but ideally I'd just like to open new files the way ":e new_file" works, without the tabs.

My main motivation is that I currently use NERDTree and Bufexplorer for my workflow and don't need the tabs at all. I'm also using PeepOpen, which is awesome except it always opens files based on MacVim's preferences, so the best I can do is get it to open in the current MacVim window with a new tab.

1

7 Answers 7

54
  1. Update to MacVim 7.3
  2. Go into the General Preferences
  3. Under "Open files from applications:" choose "in the current window"
  4. In the pull down menu below this option select "and set the arglist"
7
  • 25
    This only works for files opened in Finder - executing mvim filename.txt from the Terminal still opens multiple windows. See this mailing list discussion
    – matt b
    Aug 29, 2011 at 15:07
  • 12
    Command line was exactly the problem I was having. To summarize that link: add alias vi="open \"mvim://open?url=file://$1\"" to your .bash_profile and then when you do "vi foo" it will open foo in a new tab in your existing MacVim window. Awesome! Apr 4, 2012 at 16:39
  • 4
    i learned you can also do mvim --servername VIM --remote path/to/file.txt (re: @mattb thanks for the link to that mailing list discussion. ) to open in the current window. You just have to know the window's name which you can find at the top next to the filename and directory
    – Will
    Aug 16, 2012 at 18:16
  • I had to alter @TimKeating’s approach a bit to make it work for me: I added 'alias tv="open -t $1"' to my profile. The '-t' option causes the file to be opened in the default text editor (which in my case is MacVim).
    – spirossi
    Aug 6, 2013 at 15:21
  • @TimKeating: What about for new files? With this alias, if I type vim ~/foo then terminal will error out with The file /Users/hobbes3/foo does not exist.. With normal vim, it would create a new file called foo.
    – hobbes3
    Sep 15, 2014 at 15:46
33

You can also add:

alias mvim='open -a MacVim'

to your .bash_profile

This seems like the simplest solution to me.

4
  • 4
    Yes - this plus setting "Preferences" -> "Open files from applications" to "and set the arglist" (or "with a tab for each file" for those who do like tabs -- opening in split window is also an option) works great, thanks! Aug 28, 2012 at 21:03
  • 1
    What about for new files? With this alias, if I type vim ~/foo then terminal will error out with The file /Users/hobbes3/foo does not exist.. With normal vim, it would create a new file called foo.
    – hobbes3
    Sep 15, 2014 at 15:45
  • @hobbes3 I find in most cases opening a specific file is a bad idea. I almost always find myself navigating into a directory and then executing mvim .. This opens the current directory in vim and sets the working directory to the same directory. From there you can create new files inside vim using :e foo or NerdTree or what have you. Or you can always touch ~/foo first if you really want to create the file from the command line.
    – Dan
    Aug 4, 2015 at 21:53
  • 2
    Works great but I was ending up with multiple copies of MacVim.app running because of homebrew versioning. My solution was to change it to alias mvim='open -a /Applications/MacVim.app'. Jul 5, 2017 at 18:04
18

I personally use a command like this, after seeing everything here and resorting to experimenting with what

mvim --help 

turned up.

I found that

mvim --remote-tab-silent foo.txt

worked for me and then I soon added an alias to my .profile. Yes, it barfs if you don't feed it a file after the option, but who opens a blank file with no name anyway?

2
  • 2
    Thank you! The previous suggestions didn't give me what I wanted but this did! +1 :-)
    – SaxDaddy
    Aug 21, 2014 at 1:38
  • 2
    mvim --remote-silent foo.txt for those that prefer no tabs.
    – matlehmann
    Jun 26, 2015 at 12:16
16

You might also consider this tip on editing the main mvim script.

Improving mvim for MacVim

This modification is a bit less severe and also works:

MacVim supports tabs, but unfortunately calling `mvim multiple times from the command-line results in multiple separate windows opening, instead of multiple tabs in one window. I made the following modifications to the mvim script to correct this.

Add the following line to the top of the file, below the commented section:

tabs=true

Replace the if structure at the bottom of the file with the following:

# Last step:  fire up vim.
if [ "$gui" ]; then
  if $tabs && [[ `$binary --serverlist` = "VIM" ]]; then
    exec "$binary" -g $opts --remote-tab-silent ${1:+"$@"}
  else
    exec "$binary" -g $opts ${1:+"$@"}
  fi
else
  exec "$binary" $opts ${1:+"$@"}
fi

(from Open MacVim tabs from command-line)

Obviously these are both a bit sub-optimal b/c you'll have to maintain the hack when you do a MacVim update. But they helped me a bunch in opening multiple files from the Terminal in new Mac Vim tabs.

3
4

@Björn Winckler's answer shows you how to do it for files opened through finder and other OS opening mechanisms.

If you want it to work with the mvim command find the mvim file and changes the lines at the bottom from

if [ "$gui" ]; then
    # Note: this isn't perfect, because any error output goes to the
    # terminal instead of the console log.
    # But if you use open instead, you will need to fully qualify the
    # path names for any filenames you specify, which is hard.
    exec "$binary" -g $opts ${1:+"$@"}
else
    exec "$binary" $opts ${1:+"$@"}
fi

to

if [ "$gui" ]; then
  # Note: this isn't perfect, because any error output goes to the
  # terminal instead of the console log.
  # But if you use open instead, you will need to fully qualify the
  # path names for any filenames you specify, which is hard.

  #make macvim open stuff in the same window instead of new ones
  if $tabs && [[ `$binary --serverlist` = "VIM" ]]; then
    exec "$binary" -g $opts --remote ${1:+"$@"}
  else
    exec "$binary" -g $opts ${1:+"$@"}
  fi
else
  exec "$binary" $opts ${1:+"$@"}
fi

This will make all files opened from the command line open in the same window as well.

Also if you would like the file to open the same buffer if that file is already open in stead of splitting or adding a new tab

au VimEnter,BufWinEnter * NERDTreeFind to your gvimrc (so not to interfere with your regular vim)

(this last part requires you to have NERDTree installed)

1
  • I have been trying to do this, but I cannot find the "mvim" file. What file is that referring to?
    – lawdawg
    Feb 8, 2015 at 0:46
1

This is how I accomplished this:

In VIM, there's a command "tabo", which makes the current tab the only tab. I added the following to my ~/.vimrc:

autocmd BufWinEnter,BufNewFile * silent tabo

This makes it so that any time I create a new buffer or enter a new buffer, the current tab becomes the only tab automatically. This command doesn't affect my buffers, so the effect is exactly what I want: open a file in the current instance of MacVim and don't add any new tabs.

2
  • Yes as @peritus mentions, this is not a good solution at all, as basically what it's saying is "whenever you open a new buffer, set the current tab to the only tab", which effectively disables tabs altogether. This was fine for me since I never used tabs, but other users should be aware. See the answer from @bjorn-winckler above for a real solution.
    – davertron
    Jun 20, 2011 at 12:48
  • 1
    Yes. This is definitely a warning for those that like tabs, but a massive +1 for those of us that use better methods than tabs :)
    – kenny
    Apr 2, 2012 at 21:18
1

I found Azriel's answer great but it does not work if the file does not exist. This little function does the same thing but you can also create new files.

mvim () { touch "$@" && open -a MacVim "$@"; }

Just add it in your .bash_profile. You can then edit a new file foo as

mvim foo

and it will open in a new tab.

1
  • The above command doesn't work if the argument is empty. I altered it a bit to make it open an untitled file when the filename is not specified. mvim () { ([[ -z $@ ]] || touch "$@" ) && open -a MacVim "$@"; } Nov 28, 2014 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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