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.
You can also add:
alias mvim='open -a MacVim'
to your .bash_profile
This seems like the simplest solution to me.
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?
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.
@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)
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.
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.