在 Vim 中如何在同名的 .h 和 .cpp 文件之间快速切换?

问题描述:

假设我有一个包含大量 .h 和 .cpp 文件的文件夹.我经常需要执行以下操作:

Suppose I have a folder with lots of .h and .cpp files. I frequently need to do the following:

  1. 打开一个文件 prefix_SomeReallyLongFileName.h,
  2. 对其进行一些更改,
  3. 然后打开prefix_SomeReallyLongFileName.cpp.

我可以使用 :e <filename> 使用自动完成来做到这一点,但由于许多文件的前缀相同,这变得不方便.

I can do this using :e <filename> using auto-complete, but as the prefix is same for many of the files, this becomes inconvenient.

是否有一种快速的方法可以打开与当前文件同名但扩展名不同的文件?

其他人是否也遇到过这种情况,如果是,您在目录中导航 C++ 文件的首选方式是什么?谢谢.

Do other people come across this situation too, and if so what is your preferred way of navigating the C++ files in a directory? Thanks.

您可以使用 :r (root) 文件名修饰符来删除最后一个扩展名(查看 :h filename-修饰符了解更多信息)

You can use the :r (root) filename modifier which removes the last extension (check out :h filename-modifiers for more information)

:e %:r.cpp

哪里

  • % 是当前文件名的简写.
  • :r 删除扩展
  • .cpp 只是在末尾附加该字符串.
  • % is shorthand for current filename.
  • :r removes the extension
  • .cpp simply appends that string at the end.

这会有效地用另一个文件的扩展名替换当前文件的扩展名,然后使用更新的扩展名打开文件.

This effectively substitutes the current file's extension with another, then open the file with the newer extension.

一种更短的方法(由 Peter Rincker 提供),

An even shorter way (courtesy of Peter Rincker),

:e %<.cpp

相关文档位于:h extension-removal