My Thoughts

But what I think is that Vim works the way we want it to the right from the very start, while other editors make us work the way they have been designed, not the way we actually want them to work. I can’t say much about other editors cause I haven’t used them much ( I’m biased with Vim ). Anyway, Let’s make something out of Vim, that really does the Job god damn well.

​Vim for Programming

Executing the Code

Consider a scenario, What we do when we are working on a C++ code on Vim and we need to compile and run it. (a). We get back to the terminal either through (Ctrl + Z) thing or we just save and quit it (:wq). (b). And the trouble’s ain’t over, we now need to type on something on terminal like this { g++ fileName.cxx }.​(c). And after that execute it by typing { ./a.out } . Certainly a lot of things needed to be done in order to get our C++ code running over the shell. But it doesn’t seem to be a Vim way of doing this (as Vim always tends to keep almost everything under one/two keypresses). So, What is the Vim way of doing this stuff?

The Vim Way

Vim isn’t just a Text Editor, It is sort of a Programming Language for Editing Text. And that programming language that helps us extending the features of Vim is “VimScript”.So, with the help of VimScript, we can easily automate our task of Compiling and Running code with just a KeyPress. Above is a snippet out of my .vimrc configuration file where i created a function called CPP().

Creating Functions in VimScript

The syntax for creating a function in VimScript is pretty easy. It starts with keyword “func” and is followed by the name of Function [Function Name must start with a capital letter in VimScript, otherwise Vim will give an error]. And the end of the function is denoted by keyword “endfunc”.In the function’s body, you can see an exec statement, whatever you write after the exec keyword is executed over the Vim’s Command Mode (remember the thing starting with: at the bottom of Vim’s window). Now, the string that I passed to the exec is – What happens is when this function is called, it first clears the Terminal Screen, so that only you will be viewing is your output, then it executes g++ over the filename you are working on and after that executes the a.out file formed due to compilation.Mapping Ctrl+r to run C++ code————————————————————-I mapped the statement :call CPP() to the key-combination (Ctrl+r) so that I could now press Ctrl+r to execute my C++ Code without manually typing :call CPP() and then pressing Enter.  

End Result

We finally managed to find the Vim Way of doing that stuff. So now, You just hit a button and the output of your C++ code is on your screen, you don’t need to type all that lengthy thing. It sort of saves your time too. We can achieve this sort of functionality for other languages too. ​So For Python: Now you could press <Ctrl + e> to interpret your code. For Java: You could now press <Ctrl + j>, it will first Compile your Java Code then interpret your java class file and show you the output.  

Picture ain’t over, Marching a level deep

So, this was all about how you could manipulate things to work your way in Vim. Now, it comes to how we implement all this in Vim. We can use these Code Snippets directly in Vim and the other way around is by using the AutoCommands in Vim (autocmd’s). The beauty of autocmd is these commands need not be called by users, they execute by themselves at any certain condition which is provided by the user. What I want to do with this [autocmd] thing is that, instead of using different mappings to perform execution of codes in different Programming Languages, I would like a single mapping for execution for every language. ​What we did here is that I wrote auto commands for all the File Types for which I had functions for Executing the code. What’s going to happen is as soon as I open any buffer of any of the above-mentioned file types, Vim will automatically map my (Ctrl + r) to the function call and represents Enter Key, so that I don’t need to press “Enter key” every time I press and alone does the job. To achieve this functionality, you just need to add the function snippets to your [dot]vimrc and after that just put all those autocmds. And with that, the next time you open Vim, Vim will have all the Functionalities to execute all the Codes with the very same KeyBindings.

Conclusion

That’s all for now. Hope this thing makes you love your Vim even more. I am currently exploring things in Vim, reading Documentations, etc. and doing additions in [.vimrc] file and I will reach to you again when I will have something wonderful to share with you all. If you want to have a look at my current [.vimrc] file, here is the link to my Github account: MyVimrc.