如何在VIM中处理大型项目

6

我有许多层级的大型项目文件夹。有些文件名相同,但根据文件夹名称它们的功能不同。例如:

/admin/action1.php
/action1.php

第一个指的是管理任务,第二个可以被用户访问。 Vim 很快就会因为标签和缓冲区而变得混乱,移动起来也越来越困难。 我正在使用的插件是: NERDTree CtrlP MiniBuffExplorer

我在 Windows 上使用 gVim,像 YouCompleteMe 这样的插件不是一个选择。

  • NerdTree - 在大型项目的文件夹之间来回移动太笨重了
  • CtrlP - 您必须一直通过输入名称来知道您正在寻找什么
  • MiniBuffExplorer - 文件量太多,占用了屏幕上过多的空间,很难找到任何东西
  • Vim-Session - 为项目打开保存选项卡/窗口

enter image description here

如图所示,有太多的标签要记住哪个标签对应代码的哪个部分。 MiniBuffExplorer 也没有太大帮助。

所以我的问题是,您如何使用 VIM 处理大型项目以保持组织良好,最好按其功能或代码部分进行分类。有一种简洁的方法吗? 是否有一种将缓冲区或标签分组的方法,例如:

Migrations
Seeds
Admin Controllers
Admin Views
Config

请参阅如何在VIM中导航大型项目(以及许多其他类似的问题)。 - glts
我认为您也可以从重新评估工作流程中受益。除非您正在执行非常特定的批量操作,否则您需要同时保持100多个缓冲区打开的可能性很低。我已经养成了使用:bd关闭打开的缓冲区的习惯,而不是使用:q,因为如果我现在完成了文件,我希望它消失。 - asfallows
我使用 Unite.vim 打开文件,其行为非常类似于 CtrlP。模糊文件匹配使得打开正确的文件非常容易。结合一些类似 grep 的工具自由地搜索所需内容所在的文件,你可以迅速找到正确的文件。 - asfallows
谢谢你提供 :bd 的提示,我之前一直使用 <ctrl>wq 关闭窗口,这会导致缓冲区挂起。我会尝试使用插件 Unite。 - BlueNile
显示剩余2条评论
4个回答

6

我认为这里的问题并不在于工具,而是在于你如何使用它们。

  1. Tabs are not designed at all as file proxies like they are in others editors. Tabs are workspaces, allowing you to organize windows how you like them. They are the best candidates for your

    Migrations
    Seeds
    ...
    

    scenario.

    Here is one possible way to create a "Migration" tab:

    :tabnew | lcd path/to/Migration
    

    From there, all the windows you create in that tab will inherit the Migration working directory and every :e, :sp, :vs or even :vim will start from that working directory.

    Also, this will make NERDTree and netrw show the content of your local working directory by default.

    See :help seeting-tabline and :help setting-guitablabel if you want to change the name of the tab.

  2. You should use CtrlP's path mode to do the matching on the whole path rather than the filename:

    fbruse
    

    would match:

    foo/bar/user
    

    but not:

    baz/vroom/user
    

    With the tab setup above, CtrlP's suggestions should be restrained to the Migration directory, making it a lot faster.

    CtrlP is not perfect though: it can be slow in large projects so make sure you read the whole documentation.

  3. An "always on" list of open buffers may be a good idea when you have a small number of them but, like tabs, it obviously doesn't scale at all. It is vastly better to show the list when you actually want to switch buffers: less screen estate and brain cells wasted!

  4. Be aware, though, that while it is possible to define a window-local argument list, there's AFAIK no way to define a window-local buffer list. Since the argument list always leaks into the buffer list and the buffer list is global, buffer commands will always deal with the same quantity of buffers, no matter what tab you are in. Thus limiting the general usefulness of tabs.

  5. Files are not a very good metaphor for dealing with large projects: you must keep a complex symbol->file map in your head while your program is made of functions, classes, arrays, variables… not files.

    Using tags (:help tags) is a very convenient way to jump around your project:

    :tag foo
    :tag bar<Tab>
    :ptag /baz<Tab>
    

    And CtrlP's :CtrlPTags makes it almost fun.

说实话,我最喜欢的导航技巧是标签跳转,通常不使用选项卡或窗口。


这正是我使用标签页的方式。我正在使用vim-session插件,但更改标签页名称并未被保存。当我恢复会话时,所有更改都消失了。我喜欢你使用CtrlPTags的想法,我将尝试实现它,而不是仅使用文件作为参考。 - BlueNile
NerdTree有一个书签功能,因此您可以标记主文件夹并快速访问它们。 您可以使用此行配置nerdtree,始终显示书签列表 let NERDTreeShowBookmarks=1 - Tarsis Azevedo

2

你可能想看一下Vim-CtrlSpace,它非常适合处理大型项目。这个插件可以帮助你处理标签页、缓冲区、会话等等。

你可以在YouTube上查看新的演示视频


0
我正在使用Windows的gVim,但像YouCompleteMe这样的插件不是一个选择。
这个说法是错误的。请查看我的其他答案,了解有关此特定主题的更多信息。
祝你好运。

我之前试用了一下YouCompleteMe,但是没法让它正常工作。然而,在我的Mac上运行得非常好。今天早上我一直在尝试解决这个问题。我尝试了你的vim版本和你编译的YCM插件,但当我启动Vim时,却一直显示“ YouCompleteMe不可用:ycm_core太旧,请重新编译ycm_core”。 - BlueNile
你显然做错了什么。你确定在“PATH”环境变量中有“libclang.dll”吗? - Alexander Shukaev
此外,请阅读我的另一个回答中的常见陷阱部分。 - Alexander Shukaev

-1

Vim不是一个IDE;在我看来,你正在推动Vim打开并发的极限。你可以轻松设置简单的集成,允许你从IDE中打开一个文件到Vim。

我建议你在最适合的地方使用每个工具:用IDE管理和浏览大型项目,用Vim发挥其卓越的文本编辑能力!


我知道VIM不是一个集成开发环境。在VIM中,我可以做任何想做的事情,除了组织我的工作流程,以便我可以轻松地跳转到我需要的文件。 - BlueNile

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