Kenny Woo

finding new ways to procrastinate.

github twitter linkedin email rss
Mapping a Compile/Build Keystroke in Vim
Jan 7, 2014
One minute read

I find myself editing a single source code file and recompiling to test every change often. Recently I discovered this nifty trick in Vim to map that process to a single key combination.

The command within Vim (which is actually vimscript) is of the form:

:map \a :w<CR>:![CMD]<CR>

where:

  • : - starts the vim console
  • map - begins mapping a keystroke
  • \a - desired keystroke
  • :w - default keystroke to save your document
  • - carriage return
  • ! - executes the subsequent text as a command in shell
  • [CMD] - desired shell commands

Here’s an example to clean, rebuild, execute binary when working in C:

:map \a :w<CR>:!make clean;make;./a.out<CR>

Now all you have to do is hit \a after any change you want to test and you’ll save, compile, and run your program!

Note: you can add the above script into your .vimrc, just strip the first colon just before ‘map’.


Back to posts