How compile C in vi and run it?
Solution 1
By typing !
you can execute any shell command by the way.
You can type : to write a command and then write :
:!make
Solution 2
vim does have a inbuilt :make command, but it has to be associated with the compiler. Some examples are:
GNU gcc compiler:
set makeprg=gcc\ -o\ %<\ %
Intel Fortran compiler:
set makeprg=ifort\ %\ -o\ %<
- \ represents blank space
- % input file
- %< output file
Solution 3
Vi understands the command make
directly, so you can just type:
:make
Solution 4
I like to map shell commands to a leader+key. For example, (my leader key is ,
(comma), it's \
by default I believe):
:map <leader>m :!make && ./program<CR>
Then, pressing ,m
(comma then m) executes make && ./program
on the shell (the <CR>
is a carriage return) Once the command has terminated, you will get a prompt to press return, and your focus will go back go vim.
Another workflow you may like is suspending vim using <Control>+Z
, running a command on the shell, and then running fg
to switch back to the backgrounded program.
Josh Morrison
Updated on June 14, 2022Comments
-
Josh Morrison almost 2 years
Environment: MacOS, gcc, Vim7.2
I know I can do it in Emacs. Compile code and run it. I am wondering how can I do it in vi? I don't want to switch from vi to terminal a lot. thanks! :)
-
noahlz almost 12 yearsThis is superior than SpyrosP's answer, above.
-
Cyriac Antony over 4 yearsIsn't this rather a comment on Lawrence's answer above?
-
Cyriac Antony over 4 years(But an important comment, I agree)