cover image for article

Vim for Competitive Programming

#vim #comnpetitive-programming

Introduction

Vim is not a bad text editor when it comes to using it for Competitive Programming. It's kind of one way or the other, you would love it or you could trash it as it can waste a tremendous amount of time to write code every time. But once you are in a decent setup including some key-bindings and plugins can improve your speed in using Vim. I personally have used it since the beginning and haven't regretted it even a tiny bit till date. It's a flawless and enjoyable experience. I've used C++ for my CP journey, C is quite similar as well, but C++ has more library support and is a bit easier to write comparatively.

Using Vim for CP is a bit challenging in the initial stage but just stay consistent and you'll be in flying colors on the other side. Here, I'll give some quick key-bindings and plugins to include to Vim to enhance the workflow for Competitive Programming.

Quick Boilerplate code

Firstly, let's create a key-binding for the boilerplate code. We just need to map the key with the code we need to write which will be integrated with Vim commands.

For C++

nnoremap cpf i#include<iostream><Esc>ousing namespace std;<Esc>o<CR>int main(){<Esc>o<Esc>oreturn 0;<Esc>o}<Esc>kki

For C:

nnoremap cp i#include<stdio.h><Esc>o<CR>int main(void){<Esc>o<Esc>oreturn 0;<Esc>o}<Esc>kki

For Java:

nnoremap <C-j> iclass <ESC>"%pxxxxxa {<ESC>opublic static void main(String args[]){<Esc>o<Esc>o}<Esc>o}<Esc>kki<Tab><Tab>

Now, you get the idea of creating such macros for your own programming language. I don't think Python can have boilerplate code, even it has it's quite small compared to C/C++/Java/etc. You can include those codes into the key map. Let me explain the map to you.

Let's look at the Java example, the nnoremap is the map command with attributes like non-recursive and the mode of mapping. In this case n stands for normal mode map i.e. the map will get triggered in the normal mode, nore stands for non-recursive behavior of the map. For further readings, you can read my Vim-keymapping Guide article. The map itself has the right and a left part, kind of a map of the key and command to execute. You can use any key to trigger it, I have used CTRL+J or <C-j> to activate the map. The command is quite long but let's break it down into smaller chunks.

So, you can add more as per your preference of pre-defined constants and functions and the rest stuff involved in your language of choice.

Running Code by a Key-binding

After the boilerplate code being taken care of, we can now move to the building and running of the code file in Vim. We can add some more custom key bindings to auto compile and run the code within Vim.

We can compile the code with the compilers or interpreters for the languages from the Vim using ! which executes the commands from the terminal and pass in % i.e. the current file name and output file as %:r i.e. current file name without the extension in case of C/C++/Java.

Compile -> c++ hello.cpp -o hello

Run -> %:r for Windows

and

./%:r for Linux/macOS

We have added those two commands i.e (compile and run) using && to make things more clear and compact. For C++

nnoremap cpp :!c++ % -o %:r && %:r<CR>

For C

nnoremap c, :!gcc % -o %:r && %:r<CR>

For Java

nnoremap ,j :!javac % && java %:r<CR>

For Python

nnoremap py :python %<CR>

Again, you can apply this to any programming language you use. Also kindly note that their could be some differences for compiling the code in Linux and Windows so just be careful which command you use in which Operating system.

vimjv.gif

The above gif demonstrates the usage of those commands and key bindings to make the workflow of running and building process much easier and smoother.

Opening Terminal to compile and run code

We can use the terminal to compile and run code if you wish to do it this way as well. Vim is quite a terminal-friendly editor, you can open any terminal from Vim.

You can enter :term to open the default terminal in a horizontal split. You can explicitly enter the terminal/shell name to open it like :term bash to open bash, :term powershell to open PowerShell, :term zsh to open zsh, and so on. This is quite a great utility that vim provides with it. You can switch between windows using <C-w> or CTRL+W twice or use HJKL keys along with <C-w> to move with ease. More on Vim and Integrated Terminals in this article .

You can now explore the terminal world yourself and execute the commands from therein.

Plugins

When it comes to Vim, there are a ton of plugins made for everything you can imagine and couldn't imagine. We can use Plugin managers to manage those plugins for us or manually manage those yourself. You can go with one of the three well-known plugin managers like Vim-Plug , Vundle or Pathogen .

Using those plugin managers, we can install plugins like some of the useful ones are:

NERDTree

NERDTree is a great plugin for beginners and also for people doing competitive programming as it saves a bit of time as well. You can install the NERDTree plugin from the GitHub docs. You can read about the detail of configuring the NERDTree plugin in this article . This plugin allows you to have a graphical representation of the files and folder structures in the current directory. You can extend its capabilities to auto-refresh and auto open/close using some custom configuration as provided in the above-mentioned article.

You basically open the NERDTree with the command :NERDTree after installing and configuring. You can make key-bindings and shortcuts to use as you like and prefer.

Autocompletion and LSP

Autocompletion is quite important when it comes to competitive programming as you have to save time on the stuff you know and let the application process it for you. We do have some plugins for auto-completion and also LSPs for this specific task. LSP is quite great and provides much more accurate predictions when it comes to autocompletion. You can check out the LSP unofficial plugin for vim for more information.

For normal plugins like AuotoComplPop are also decent and provide a great beginner experience.

Not many plugins would be required for competitive programming just some autocompletion and a nice interface with a file system will work for the basic set-up but that is not the end, you can extend it as per your needs and skills. Vim is quite epic in terms of the customization it can offer.

vimcpt.gif

Similarly, for C++, I have demonstrated the use of key mappings and also showed the usage of the terminal inside of Vim, this is quite a flawless experience to integrate and use the terminal as per choice and preference.

Conclusion

So from this article, we were able to set up Vim for competitive programming by adding some custom key mappings and plugins for different languages. This was not a complete guide, I could not include every language's configuration here, surely you can ask me anytime in the comments, GitHub, Twitter or just mail me I can try to configure a programming language on Vim. Thanks for reading.

Happy Viming and Happy Coding :)