Saturday, January 4, 2014

Compiling/Executing a program with One Key Press in Vim/Gvim


Eventhough there are more than one way to compile/execute programs in Vim/Gvim, the normal method to compile and execute C,C++,Java,Python programs are:

C:

 !gcc % -o %:r
 !./%:r

C++:

!g++ % -o %:r
!./%:r

Python:

!python %

Java:

!javac %
!java %:r

To do all these things you've to type the commands repeatedly everytime you want to compile and execute. To prove the laziness of a programmer, there's a way to do all these things by pressing a single key in Vim/Gvim.

First of all check if you have ~/.vimrc, if you don't have create one using this command :
nano ~/.vimrc

Now after creating this, put the following commands in your .vimrc :

autocmd filetype python nnoremap <F4> :w <bar> exec '!python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F5> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype java nnoremap <F7> :w <bar> exec '!javac '.shellescape('%').' && java '.shellescape('%:r')<CR>

Here's a screenshot of compiling a program in Vim/Gvim :



If you want to know the execution time of your program too, then use the following code instead of the previous one :


autocmd filetype python nnoremap <F4> :w <bar> exec '!time python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F5> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && time ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && time ./'.shellescape('%:r')<CR>
autocmd filetype java nnoremap <F7> :w <bar> exec '!javac '.shellescape('%').' && time java '.shellescape('%:r')<CR>

To compile and execute the programs, press:
F4 for Python
F5 for C
F6 for C++
F7 for Java



You can change the default key-mappings, just make sure that it didn't conflict with the original Vim/Gvim key-mappings.
That's it enjoy your programming in Vim.

Sunday, December 29, 2013

Improving the Syntax Highlighting in VIM/Gvim

GVim
GVim
The default syntax highlighting VIM Editor is not that much upto the expectation of the programmers. It's also one of my favourite text editor. So, i've modified the original source code of the syntax highlighting files of the VIm  and able to improved it.

I'm going to share the improved syntax highlighting  files. So, please download the files from my GIT repository. Languages for syntax highlighting  that i've modified are Python, C , C++, JAVA.
and i would like to encourage you to contribute in it which is hosted in GITHUB.

Please clone the following GIT repository :


or download the zip file from here.

Screenshots after using the changed original files:

Python:



C++ :

      


C :



JAVA:








Installing GIT on Linux

Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

Installing GIT:


GIT can be installed in many  ways. The two major ones are: to install from source and to install from an existing 

package for your platform i.e apt based (Ubuntu, Debian) or rpm based ( Fedora ).

Installing from Source:


For rpm based systems(Fedora,Mandrake,...etc):
# yum install curl-devel expat-devel gettext-devel   openssl-devel zlib-devel
For apt based systems(Ubuntu,Debian,...etc):
# sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev


When you have all the necessary dependencies, you can go ahead and grab the latest snapshot from the Git web site:
http://git-scm.com/download
or you can download the package using wget:
# wget https://git-core.googlecode.com/files/git-1.8.4.2.tar.gz
Then, compile and install:
# tar xf git-1.7.2.2.tar.gz
# cd git-1.7.2.2
# make prefix=/usr/local all
# sudo make prefix=/usr/local install

Installing on Linux

If you want to install Git on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora, you can use yum:
# yum install git-core
    
Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
# sudo apt-get install git
That's it.
The first thing you should do when you install Git is to set your user name and e-mail address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you pass around:
# git config --global user.name "Unix Roots"
# git config --global user.email unixroots@example.com

If you want to check your settings, you can use the git config --list command to list all the settings Git can find at that point:

# git config --list
    user.name=Unix Roots
    user.email=unixroots@example.com                                                                                                         ...
Now check that if it's working using the command git clone and using any git repository:
Here's a screenshot of cloning from GITHUB:


If you are going to use GIT from a proxy server then check out my post on Using GIT behind proxy server.