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.
0 comments:
Post a Comment