在Macvim中以选项卡形式打开文件

4
我正在尝试使用Janus构建的MacVim学习。我已经完成了Vim教程,现在想深入学习并创建一些简单的网站。我的第一个项目是一个将罗马数字转换为阿拉伯数字的网站。但是,我似乎无法做最简单的事情——在一个窗口中的不同标签页中创建新文件,包括html、css和js。我可以通过终端创建空白文件,然后打开Finder并将这些文件拖到打开的MacVim窗口中,以达到我的目的,但这似乎是一种非常复杂的方法。我想要做的是启动MacVim,并在打开的窗口中创建我的空白html文件,然后在相邻的标签页中创建一个空白的css文件,再在第三个相邻的标签页中创建一个空白的js文件,然后开始工作。但是,当我在MacVim中使用命令行创建新文件(:!mvim roman.html)时,我得到了一个新窗口。因此,我最终得到了一系列窗口而不是一系列标签页。我在谷歌上搜索了一下,发现其他人也遇到了这个问题。他们的解决办法是修改.bashrc或.bash_profie,使用别名(MacVim Open File In Existing Window),但是当我尝试这样做并尝试在MacVim中打开一个文件时,我得到了一个充满垃圾的文件,而不是空文件。我还在尝试理解NERDTRee。也许那里有一个简单的解决方案,但我刚开始探索它。任何指导都将不胜感激。谢谢。
3个回答

9

您的要求不是很多,但我们还是来吧...

  • Drop Janus as soon as you can. This piece of shit gives you a false sense of comfort while actively preventing you from learning how to use Vim and making it a lot harder than necessary to customize it to your liking.

  • Vim's tabs are not like tabs in other editors: they are not and can't be 1-to-1 proxies for files. In Vim, a file is loaded in a "buffer" and that buffer may or may not be displayed in zero or more "windows", in zero or more "tabs". This particularity will probably bite you one of these days so you'd better get used to deal with buffers.

  • In Vim, creating a new file works the same as in any editor: you edit a new empty file and, when you are done, you write it to disk.

    :e[dit] file.html
    (editing)
    :w[rite]
    

    Use :sp[lit] file.js to edit file.js in a new horizontally window.

    Use :vs[plit] file.html to edit file.html in a new vertical window.

    Use :tabe[dit] file.css to edit file.css in a new tab.

    If you want to postpone the decision of the filename, :enew, :new, :vnew and :tabnew create empty buffers in place, in an horizontal window, in a vertical one and a tab.

    You don't really need to create those files from outside of Vim.

  • From the Finder, the simplest way to edit a file in MacVim is to right-click on it and choose "Edit in MacVim". Same for three files.

    There is a drop menu, somewhere in MacVim's Preferences window, that lets you define the default behavior when MacVim is launched by other programs. Try it.

    Since you seem to have installed the mvim script, editing a file in the MacVim GUI is done with $ mvim filename and editing the same file directly in your shell is done with $ mvim -v filename.

    You can also open multiples files (even if they don't exist) in their own tabs from your shell:

    $ mvim -p file1 file2 file3
    
  • Your command, :!mvim filename, does exactly what it's supposed to do: it launches a new MacVim instance.

  • Vim already comes with a file explorer that does a lot more than NERDTree called Netrw. See :help netrw.

  • Did I tell you that you shouldn't use tabs if you don't have a solid understanding of what they are and what they do?


1
哇!感谢您抽出时间提供反馈。但现在我有更多问题。1. 如果选项卡不等同于缓冲区,那么我应该如何处理多个文件,例如相关的html、css文件?多个窗口似乎并不能解决这个问题。2. 我想在摆脱Janus之前先了解一下它是什么,但是当时机成熟时,我该如何删除它呢?3. 我希望从一组核心配置中得到一些支持,以便在html、css甚至javascript中工作——例如,颜色编码、缩进、自动完成。有更好的方法来实现这一点吗? - user1936292
1
简单来说:与像TextMate或其他编辑器中需要切换选项卡不同,你可以通过切换缓冲区来实现。使用:b <Tab>命令可以方便地进行切换。我从事前端开发工作,我的设置很简单:HTML代码在一个竖直窗口中占据了一半的可用空间,另一半则是CSS或JS,具体取决于我正在处理的功能。我很少需要同时查看CSS和JS,但HTML是必须的,因为这两个文件都涉及HTML结构。所以,我认为两个竖直窗口是最好的设置。当然,这里指的是分割窗口,而不是GUI窗口。 - romainl
我不知道如何移除那个“癌症”,也许它在他们的README文件中。相信我,你不需要那个东西。 - romainl
1
所有这些都是内置的:不需要添加任何东西,当然也不需要一个可怕的发行版。你只需要在你的~/.vimrc中加入filetype plugin indent onsyntax on就可以获得语法高亮、自动完成和正确的缩进规则。如果你想要一个代码检查器,有许多插件可用。再次强调,不需要一个可怕的发行版! - romainl
  1. 是的,这是一个糟糕的工作流程。由于你的JS文件在HTML中被调用,你可以在Firebug的控制台中与你的代码交互。这非常容易和方便,而且你显然不需要复制/粘贴任何东西。这就像Web Dev 101...有很多插件旨在让你在Vim中获得REPL 感觉,试试它们。我最近发现了Pippe2Eval,它在JavaScript中运行得非常好。
- romainl
显示剩余2条评论

7
我认为你想要的命令是 :tabe something.html。这会在当前窗口中创建一个新标签,并将文件 something.html 放入其中,如果不存在,则会创建一个新文件。(实际上直到保存该文件之前都不会创建新文件)。
如果喜欢使用选项卡,阅读 :help tab-page-commands 可能值得你的时间。

是的,就是这样。实际上这并不可能更简单了。我原本以为我需要编写一个30行的脚本之类的东西。感谢帮助命令。我看过其他的命令,但没有看到这个。它还向我展示了如何重新打开我创建的那些文件!太棒了! - user1936292

0

Command + T

在MacVim中创建一个新的标签页。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接