03 September, 2014

[Vim] Default text for any kind of file.

Have you ever wondered - "How can I avoid writing #includes and other macros in my *.cpp file in vim before every programming contest? "

Here is a simple solution:

Just type the following in your terminal and you are done ! You can personalize in any manner as you want.

1. vim /etc/vim/vimrc            
/* If you are not in superuser mode, you need to add sudo in the beginning in order to have the rights to edit it for that session. */

2. In this file, append the following autorun commands- 

"
autocmd BufNewFile *.cpp 0r ~/vim/skeleton.cpp
autocmd BufNewFile *.c 0r ~/vim/skeleton.c
"

Just scanning a quick look through this file, you can uncomment some lines (description provided) to have your vim super cool and personalized :P

3. Go to ~/vim/skeleton.c and have your skeleton here - 

"
#include<stdio.h>

int main()
{

    return 0;
}
"

4.  Go to ~/vim/skeleton.cpp and have your skeleton here - 

"
#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

int main()
{

    return 0;
}
"

Now make any *.c and *.cpp file and see how it simplifies your tedious work everytime !

PS: you can extend it to any kind of file (python, java, html, headers) and add analogous commands!

Reference: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#skeleton