在 /usr/include/files 周围导航时使用 Vim 查找定义

Using Vim to find definition when navigation around /usr/include/files

我发现当您想在 linux 下做某事时,在头文件之间导航很随意。当我想找到某物的定义时,例如 struct sockaddr_storage。我现在正在做的是:!find /usr/include/ -type f | xargs grep " struct sockaddr "。它很强大,但对我来说没有说服力。

有没有像其他 IDE(例如 VStudio)提供的更简单的方法来浏览 .h 文件? (我几乎是 C/CPP 用户)ctags 和 cscope 或 tagbar 是供项目使用的,在这种情况下它们对我有好处吗?

你需要的是一个标签工具,比如ctags or gtags

Vim 可以跟随包含文件并在定义它们的地方找到符号。为此,它使用了许多选项:

:help 'path'
:help 'include'
:help 'define'

整个机制在 :help include-search 下进行了描述。

在你的情况下,假设你在当前缓冲区中有 #include <sys/socket.h>,你会做:

:ijump sockaddr_storage

或:

:isplit sockaddr_storage

或:

:isp /socka

或将光标放在 sockaddr_storage 上按 [I

C 的 define 选项的默认值限于 #define,但它可以扩展为包括 structint 等内容

:set define=^\s*#*\s*\(define\\|struct\)

这让您可以执行以下操作:

:dlist sockaddr_storage

另一种方法显然是使用像 ctags (:help ctags), gnu global, or cscope (:help cscope) 这样的代码索引器来索引您的项目 /usr/include/.