junghan
junghan

Reputation: 39

vim autowrite functions in header file. C lang

I tried to use vim for c program editing. Is there a way to auto write function skeleton defined in header file?

situations like

"my_code.h"
int temp(int*);

and "my_code.c" <<< here auto write >>> like

int temp(int*) { return }
int main()
{
}

I'm using c.vim plug-in. I tried to find it, but I couldn't make it.

Upvotes: 2

Views: 974

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32976

I've have a :GOTOIMPL command in lh-cpp that generates an empty function definition from a function declaration.

However, you'll have to execute the command on each function declaration and go back to the header file. I've never took the time to batch the process from an header file and no implementation file -- as this is not a use case I have as there exist other solutions...

IOW... there exist projects that do the job from the command-line (and which you could call from vim then) (like for instance https://github.com/Davidbrcz/header-expander), or even other plugins (like protodef: http://www.vim.org/scripts/script.php?script_id=2624).

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172768

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.

There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.


Additionally, there are also template plugins that pre-initialize a new, empty file with a skeleton, often including a file header and copyright statement. Search vim.org; you'll find plenty.

Upvotes: 2

Cloud
Cloud

Reputation: 19331

There are code completion scripts, yes.. However, this is not something you generally want. It works for simple things like basic C functions, and fails horribly beyond that (i.e. templates etc in ). You don't save any time by using such plugins, and mastering motion/yank/paste commands provide the same result in the same amount of time, and you become more familiar with a modal editor. Is it that hard to copy-paste the function prototype and add some braces {/}?

If you want something to help as a reminder to write function definitions to go with function prototypes, consider using the taglist plugin.

enter image description here

enter image description here

Upvotes: 2

Related Questions