打开 C/C++ 的头文件来自多个目录和多个扩展名的 vim 源文件

发布于 2024-09-12 13:58:59 字数 934 浏览 5 评论 0原文

首先,我打算在 vim 中编写一长串 if/else 语句,并意识到 1) 有更好的方法来完成我想做的事情,2) 这样就成熟了并就该主题提供帮助。所以!我有各种各样的文件,比如

foo/src/file01.C
foo/src/file02.cc
foo/src/file03.c
foo/include/file01.hh
foo/include/file02.h
foo/include/file03.h

如果您注意到 C/H、cc/hh、c/h 扩展名可能匹配也可能不匹配,那么您很热衷,我希望您能提供帮助。我查看了诸如以下vim脚本之类的内容Vim wiki 上的“在源文件和头文件之间轻松切换”,虽然我只在 a.vim 中转储了几个小时但没有成功,但其他人似乎无法通过该页面上的文档进行工作。那么任何人都可以帮助如何使这项工作有效吗?

我的一个很好的线索是快速 如何在标题和源之间轻松切换 主题,但仍然无法使其发挥作用。

我想我真正想要的是如何避免多个 if 语句并使用真正的匹配来完成我想要的事情。我想查看另一个目录,如果它是源 C/C++ 文件,则查找具有任何熟悉扩展名的同名头文件,或者如果它是头文件,则查找任何常规扩展名的源文件。感谢您的帮助!

更新:我特别想在新选项卡中打开该文件。我靠 Vim 标签生活!

First off, I was about to write a long list of if/else statements in vim and realized that 1) there was a better way to do what I was trying to do and 2) SO would be ripe with help on the subject. So! I have a variety of files spread about like

foo/src/file01.C
foo/src/file02.cc
foo/src/file03.c
foo/include/file01.hh
foo/include/file02.h
foo/include/file03.h

If you notice that the C/H, cc/hh, c/h extension may or may not match then you are keen and I'd like you to please help. I've look at things like the following vim scripts from the Vim wiki for "Easily switch between source and header file" and although I only dumped a few hours into a.vim without success, it doesn't seem that the others would work via the docs on that page. So can anyone help out on how to make this work?

A good lead I had was a quick How to Easily Switch between Header and Source topic, but still couldn't make it work.

I guess what I really want is how to avoid the multiple if statements and use real matching to do what I want. I want to look into another directory and if look for a header file of the same name with any familiar extension if it was a source C/C++ file, or look for a source file of any regular extension if it was a header file. Thanks for your help!

UPDATE: I specifically want to open the file in a new tab. I live on vim tabs!

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

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

发布评论

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

评论(4

年少掌心 2024-09-19 13:58:59

我建议使用 FSwitch 插件。 https://github.com/derekwyatt/vim-fswitch
这正是您开箱即用所需的。它在不止一个方面比 a.vim 更好,重写了 a.vim 背后的想法。

您发布的链接也将其作为解决方案提供。
我刚刚将它安装到我的 vim 配置中,并且它的工作效果很好。

享受。

I recommend using the FSwitch plugin. https://github.com/derekwyatt/vim-fswitch
This does exactly what you need out of the box. It is better than a.vim in more than one way, being a rewrite of the idea behind a.vim.

The link you posted presents it as a solution, too.
I have just installed it to my vim configuration and it does its job well.

Enjoy.

奈何桥上唱咆哮 2024-09-19 13:58:59

为了确保我使用的是最新版本,我 下载了最新的 a. vim 脚本 (2.18) 并将其复制到我的 ~/.vim/plugin 目录中。

您可以在 ~/.vimrc 文件中定义某些变量,以使 a.vim 识别备用文件扩展名。

为了使示例中的文件与其替代文件相匹配,我将以下内容添加到我的 ~/.vimrc 中:

let g:alternateExtensions_C = "H,hh"
let g:alternateExtensions_hh = "C"

这些是全局变量,允许您覆盖已定义的内容。你必须定义
两种关系(它们不是双向的)。

您可以通过键入以下内容来查看当前映射:

:echo g:alternateExtensionsDict

如果需要定义其他映射,请遵循相同的模式。下划线后面的是您正在编辑的文件扩展名。双引号中是以逗号分隔的备用扩展名列表。

let g:alternateExtensions_<ext> = "<list,of,alt,ext>"

如果您有不同的目录结构,则可以通过覆盖 g:alternateSearchPath 变量来定义要搜索的路径。默认情况下已包含 ../src../include

:echo g:alternateSearchPath

要在新选项卡中打开备用文件:

:AT

顺便说一句,a.vim 脚本有很好的文档记录。您可能想打开它看看。我发现了一两个我不知道的设置,并且我已经使用它很多年了;o)

我希望这会有所帮助。

Just to make sure I was using the most current version, I downloaded the latest a.vim script (2.18) and copied it into my ~/.vim/plugin directory.

You can define certain variables in your ~/.vimrc file to get a.vim to recognize alternate file extensions.

To get the files in your example to match their alternates I added the following to my ~/.vimrc:

let g:alternateExtensions_C = "H,hh"
let g:alternateExtensions_hh = "C"

These are global variables that allow you to override what's already defined. You'll have to define
both relationships (they don't work both ways).

You can see what the current mappings are by typing:

:echo g:alternateExtensionsDict

If you need to define other mappings, follow the same pattern. What comes after the underscore is the file extension you're editing. What's in the double quotes is a comma-separated list of alternate extensions.

let g:alternateExtensions_<ext> = "<list,of,alt,ext>"

If you have a different directory structure, you can define what paths to search by overriding the g:alternateSearchPath variable. ../src and ../include are already included by default.

:echo g:alternateSearchPath

To open the alternate file in a new tab:

:AT

By the way, the a.vim script is pretty well documented. You might want to open it up and take a look. I found a setting or two that I didn't know about and I've been using it for years ;o)

I hope that helps.

德意的啸 2024-09-19 13:58:59

IMO,您最好的选择是采用现有脚本来使用 :tabf 而不是 :e 或脚本现在用来打开对应文件的任何内容。您还可以尝试将更改配置为可配置并将其提交给脚本作者。 (我很确定很多人会发现增强功能很有用。)

这让我想起了很久以前使用的一个技巧。我没有猜测相应的源文件/头文件在哪里,而是在文件顶部使用了包含对应文件相对路径的特殊注释。切换就像找到特殊注释、提取文件名并打开它一样简单。问题与您的问题类似,文件扩展名不可预测。我的理性解决方案是停止猜测并在源代码中明确表示对应项。 (现在我可能会尝试将关系表放入具有标准名称的外部文件中,并使用 向上搜索。)

IMO your best option is to adopt existing scripts to use :tabf instead of :e or whatever the scripts use right now to open the counterpart file. You can also try to make the change configurable and submit it to the script author. (I'm pretty sure many would find the enhancement useful.)

That reminded me of a trick I used very long time ago. Instead of guessing where the corresponding source/header files are, I have used at the top of file special comment containing relative path to the counterpart file. Switching was as simple as finding the special comment, extracting file name and opening it. Problem was similar to yours in that file extensions were not predictable. My rational solution was to stop guessing and denote counterparts explicitly in the source code. (This days I would have probably tried to put the relationship table into an external file with a standard name and look it up in VIM using the upward search.)

故事和酒 2024-09-19 13:58:59

两件有用的事情

:he 'path'
:he tabfind

因此,您可以

:set path=../,/usr/include/,/home/buildagent/SDKROOT/mysdk/inc
:tabfind error_codes.h

从异国情调的地方打开 error_codes.h,而不必指定它。请注意,vim 通配非常非常灵活,因此您可能不需要太多的时间

:argadd ./**/*.[h,H] | tab sall

来打开当前目录下的所有头文件,无论深度有多少层。在大树上或在树外部使用符号链接运行此命令时要小心

Two helpful things

:he 'path'
:he tabfind

So you would do

:set path=../,/usr/include/,/home/buildagent/SDKROOT/mysdk/inc
:tabfind error_codes.h

to open error_codes.h from somewhere exotic without having to specify it. Note how vim globbing is very very flexible, so you might not need mucht

:argadd ./**/*.[h,H] | tab sall

will open all header files under the current directory, regardless of how many levels deep. Be careful running this command on a large tree or with symlinks outside the tree

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