Fedora iTOps Tube

Thursday, February 23, 2012

Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin

This article is part of the ongoing 
Vi / Vim Tips and Tricks Series. As a programmer, you may do lot of repetitive tasks while coding such as:

§  Adding file header

§  Adding function/frame comment

§  Including default code snippet

§  Performing syntax check

§  Reading documentation about a function

§  Converting a code block to comment, and vice versa

The C-Support Vim Plugin offers easiest way to do all of the above, saving lot of time and keystrokes for C and C++ programmers.

The plugin was written by Fritz Mehner, who explains the purpose of the plugin as: “Write and run programs. Insert statements, idioms, comments”.

He also highlights following features:

§  Statement oriented editing of C / C++ programs

§  Speed up writing new code considerably.

§  Write code and comments with a professional appearance from the beginning.

§  Use code snippets

This article explains how to install the plugin in 3 easy steps and 7 powerful features of the plugin.


3 Steps to Install the C.Vim Plugin


Step 1: Download C Vim Plugin

Download the plugin from vim.org website.


$ cd /usr/src
$ wget http://www.vim.org/scripts/download_script.php?src_id=9679


Step 2: Install the C Vim Plugin

$ mkdir ~/.vim
$ cd ~/.vim
$ unzip /usr/src/cvim.zip


Step 3: Enable the plugin in the ~/.vimrc

Add the following line to the ~/.vimrc to enable the plugin for Vim editor.

$ vim ~/.vimrc
filetype plugin on


8 Powerful Features of C.Vim Plugin

Feature 1: Add Automatic Header to *.c file

When you open a file with the extension .c it opens the file with header as shown below. This will also place the cursor in the Description field in Insert mode.

$ vim myprogram.c
* =================================================
*       Filename:  myprogram.c
*    Description:
*        Version:  1.0
*        Created:  01/19/09 20:23:25
*       Revision:  none
*       Compiler:  gcc
*         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*        Company:  FH Südwestfalen, Iserlohn
* =================================================

To change the default value of the AUTHOR and COMPANY, modify the default value in ~/.vim/c-support/templates/Templates


$ vim ~/.vim/c-support/templates/Templates
|AUTHOR|    = geekstuff
|EMAIL|     = subscribe@geekstuff
|COMPANY|   = thegeekstuff.com

Now, when you create a new c file, it will show the modified values for AUTHOR and COMPANY as shown below.


$ vim myprogram.c
* =================================================
*       Filename:  myprogram.c
*    Description:
*        Version:  1.0
*        Created:  01/19/09 20:26:43
*       Revision:  none
*       Compiler:  gcc
*         Author:  geekstuff (gk), subscribe@geekstuff
*        Company:  thegeekstuff.com
* =================================================

Note: To add custom fields to the header, modify the ~/.vim/c-support/templates/file-description.template file and add your own custom field.


Feature 2: Adding C function using \if

For writing a subroutine, type \if in normal mode, which will prompt for the function name (as shown in Fig1 below) and inserts the subroutine with default function content (as shown in Fig2 below).

Fig1:Insert C Function Automatically

Fig 2:Insert C Function Automatically


Feature 3: Insert main Function Header using \im

For inserting main function, type \im in normal mode, which will add the main function as shown below.

Fig 3: Insert C main function automatically


Feature 4: Insert a Function Header using \cfu

For inserting a function header, type \cfu in normal mode, which will ask the function name as shown in Fig 4, and adds the comment as shown in Fig 5.

Fig 4: Insert C Function Header Automatically

Fig 5: Insert C Function Header Automatically

Feature 5: Add a Frame comment using \cfr

To add a frame comment, type \cfr in normal mode, which will give the following formatted comment.

Fig 6: Insert a Frame Comment Automatically


Feature 6: To include header file, use \p<

Type \p< in the normal mode, which will include the text “#include <>”, and places the cursor in the < symbol in Insert mode where you can type the header file name.


Feature 7: Save the file, compile it and execute it immediately.

To save and compile the file use \rc.

To run use \rr.


Feature 8: Insert pre-defined code-snippet to the C code using \nr

The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.


$ ls ~/.vim/c-support/codesnippets
Makefile                        calloc_double_matrix.c  main.c   print_double_array.c.noindent
Makefile.multi-target.template  calloc_int_matrix.c     main.cc  print_int_array.c.noindent

For example, if you want to create a function that will Allocate a dynamic int-matrix of size rows*columns; return a pointer, you can re-use it from the existing code snippets. Following is the content of the calloc_int_matrix.c pre-defined code snippets.


* ===  FUNCTION  ======================================================================
*         Name:  calloc_int_matrix
*  Description:  Allocate a dynamic int-matrix of size rows*columns; return a pointer.
* =====================================================================================
calloc_int_matrix ( int rows, int columns )
int   i;
int **m;
m     = calloc ( rows, sizeof(int*) );        /* allocate pointer array     */
assert( m != NULL );                          /* abort if allocation failed */
*m    = calloc ( rows*columns, sizeof(int) ); /* allocate data array        */
assert(*m != NULL );                          /* abort if allocation failed */
for ( i=1; i
m[i]  = m[i-1] + columns;
return m;
}  /* ----------  end of function calloc_int_matrix  ---------- */

To insert this into your working c program, type \nr from the normal mode inside vim, which will prompt “read snippet /home/ramesh/.vim/c-support/codesnippets/”, type calloc_int_matrix.c at the end and press enter, which will insert the content of the ~/.vim/c-support/codesnippets/ calloc_int_matrix.c to your working file automatically.

Note: You can define your own code snippets and place it under ~/.vim/c-support/codesnippets/. You can also build your own code snippets from the existing code – select the part of code need to be made as code snippet, press \nw, and give a file-name to it. From next time, type \nr and the file-name to get your custom code snippet.

There are lot of powerful features in the C-Support Vim Plugin. Read the documentation for more information. The documentation is located in the following location on your system.


§  README : ~/.vim/README.csupport

§  PDF : ~/.vim/c-support/doc/c-hotkeys.pdf

§  Online c-support vim plugin documentation

§  This plugin comes with a help file (csupport.txt) which can be viewed by :h csupport

§  Additional Screenshots of this plug-in.


Overview Of PoE - Power Over Ethernet Concepts and Devices List


1. What is PoE – Power Over Ethernet?

Power over Ethernet (PoE) does exactly what it says — it carries power over ethernet cables. So, an ethernet cables caries both data and current together to operate devices like wi-fi access points. This is very helpful to install wi-fi access points in ceilings, where it is hard to find power outlets. Only smaller devices can be powered using the PoE, as it carries limited power over the ethernet.

Fig 1: A Typical PoE Setup (Image From: PowerOverEthernet.com)


2. How does PoE works?

Following are the terminologies used in the PoE network:

§  PD – Powered Devices: PDs are end devices that can accept the power transmitted over Ethernet Cat-5 cable.

§  PSE – Power Source Equipment: PSEs add power to the ethernet cable.

§  Midspan device: These are power injectors that stands between the standard ethernet switch and the end device.

§  Endspan devices (PoE Switches): These are Ethernet Switches that includes the circuit to inject power to the ethernet cable.

Cat5 ethernet cable has 4 twisted pairs. Out of 4 pairs:

§  2 pairs are used for data transfer,

§  2 pairs are spare and not used.

There are two approaches to transfer power over ethernet.

Approach 1 uses data pairs:

The same 2 pairs used for data transfer, is used to carry power. The PSE injects power to the 2 data pairs (pin 1,2 and pin 3,6) via the center tap of the transformers without affecting the data transfer.

Fig 2: PoE Approach 1 uses data pairs to send power (Image From: PowerOverEthernet.com)

Approach 2 uses the spare pairs:

The 2 spare pairs that are not used, is used to carry power. It applies +ve voltage to pins 4 and 5 of ethernet cable. It applies -ve voltage to pins 7 and 8.

Fig 3: PoE Approach 2 uses spare pairs to send power (Image From: PowerOverEthernet.com)


3. What are the Benefits of PoE?

§  Portability: Install end-devices where it is hard to get power.  For example, you can install a wireless access point in the ceiling, where you cannot find power outlet.

§  Cost Savings: For a large scale deployment of wireless access point, you don’t need to install power outlet for the access points, which can be a huge cost saving.

§  Simplicity: You need to run only ethernet cable to the end devices, which eliminates the cable clutter.

§  Safer: No AC power is involved, as you don’t need to install A/C power outlet for the end-devices.

§  Easy Maintenance: You can restart the end-device remotely. No need to press the power button to reset the equipment.  You can easily move the end-devices wherever you can lay a LAN cable.


4. Sample List of Equipments that uses PoE.

PoE Adapters:

§  D-Link Power over Ethernet (PoE) Adapter – DWL-P50

§  D-Link Power Over Ethernet Adapter – DWL-P200

§  Linksys Power Over Ethernet Adapter Kit

PoE Wireless Access Point:

§  D-Link PoE Wireless Access Point – DWL-2200AP

§  D-Link PoE Outdoor Wireless Access Point – DWL-7700AP

PoE Switch:

§  Netgear 24 Port Switch with 2 Gigabit Ports and 12 Port POE – FS726TP ProSafe

PoE Network Surveillance Camera:

§  Airlink Power over Ethernet Network Camera – SkyIPCam310

§  Panasonic 21x Optical Zoom Pan/Tilt Network Camera – BB-HCM580A

§  Mobotix Outdoor, Wide Angle Lens, Motion Detection Camera D22M-IT

PoE VoIP Phone:

§  Polycom IP Phone with Integrated PoE – IP550

PowerOverEthernet.com site maintains a Huge List of all PoE products, including a PoE Analog Clocks and PoE Gas Detector.

Additional PoE References

§  PoE White Paper from PowerOverEthernet.com

§  PoE on Wikipedia


How To Backup and Restore PostgreSQL Database Using pg_dump and psql


pg_dump is an effective tool to backup postgres database. It creates a *.sql file with CREATE TABLE, ALTER TABLE, and COPY SQL statements of source database. To restore these dumps psql command is enough.

Using pg_dump, you can backup a local database and restore it on a remote database at the same time, using a single command. In this article, let us review several practical examples on how to use pg_dump to backup and restore.

For the impatient, here is the quick snippet of how backup and restore postgres database using pg_dump and psql:

Backup:  $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}


Restore: $ psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}

How To Backup Postgres Database

1. Backup a single postgres database

This example will backup erp database that belongs to user geekstuff, to the file mydb.sql

$ pg_dump -U geekstuff erp -f mydb.sql

It prompts for password, after authentication mydb.sql got created with create table, alter table and copy commands for all the tables in the erp database. Following is a partial output of mydb.sql showing the dump information of employee_details table.


-- Name: employee_details; Type: TABLE; Schema: public; Owner: geekstuff; Tablespace:



CREATE TABLE employee_details (

employee_name character varying(100),

emp_id integer NOT NULL,

designation character varying(50),

comments text



ALTER TABLE public.employee_details OWNER TO geekstuff;



-- Data for Name: employee_details; Type: TABLE DATA; Schema: public; Owner: geekstuff


COPY employee_details (employee_name, emp_id, designation, comments) FROM stdin;

geekstuff 1001 trainer

ramesh 1002 author

sathiya 1003 reader



-- Name: employee_details_pkey; Type: CONSTRAINT; Schema: public; Owner: geekstuff; Tablespace:


ALTER TABLE ONLY employee_details


ADD CONSTRAINT employee_details_pkey PRIMARY KEY (emp_id);

2. Backup all postgres databases

To backup all databases, list out all the available databases as shown below.

Login as postgres / psql user:

$ su postgres

List the databases:

$ psql -l


List of databases

Name | Owner | Encoding


article | sathiya | UTF8

backup | postgres | UTF8

erp | geekstuff | UTF8

geeker | sathiya | UTF8

Backup all postgres databases using pg_dumpall:

You can backup all the databases using pg_dumpall command.

$ pg_dumpall > all.sql

Verify the backup:

Verify whether all the databases are backed up,

$ grep "^[\]connect" all.sql

\connect article

\connect backup

\connect erp

\connect geeker

3. Backup a specific postgres table

$ pg_dump --table products -U geekstuff article -f onlytable.sql

To backup a specific table, use the –table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the –schema SCHEMANAME option.


How To Restore Postgres Database

1. Restore a postgres database

$ psql -U erp -d erp_devel -f mydb.sql

This restores the dumped database to the erp_devel database.

Restore error messages

While restoring, there may be following errors and warning, which can be ignored.

psql:mydb.sql:13: ERROR:  must be owner of schema public
psql:mydb.sql:34: ERROR:  must be member of role "geekstuff"
psql:mydb.sql:59: WARNING:  no privileges could be revoked
psql:mydb.sql:60: WARNING:  no privileges could be revoked
psql:mydb.sql:61: WARNING:  no privileges were granted
psql:mydb.sql:62: WARNING:  no privileges were granted

2. Backup a local postgres database and restore to remote server using single command:

$ pg_dump dbname | psql -h hostname dbname

The above dumps the local database, and extracts it at the given hostname.

3. Restore all the postgres databases

$ su postgres
$ psql -f alldb.sql

4. Restore a single postgres table

The following psql command installs the product table in the geek stuff database.

$ psql -f producttable.sql geekstuff