4

I wonder if there is any method that I can use to check a function name against its prototype, which may help a lot when I make typo.

For example, I once wrote a function prototype this way:void printOneNode();;but when I defined it afterward, I make a typo and wrote:void PrintOneNode(){...}.So you see, I mistake the uppercase P for the lowercase p, and that give me link error. That little bug cost me lots of time. So I wonder if there is method in vim to help.

By the way, if language involved, I'm using C.

  • The problem is void PrintOneNode(){} becomes the declaration and definition, it is syntactically valid. So, for vim to "guess" that it was wrong, it would have to keep a list of functions declared/defined and look for "similar" names. The best that I can think of is to check the declaration when you're defining, using ctags (or similar), or use some completion tool so that you can pick the name from a list. – muru Jun 19 '15 at 23:46
  • 2
    Have you considered using syntastic? – lcd047 Jun 20 '15 at 4:00
  • @lcd047 That's excellent!! – Alex Jun 20 '15 at 4:25
  • 3
    If your compiler supports it -Wmissing-prototypes will tell you if a function is missing a prototype. – FDinoff Jun 20 '15 at 16:41
2

I think you might solve your problem with the Omni Completion function working with a program like exuberant ctags.

The idea is to generate a tags file containing the different classes, constants, fields, etc... of your code and letting Vim use this list to generate the autocomplete suggestions.

To do so:

  • Install exuberant ctags: $ sudo apt-get install exuberant-ctags (or the equivalent command for your distribution).

  • Generate the tags file: $ ctags *.c *.h a file named tagsshoud be generated on your folder.

  • Let Vim knows where is your tags file: :tags=./tags,tags with these settings Vim looks for a tags file in the directory of the current file and in the working directory. Note that you can use :set tags? to check your current configuration.

  • Call the Tag Completion: Now when writing a word and hitting <C-x><C-]> Vim will use this tags list to autocomplete your word.

Bonus: As you might have noticed your tags file will not update automatically, which means that when you write new prototypes the tag completion will not suggest them. If your codebase isn't too big you could put this line in your .vimrc:

:autocmd BufWritePost * call system("ctags -R")

That will simply regenerate the tags file each time you save a file. It is important to note that if your codebase is important, this trick might considerably slow down your workflow.

| improve this answer | |

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.