Skip to content

Instantly share code, notes, and snippets.

@jaege
Created February 19, 2016 02:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 Dec 3, 2019

What does the "%<" mean in the gcc commands in line 12 and 13? I suppose it gets the current filename of the file being editted, but is that a feature of vim or of gcc?

@DominusDrow
Copy link

What does the "%<" mean in the gcc commands in line 12 and 13? I suppose it gets the current filename of the file being editted, but is that a feature of vim or of gcc?

It is to get the current file name without its extension, for example:

algorithm.c =>:% <=> algorithm

It is a characteristic of Vim.
more information: http://vimdoc.sourceforge.net/htmldoc/cmdline.html#:_%%3C

@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