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.

davertron
  • 1,427
  • 1
  • 14
  • 28

7 Answers7

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"
Björn Winckler
  • 586
  • 5
  • 3
  • 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](http://vim.1045645.n5.nabble.com/How-do-I-open-a-file-from-the-Terminal-into-MacVim-s-current-window-td1220082.html) – matt b Aug 29 '11 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! – Tim Keating Apr 04 '12 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 '12 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 06 '13 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 '14 at 15:46
  • With MacVim.app in mt Applications directory, I'm using: `alias v="open -a MacVim.app $1"` – f1lt3r Jan 15 '16 at 21:55
  • What I know about `arglist` is, it `is a feature of Vim that lets you specify a set of files then run commands on those files.`. Why do need to set it when we open a new file? – Sam Kah Chiin Dec 12 '17 at 06:27
33

You can also add:

alias mvim='open -a MacVim'

to your .bash_profile

This seems like the simplest solution to me.

Dan
  • 1,559
  • 1
  • 15
  • 18
  • 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! – Ben Regenspan Aug 28 '12 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 '14 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 04 '15 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'`. – Christopher Camps Jul 05 '17 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?

jmorriscal
  • 181
  • 1
  • 2
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.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
bryan kennedy
  • 6,969
  • 5
  • 43
  • 64
  • This is a good solution. Would you consider also putting the actual fix in the body of the answer? Links might disappear in the future. – Ovesh Mar 25 '13 at 10:51
  • The second link no longer works. I wonder what that solution was. It sounds like the one I wanted. – Woody Sep 29 '14 at 21:25
  • https://web.archive.org/web/20110205015906/http://webexpose.org/2008/10/13/open-macvim-tabs-from-command-line/ – natchiketa Mar 12 '15 at 14:03
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)

Will
  • 4,358
  • 2
  • 20
  • 17
  • I have been trying to do this, but I cannot find the "mvim" file. What file is that referring to? – lawdawg Feb 08 '15 at 00:46
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.

Erik Henriksson
  • 737
  • 1
  • 6
  • 13
  • 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 "$@"; } ` – Hai Feng Kao Nov 28 '14 at 12:23
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.

davertron
  • 1,427
  • 1
  • 14
  • 28
  • 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 '11 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 02 '12 at 21:18