In Vim how to switch quickly between .h and .cpp files with the same name?

后端 未结 6 2036
太阳男子
太阳男子 2020-12-23 16:55

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

  1. open a file prefix_SomeReallyLongFileName.h,
6条回答
  •  孤城傲影
    2020-12-23 17:12

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

    :e %:r.cpp
    

    where

    • % 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.


    An even shorter way (courtesy of Peter Rincker),

    :e %<.cpp
    

    Relevant documentation at :h extension-removal

提交回复
热议问题