Josh Morrison
Josh Morrison

Reputation: 7636

How compile C in vi and run it?

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! :)

Upvotes: 6

Views: 11874

Answers (5)

jadelord
jadelord

Reputation: 1765

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

Upvotes: 6

Spyros
Spyros

Reputation: 48626

By typing ! you can execute any shell command by the way. You can type : to write a command and then write :

:!make

Upvotes: 5

Aydogan Karagoz
Aydogan Karagoz

Reputation: 21

In order to complile by just calling :make you need to have a Makefile in that directory.

Upvotes: 0

Lawrence Woodman
Lawrence Woodman

Reputation: 1444

Vi understands the command make directly, so you can just type:

:make

Upvotes: 4

Michelle Tilley
Michelle Tilley

Reputation: 159105

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.

Upvotes: 1

Related Questions