The code on this page may only works on GNU/Linux system only.
function! CompileAndRun()
let ext = expand("%:e")
if ext ==# "c"
execute "!gcc %:p -o %:p:r && %:p:r"
elseif ext ==# "cpp"
execute "!g++ %:p -o %:p:r && %:p:r"
elseif ext ==# "asm"
execute "!nasm -f elf64 %:p -o %:p:r.o && ld %:p:r.o -o %:p:r && %:p:r"
elseif ext ==# "s"
execute "!as %:p -o %:p:r.o && ld %:p:r.o -o %:p:r && %:p:r"
elseif ext ==# "rs"
execute "!rustc %:p -o %:p:r && %:p:r"
elseif ext ==# "py"
execute "!python3 %:p"
elseif ext ==# "hs"
execute "!runghc %:p"
elseif ext ==# "ts"
execute "!tsc %:p --outFile %:p:r.js && node %:p:r.js"
elseif ext ==# "js"
execute "!node %:p"
endif
endfunction
nnoremap <F5> :call CompileAndRun() <CR>
Add the code above to ~/.vimrc will activate the function which let you compile and run C/C++, Assembly (x86-64) with both NASM and GAS syntax, Rust and Typescript code; run Python, Haskell, Node.js code directly. Remember to type `:source` in vim (normal mode) to load the configuration.
The vimscript code distinguishes your programming language by the filename extension.
Latest update: Sep 7th 2025