How I use Vim for Competitive Programming

Akash Rao
3 min readMay 5, 2020

I have been using Vim for almost a year now, and I’ve noticed its benefits when I decided to use it for my competitive programming course at school. For those of you who are not familiar with Vim, it is a lightweight text editor that can be quite powerful if you commit a few days/weeks to learning the basics of the editor and customizing it for your needs.

Vim Setup

I use macOS and my terminal of choice is Alacritty. At the time of writing this post I’m using Vim 8.2.6, but the tips and tricks I use should work with any version of Vim. Here’s a brief look at Vim setup:

Generating Template Files

In competitive programming it can often be handy to start with a template containing a lot of the boilerplate code that you need, plus a few macros that might save you some time. There are probably several command line applications that will let you create template files, but I have a simply bash script that let’s me create a simple C++ template with a single command:

As you can see I use this template to generate both C++ files as well as LaTeX files (which I use for homework). Feel free to alias this script in your .profile for convenient use:

alias gt="sh ~/.scripts/generate_template.sh"

With the above setup, you should be able to easily create templates with a simple call of

$ gt helloworld.cpp

Compiling, Running and Testing

If you’ve used Vim before, you’ve probably come across macros and ability to map certain keys to simply commands. This comes in very handy when doing repeated tasks like compiling, running and testing code, for example. I have the following maps in my .vimrc which help make this process feel a bit snappy and less of a pain:

The above mappings are quite simple. I have remapped <leader>rm to compile my source file into an object file with the same name (without the extension). You can of course use the built-in Vim command, :make, which gives you a few neat features like going to next line with compilation error and such. Feel free to explore these with :help make and :help quickfix. I’ve found that simply running the commands in the terminal directly is good enough for me. Next, I have remapped <leader>rr to execute the object file and perform simple tests. This is usually handy when the sample input for the program is quite short. Finally, for larger sample inputs, using input/output redirection saves you from having to copy paste several times, and can also be more accurate, preventing any human error. For this reason, I have remapped <leader>rt to run a simple bash script that redirects input from each test file and runs the executable. I have used the naming convention <filename>.<test number>.test, but you can change this to what makes sense to you. Feel free to change the remap commands. Note that by default the leader key in Vim is \ (backslash). However, I have changed it to , with let mapleader=",".

Demo

Of course, this post wouldn’t be complete without a short demo of the above commands, so here you go. I have written a small program to simply add up a set of numbers and output the result.

 by the author.

--

--