foo
foo

Reputation: 2311

How to write compile and run with one command in vim

So I have my config file .vimrc that has this code which should save the file compile and run it when I press F3. This doesn't work.

map <F3> ^M:w^M:!gcc *.c -g; ./a.out^M

Upvotes: 0

Views: 2249

Answers (4)

baz
baz

Reputation: 1587

also it's not necessary to compile *.c and to run a.out, % and %:r can be used instead

map <F3> :w<enter>:!gcc % -o %:r;./%:r<enter>

or

map <F3> :w<enter>:!make;./%:r<enter>

Upvotes: 0

Ren&#233; Nyffenegger
Ren&#233; Nyffenegger

Reputation: 40499

The first ^M is wrong. Change the others to <enter>:

map <F3> :w<enter>:!gcc *.c -g; ./a.out<enter>

Upvotes: 3

Mikel
Mikel

Reputation: 25606

How are you writing ^M? You can write it by pressing Ctrl+V Enter, or better still, change it to <CR> which doesn't require any special tricks.

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791769

Your keymapping worked for me (assuming you type ^M as e.g. Ctrl-V Ctrl-M).

If you have a makefile setup (and you probably should) you can just use :make.

You can set the makeprg option if you use something other than make, e.g. cmake scons ant or something else.

Upvotes: 3

Related Questions