How to setup your VIM Editor to write C,C++,Bash, Python or any other language Program?

Today we will look at an interesting feature of VIM editor using which we can setup the VIM editor properties to ease our programming life.

The complete VIM setup would automate three important tasks:

1. Adds file execute(chmod +x) permission.
2. Write shebang sequence line in case of Bash script.
3. Set intention spaces useful for reading and best for python.
4. Insert main function and return 0 in C program.

To achieve this, we will use the file .vimrc in user’s home directory.

Lets have a look at the steps we need to follow to automate this.

1. Add general settings in ~/.vimrc file.

syntax on
filetype plugin indent on
" show existing tab with 6 spaces width
set tabstop=6
" when indenting with '>', use 6 spaces width
set shiftwidth=6
" On pressing tab, insert 6 spaces
set expandtab
set modeline
set background=dark

2. Now add lines to insert shebang sequence in BASH script.

autocmd BufWritePost *.sh silent execute "! chmod u+x %"
autocmd BufNewFile *.sh 0put =\"#!/bin/bash\\"|$

Output:

[root@nglinux deleteme]# vim newfile.sh

#!/bin/bash

~ 

3. Add python executable path for Python script with .py extension.

autocmd BufWritePost *.py silent execute "! chmod u+x %"
autocmd BufNewFile *.py 0put =\"#!/usr/bin/python3\\"|$

Output:

[root@nglinux deleteme]# vim newfile.py

#!/usr/bin/python3

 

Automating C Program Headers

Lets have a look how to add C libraries and main program.

autocmd BufWritePost *.c silent execute "! chmod u+x %"
autocmd BufNewFile *.c 0put =\"#include \\"|$
autocmd BufNewFile *.c 1put =\"#include \\"|$
autocmd BufNewFile *.c 2put =\"#include \\"|$
autocmd BufNewFile *.c 3put =\"#include \\"|$
autocmd BufNewFile *.c 4put =\"\\"|$
autocmd BufNewFile *.c 5put =\"int main()\\"|$
autocmd BufNewFile *.c 6put =\"{\\"|$
autocmd BufNewFile *.c 7put =\"\\"|$
autocmd BufNewFile *.c 8put =\"return 0\\"|$
autocmd BufNewFile *.c 9put =\"}\\"|$

Output:

[root@nglinux deleteme]# vim newfile.c

#include 
#include 
#include 
#include 

int main()
{

return 0
}

Complete .vimrc Script

The complete .vimrc file can be seen as follows:

[root@nglinux ~]# cat .vimrc
syntax on
filetype plugin indent on
" show existing tab with 6 spaces width
set tabstop=6
" when indenting with '>', use 6 spaces width
set shiftwidth=6
" On pressing tab, insert 6 spaces
set expandtab
set modeline
set background=dark
autocmd BufWritePost *.py silent execute "! chmod u+x %"
autocmd BufNewFile *.py 0put =\"#!/usr/bin/python3\\"|$

autocmd BufWritePost *.sh silent execute "! chmod u+x %"
autocmd BufNewFile *.sh 0put =\"#!/bin/bash\\"|$

autocmd BufWritePost *.c silent execute "! chmod u+x %"
autocmd BufNewFile *.c 0put =\"#include \\"|$
autocmd BufNewFile *.c 1put =\"#include \\"|$
autocmd BufNewFile *.c 2put =\"#include \\"|$
autocmd BufNewFile *.c 3put =\"#include \\"|$
autocmd BufNewFile *.c 4put =\"\\"|$
autocmd BufNewFile *.c 5put =\"int main()\\"|$
autocmd BufNewFile *.c 6put =\"{\\"|$
autocmd BufNewFile *.c 7put =\"\\"|$a
autocmd BufNewFile *.c 8put =\"return 0\\"|$
autocmd BufNewFile *.c 9put =\"}\\"|$
[root@nglinux ~]# 

Lets see the script file in Image below for better understanding.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments