Patterns: Code Completion using VI

Generally, I don’t much care for IDEs. In my experience, IDEs add increasingly specific features to maintain their relevance, resulting in bloated software which takes too many resources to accomplish a relatively simple task.

However, as functions get more complex, it becomes increasingly difficult to make your code match the original prototypes. This is where Vi’s “yank” and “mark” features truly shine.

Yank to Mark

We know that the vi equivalent to “copy-and-paste” is “yank-and-put”. What many people do not realize is that you can open any number of files in a single vi instance, allowing you to yank from one file and put into another.

The final piece of this puzzle is the “mark” feature. It is possible for us to create a mark on a given line with the following command:



We can then return to that line with the command:



This allows us to yank large sections of code from one place to another using the command:



This is the “yank to mark” feature.

Application 1: Code Completion

We can use the yank-to-mark feature to copy entire function prototypes back into our new code. To do this, we first navigate to the spot where our function call will go. We can then go to the header file containing the required function prototype and yank it out. Finally, we “put” the prototype into our new code, then swap the prototype values out for our new values.

For example, suppose we have a prototype that looks something like this in a file file.h:

int function(

int Value_one,

char *Value_two,

struct astructure Value_three


If we want to call this function in main.c, we can open vi like so:

vi main.c file.h

We then yank everything from “int function(” to “)” using our “yank-to-mark” feature and put it into main.c.

At that point, it is a simple matter to replace the return value and all parameters with the values that we should pass into the function, all while ensuring that the types match.

Application 2: Headers match Code

For many programmers, it is a pain to ensure that your headers and code files match exactly. This is easily achieved with yank, however.

We can copy all the code from the start to the end of the code file using the command:

gg (go to start)

yG (yank to end of file)

We can then put everything from the code file directly into the header.

We can delete all the code inside our function prototypes using the “go-to-matching” command. We can highlight the start of a code block (marked by the { brace) and hit the % key to go directly to its matching closing brace (}). Thus, we can delete all the code using the command:


Then, we can insert a semicolon to complete the function prototype.

Using this feature, we can create header files in seconds which EXACTLY match the code files to which they belong.

Note: If you’re using doxygen-style comments, you should write them directly into your code files. This way, when you update the function definitions in the code, you can perform another simple yank to replace the old prototype without having to adjust your comments again. It is a real time-saver and it allows you to see your comments wherever the function is declared.

Lesson: IDEs such as VI contain powerful tools while remaining minimally invasive. Learn to leverage these simple tools to speed up development.

Facebook Auto Publish Powered By :