MacVim Open File In Existing Window

蹲街弑〆低调 提交于 2019-11-28 16:11:30
  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"

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?

bryan kennedy

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!