如何在 VIM 中的大型项目中导航
如何仅使用 VIM 管理大型项目(数百个文件)?
我个人在任何大项目中都会遇到问题。
- 有什么方法可以快速“转到文件”,最好是完成名称?
- 与“转到类定义”相同,当它在另一个文件中时,
我有点了解所有 VIM 基础知识,因此使用它编写脚本或快速编辑某些源代码没有问题。但当我必须在文件之间导航时,这对我来说真的很混乱。
How do you manage big projects (hundreds of files) using only VIM?
I personally start having problems in any larger than small project.
- is there any way to quickly 'go to file', preferably with name completition?
- same for 'go to class definition', when it is in another file
I kinda know all the VIM basics, so I don't have problem using it for writing scripts or quick editing some source code. But it gets really messy for me when I have to navigate between files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
VIM 对标签有很好的支持。为项目创建标签文件后,您可以跳转到方法、类等的定义或声明,包括跨文件跳转,所有这些都在同一编辑会话中进行。
尝试
为 C/C++ 生成标签文件,请转到 shell 提示符(我假设您的系统是 *nix/Cygwin)并键入
或
VIM has excellent support for tags. Once you have created a tags file for your project, you can jump to a definition or declaration of a method, class, etc., including jumping across files, all inside the same editing session.
Try
To generate a tags file for C/C++, go to your shell prompt (I'm assuming your system is *nix/Cygwin) and type
or
我喜欢简单的解决方案,目前我最喜欢的导航方式是:
添加到
~/.vimrc.local
然后在编辑器中键入此内容以在当前工作目录 (pwd) 中的任何位置查找该
文件还可以在文件名上使用制表符补全来查找多个选择,这使得 TextMate 转换非常愉快。
I like simple solutions, my favorite way to navigate at the moment is:
Add to
~/.vimrc.local
Then type this in the editor to find the file anywhere in the current working directory (pwd)
You can use tab-completion on the filenames to find multiple choices as well, making this TextMate convert very happy.
我使用 NERDTree(目录侧边栏)、FuzzyFinder Textmate(像 TextMate 的 CMD 一样转到文件)的组合+T) 和会话 (:h 会话) 来帮助我处理大型项目。
我建议使用一些会话帮助插件。我会提到我使用的东西,但我对它还不满意。只需谷歌“vim 会话”即可。
让 FuzzyFinder Textmate 正常工作需要注意的一件事是,它依赖于旧版本的 FuzzyFinder 插件,特别是 v2.16。任何更高的值都会出错。但这绝对是值得的。虽然它没有名称补全功能,但它的搜索很智能,因此如果我搜索
fro/time/actionsphp
,它将提取文件apps/(fro)ntend/modules/(time )_tracking/actions/(actions).class.(php)
(括号表示它匹配的内容)。它可以很容易地挑选出仅通过文件夹名称唯一的文件。I use a combination of NERDTree (directory sidebar), FuzzyFinder Textmate (go-to-file like TextMate's CMD+T), and Sessions (:h sessions) to help me deal with large projects.
I would suggest using some sessions helper plugin. I would mention what I use, but I'm not satisfied with it yet. Just Google "vim sessions".
One thing to note with getting FuzzyFinder Textmate to work is that it depends on an old version the FuzzyFinder plugin, specifically v2.16. Anything higher and you'll get errors. But it's definitely worth the trouble. While it doesn't have name completion, its search is smart so if I search for
fro/time/actionsphp
it will pull up the fileapps/(fro)ntend/modules/(time)_tracking/actions/(actions).class.(php)
(parenthesis denote what it's matching). It makes it very easy to pick out files that are only unique by their folder name.以及宝贵的 ctag 和各种相关命令。我也无法没有 项目插件,它允许您将与项目关联的感兴趣的文件放在单独的窗格中。我不记得我的设置有多少是自定义的,但如果我想打开一个名为 Debug.c 的源文件,我会点击:
我经常这样做:
通过这个相对较短的序列,我可以打开任何文件及其标题垂直分割。由于项目插件窗口只是一个文本文件,因此通过使用 Vim 的搜索功能来完成。
As well as the invaluable ctags and the various associated commands. I also couldn't live without the project plugin, which allows you to have the files of interest associated with a project in a separate pane. I can't remember how much of my setup is customised, but if I want to open a source file called Debug.c, I hit:
I often then do:
With this relatively short sequence, I can open any file and it's header in a vertical split. Since the project plugin window is just a text file, completion is achieved by using Vim's searching capability.
从 Vim 7.3 开始,
:find
命令具有制表符补全文件名功能。因此,如果您将
'path'
选项设置为包含整个项目(可能使用**
通配符以允许递归搜索子目录),那么您可以使用:find
、:sfind
、:tabfind
等命令完成后可获取项目中的任何文件。如果文件名在文本中(例如在 include 指令中),这还允许使用gf
和朋友直接跳转到文件。使用这种方法,不需要外部工具或插件来导航到特定文件。尽管如此,无可否认,它可能没有那么快或易于使用,并且没有解决跳转到定义的需要。对于定义,我使用 ctags,正如其他答案所建议的那样。
Starting in Vim 7.3, the
:find
command has tab-completion of filenames.So if you set your
'path'
option to contain your entire project (probably using the**
wildcard to allow recursively searching subdirectories), then you can use the:find
,:sfind
,:tabfind
, etc. commands with completion to get to any file in your project. This also allows jumping to files directly withgf
and friends if the file name is in your text, for example in an include directive.With this method, no external tools or plugins are needed for navigating to specific files. Although, it may admittedly not be as fast or easy to use, and doesn't address the need for jumping to definitions. For definitions, I use ctags as other answers suggest.
如果您按照其他发帖者的建议使用 ctags,请确保查看 taglist 插件。
确保您花时间阅读文档并了解按键绑定。以下是一些可以帮助您开始的操作(从 TList 窗口):
If you are using ctags as other posters have recommended, then make sure you look at the taglist plugin.
Make sure you take the time to read the docs and learn the key bindings. Here are a few to get you started (from the TList window):
丰富的 ctags。
使用 Ctrl-] 跳转到光标下的标签。
Exuberant ctags.
Use Ctrl-] to jump to the tag under the cursor.
从源文件的根目录打开
vim
并扩展path
选项以包含其中的所有子目录。例如,为 C++ 标头设置
set path+=/usr/include/c++/**
,为源目录设置set path+=**
。那么,这开启了以下多种可能性。
1) 按名称或部分文件打开文件
您可以通过
:find
可靠地使用自动完成和通配符扩展。您输入名称,它会找到该名称。这与语言无关。我相信你会喜欢它。2) 导航到 cusror 下的文件:
如果你想进入像
#include "project/path/classA.h
这样的文件路径。Ctrl-6 - 返回到最后一个
gf
或gF
之后的光标位置3) API 查找并导航到 API 位置
[i
或[I
可用于查找光标下单词的函数签名,而无需离开工作区[
实际使用 Ctrl-6 返回上一个位置在不扩展
path
的情况下,您可以通过:Ex
命令开始导航文件并导航并打开文件。不过,我更喜欢NerdTree
。Opening
vim
from root of your source file and extendingpath
option to include all sub-directories therein.For example
set path+=/usr/include/c++/**
for C++ headers andset path+=**
for your source directory.This ,then, opens a plethora of following possibilities.
1) Opening file by name or parts of it
You can use auto-completion and wildcard expansion with
:find
reliably. You type the name, it will locate the name. This works language agnostic.I am sure you will like it.2) Navigating to files under cusror:
if you want to go a file path like
#include "project/path/classA.h
.Ctrl-6 - to come back to last cursor position after
gf
orgF
3) API lookup and navigating to the API location
[i
or[I
can be used to look up your function signature for word under cursor without leaving your workspace.[<Tab>
to actually go to declaration. Use Ctrl-6 to come back to last location.Without extending
path
, you can start navigating files by:Ex
command and navigate and open your file. I preferNerdTree
over this though.我使用 FindFile。如果您在项目的根目录下打开 vim 并运行
:FC 。
该插件将缓存您的 cwd 下的所有文件名。然后,您可以执行:FF
打开完成菜单并键入所需文件的名称(或者更确切地说,前几个字母)。I use FindFile. If you open vim at the root of your project and run
:FC .
the plugin will cache all the filenames beneath your cwd. You can then do:FF
to open a completion menu and type the name of the file you want (or rather, the first few letters).虽然我有点希望有人能指出一个更好的解决方案,以便我可以学到一些东西,但 NERDTree 对我来说很好,只要我扩展了树,就可以通过名称完成来获取特定文件。当我需要访问文件时的命令类似于:
,d/foo.pyo (其中 foo.py 是文件名)
,d 打开树, / 进入搜索模式,名称(或部分名称,或正则表达式,或其他)的文件,然后 o 打开。
当然,如果您没有输入足够的文件名或有重复的文件名,您可能需要按几次“n”。
我承认,像这样使用 NERDTree 感觉有点像黑客,尽管它已经深入我的肌肉记忆,我什至没有想到它。
当然,我也使用 ctags,但这些仅当您在光标附近有一个函数并且需要在另一个文件或其他文件中获取其定义时才有用。很多时候我会说“好吧,我现在需要处理功能 x”,并且需要导航到另一个文件,而附近没有任何 ctags 真正有帮助的引用。
Although I'm kinda hoping someone will point out a better solution so I can learn something, NERDTree has been good to me for getting to specific files with name completion as long as I have the tree expanded. The command when I need to get to a file is something like:
,d/foo.pyo (where foo.py is a file name)
,d to open the tree, / to enter search mode, the name (or partial name, or regex, or whatever) of the file, and then o to open.
Of course you may have to hit 'n' a few times if you didn't type enough of the filename or there are duplicates.
I admit it feels like a bit of a hack using NERDTree like this although it has gotten so far into my muscle memory by now that I don't even think about it.
Of course I use ctags too but those are only useful when you have a function near the cursor and need to get to its definition in another file or something. A lot of times I say "OK, I need to work on feature x now" and need to navigate to another file without any references nearby that ctags would really help with.
我正在使用我的两个插件:
的标记选择器。您可能想检查著名的 taglist 插件。两者都需要我的 viml 库 lh-vim-lib。
I'm using two plugins of mine:
<c-w><m-down>
by default. You may want to check the renowned taglist plugin instead.Both require my viml library lh-vim-lib.
尝试 SourceCodeObedinece。这是我开发的用于处理 C++ 1Gb 源文件项目的项目。
我将它与 0scan 一起使用。
这两个插件是最流行的 Vim 浏览工具的包装: ctags 和 cscope。
Try SourceCodeObedinece. This one I developed to handle C++ 1Gb source files project.
I use it in pair with 0scan.
These two plugins are wrappers around the most popular Vim browsing tools: ctags and cscope.