Skip to content

Instantly share code, notes, and snippets.

@jaege
Created February 19, 2016 02:56
Show Gist options
  • Save jaege/3b2fa10b3e701ee57b8f to your computer and use it in GitHub Desktop.
Save jaege/3b2fa10b3e701ee57b8f to your computer and use it in GitHub Desktop.
VIM autocmd to compile and run single source file.
if has("autocmd")
augroup vimrcCompileMap
" Remove ALL autocommands for the current group. This prevents having the
" autocommands defined twice (e.g., after sourcing the .vimrc file again).
autocmd!
" Map <F5> to save, compile and run single source file.
if has("win32")
autocmd FileType cpp nnoremap <buffer> <F5> :w<CR>:!cls && cl /EHsc % && %< <CR>
autocmd FileType python nnoremap <buffer> <F5> :w<CR>:!py % <CR>
else
autocmd FileType c nnoremap <buffer> <F5> :w<CR>:!gcc -o %< % && ./%< <CR>
autocmd FileType cpp nnoremap <buffer> <F5> :w<CR>:!g++ -o %< % && ./%< <CR>
autocmd FileType python nnoremap <buffer> <F5> :w<CR>:!python % <CR>
endif
augroup END
endif
@ayhon
Copy link

ayhon commented Oct 5, 2021

Thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment