MacVim 在现有窗口中打开文件

发布于 2024-09-14 17:52:46 字数 269 浏览 4 评论 0原文

有没有办法设置 MacVim 在正在运行的 MacVim 实例的当前窗口中打开新文件?我目前设置了 MacVim 首选项“在当前窗口的新选项卡中打开新文件”,但理想情况下我只想以“:e new_file”的方式打开新文件,而不需要选项卡。

我的主要动机是我目前在我的工作流程中使用 NERDTree 和 Bufexplorer,根本不需要选项卡。我还使用 PeepOpen,它非常棒,除了它总是根据 MacVim 的首选项打开文件,所以我能做的最好的事情就是让它在当前 MacVim 窗口中使用新选项卡打开。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

山有枢 2024-09-21 17:52:46
  1. 更新到 MacVim 7.3
  2. 进入常规首选项
  3. 在“从应用程序打开文件:”下选择“在当前窗口中”
  4. 在此选项下方的下拉菜单中选择“并设置参数列表”
  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"
迷途知返 2024-09-21 17:52:46

您还可以添加:

alias mvim='open -a MacVim'

到您的 .bash_profile

这对我来说似乎是最简单的解决方案。

You can also add:

alias mvim='open -a MacVim'

to your .bash_profile

This seems like the simplest solution to me.

为人所爱 2024-09-21 17:52:46

在看到这里的所有内容并尝试了所

mvim --help 

出现的结果后,我个人使用这样的命令。

我发现这

mvim --remote-tab-silent foo.txt

对我有用,然后我很快就在我的 .profile 中添加了一个别名。是的,如果你在选项后不给它提供一个文件,它就会呕吐,但是谁会打开一个没有名称的空白文件呢?

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?

莫相离 2024-09-21 17:52:46

您也可以考虑这个关于编辑主 mvim 脚本的技巧。

为 MacVim 改进 mvim

这个修改稍微不那么严重,也有效:

MacVim 支持 tabs,但不幸的是多次调用 `mvim
从命令行结果会打开多个单独的窗口,
而不是一个窗口中的多个选项卡。我做了以下
修改 mvim 脚本来纠正这个问题。

将以下行添加到文件顶部、注释下方
部分:

tabs=true

将文件底部的 if 结构替换为以下内容:

# 最后一步:启动 vim。
如果[“$gui”];然后
  如果 $tabs && [[ `$binary --serverlist` = "VIM" ]];然后
    exec "$binary" -g $opts --remote-tab-silent ${1:+"$@"}
  别的
    exec "$binary" -g $opts ${1:+"$@"}
  菲
别的
  执行“$binary”$opts${1:+“$@”}
菲

(来自 从命令行打开 MacVim 选项卡

显然这些都有点次优,因为当你进行 MacVim 更新。但它们帮助我在新的 Mac Vim 选项卡中从终端打开多个文件。

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.

何必那么矫情 2024-09-21 17:52:46

@Björn Winckler 的答案向您展示了如何对通过查找器和其他操作系统打开机制打开的文件执行此操作。

如果您希望它与 mvim 命令一起使用,请找到 mvim 文件并将底部的行从 更改为

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

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

将使从命令行打开的所有文件也在同一窗口中打开。

另外,如果您希望文件打开相同的缓冲区(如果该文件已经打开)而不是拆分或添加新选项卡

au VimEnter,BufWinEnter * NERDTreeFind 到您的 gvimrc (这样就不会干扰您的常规 vim)

(最后一部分要求您安装 NERDTree)

@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)

握住你手 2024-09-21 17:52:46

我是这样实现的:

在 VIM 中,有一个命令“tabo”,它使当前选项卡成为唯一的选项卡。我将以下内容添加到我的 ~/.vimrc 中:

autocmd BufWinEnter,BufNewFile * silent tabo

这使得每当我创建新缓冲区或输入新缓冲区时,当前选项卡都会自动成为唯一的选项卡。该命令不会影响我的缓冲区,因此效果正是我想要的:在 MacVim 的当前实例中打开文件并且不添加任何新选项卡。

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.

青芜 2024-09-21 17:52:46

我发现 Azriel 的答案很好,但如果文件不存在,它就不起作用。这个小函数可以做同样的事情,但你也可以创建新文件。

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

只需将其添加到您的 .bash_profile 中即可。然后,您可以编辑一个新文件 foo as

mvim foo

,它将在新选项卡中打开。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文