Skip to content

Commit a8f0b6f

Browse files
authored
Run code directly from vim (#594)
1 parent e1f237c commit a8f0b6f

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ Shortcuts using `<leader>` instead of special characters:
364364
map <leader>sa zg
365365
map <leader>s? z=
366366

367+
### Running Code
368+
To run code directly from vim, press `F5`. The currently open code will execute without you having to type anything.
369+
370+
Can be used to execute code written in C, C++, Java, Python, Go, Octave, Bash scripts and HTML. To edit how you want your code to be executed, make changes in the file
371+
```
372+
~/vim_runtime/vimrcs/extended.vim
373+
```
367374

368375
### Cope
369376
Query `:help cope` if you are unsure what cope is. It's super useful!

install_awesome_vimrc.sh

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ source ~/.vim_runtime/vimrcs/basic.vim
1212
source ~/.vim_runtime/vimrcs/filetypes.vim
1313
source ~/.vim_runtime/vimrcs/plugins_config.vim
1414
source ~/.vim_runtime/vimrcs/extended.vim
15-
1615
try
1716
source ~/.vim_runtime/my_configs.vim
1817
catch

vimrcs/extended.vim

+56
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,59 @@ endfunc
172172
func! CurrentFileDir(cmd)
173173
return a:cmd . " " . expand("%:p:h") . "/"
174174
endfunc
175+
176+
"=================================================================================
177+
"
178+
" Following file contains the commands on how to run the currently open code.
179+
" The default mapping is set to F5 like most code editors.
180+
" Change it as you feel comfortable with, keeping in mind that it does not
181+
" clash with any other keymapping.
182+
"
183+
" NOTE: Compilers for different systems may differ. For example, in the case
184+
" of C and C++, we have assumed it to be gcc and g++ respectively, but it may
185+
" not be the same. It is suggested to check first if the compilers are installed
186+
" before running the code, or maybe even switch to a different compiler.
187+
"
188+
" NOTE: Adding support for more programming languages
189+
"
190+
" Just add another elseif block before the 'endif' statement in the same
191+
" way it is done in each case. Take care to add tabbed spaces after each
192+
" elseif block (similar to python). For example:
193+
"
194+
" elseif &filetype == '<your_file_extension>'
195+
" exec '!<your_compiler> %'
196+
"
197+
" NOTE: The '%' sign indicates the name of the currently open file with extension.
198+
" The time command displays the time taken for execution. Remove the
199+
" time command if you dont want the system to display the time
200+
"
201+
"=================================================================================
202+
203+
map <F5> :call CompileRun()<CR>
204+
imap <F5> <Esc>:call CompileRun()<CR>
205+
vmap <F5> <Esc>:call CompileRun()<CR>
206+
207+
func! CompileRun()
208+
exec "w"
209+
if &filetype == 'c'
210+
exec "!gcc % -o %<"
211+
exec "!time ./%<"
212+
elseif &filetype == 'cpp'
213+
exec "!g++ % -o %<"
214+
exec "!time ./%<"
215+
elseif &filetype == 'java'
216+
exec "!javac %"
217+
exec "!time java %"
218+
elseif &filetype == 'sh'
219+
exec "!time bash %"
220+
elseif &filetype == 'python'
221+
exec "!time python3 %"
222+
elseif &filetype == 'html'
223+
exec "!google-chrome % &"
224+
elseif &filetype == 'go'
225+
exec "!go build %<"
226+
exec "!time go run %"
227+
elseif &filetype == 'matlab'
228+
exec "!time octave %"
229+
endif
230+
endfunc

0 commit comments

Comments
 (0)