I'm trying to configure vim for programming in C. I currently use YouCompleteMe and Syntastic for programming auto-completion and syntax-checking. But I came up with a problem.

I cannot jump to a definition in a header file. I cannot open header file.

I installed sudo apt-get install exuberant-ctags and generated a tag file as follows ctags -R --exclude=.git .. But this does not work for headers.

Can you suggest a way to work with headers?


We need more details.

Header files aren't supposed to contain function definitions, in C. Function declaration, yes, but definitions, unless they are inlined, no. Could it be related to YCM? YCM isn't able to jump to a function definition that isn't in the same translation unit as the current file.

For this, you'll need to index the files in your project. ctags is one way to do it. exhuberant-ctags is no longer maintained. For C projects, you shouldn't see any differences. For C++, there is a big one: use the fork: universal-ctags instead.

BTW, I index my C++ projects with a plugin of mine that takes care of the options

ctags  --tag-relative=yes --c++-kinds=+pf --fields=+imaSft --fields-c++=+{properties} --extras=+q   -f tags -R

As you're in C, you should use cscope -- I cannot seriously use it in C++.

Note: if you just want to jump from one header file to its implementation file and the other way around, I've forked the plugin alternate.

  • As @Lucas suggested in his answer I installed a.vim and it works pretty much fine. But it seems no longer maintained, doesn't it? Actually I just need to be able to jump to a function declaration that came from a header file. And open a header file when a cursor is point to its name inside #include directive. – St.Antario Jul 4 '18 at 15:53
  • a.vim hasn't evolved in years. It lacked two features I needed. 1- the possibility to have a different value for an option when I'm working in different project. 2- and access to some internal stuff in order to implement a command in lh-cpp: :GOTOIMPL that jumps to a function definition from its declarations -- if no definition is found in the associated .c(pp) file, an empty definition is added on the fly from the declaration. – Luc Hermitte Jul 4 '18 at 15:58
  • 1
    alternate plugins can't do anything here. With tags you can jump to stdlib.h as long as you run ctags on /usr/include & all. – Luc Hermitte Jul 4 '18 at 17:04
  • 1
    BTW, you should read about :h include-search – Luc Hermitte Jul 4 '18 at 17:08
  • 1
    It's a way to proceed in order to jump to the declaration of a standard function. There are other ways (cscope (C)), source code indexers (ex/universal-ctags, clang based, LSP based...). If you just want to open a file, setting &path is almost enough. I used to index /usr/include, now I don't care much about it. – Luc Hermitte Jul 5 '18 at 16:11

There is a dedicated plugin for switching to header files: https://www.vim.org/scripts/script.php?script_id=31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.