tags:

views:

39

answers:

1

In my .vimrc, I set different makeprg according to the source file type as following :

au BufRead,BufNewFile /home/me/cpp/**/**.cc set makeprg=g++\ %"
au BufRead,BufNewFile /home/me/c/**/**.c set makeprg=gcc\ %\"

It's good practicse but not perfect. the problem with this setting is that you can just have on binary with the default name a.out under a certain directory.

To put it more clearly, let's say there are client.c and server.c under the same diretory. After compile the client.c in vim, you should rename a.out to some other name before you can edit server.c and compile it vim. otherwise the existing a.out will be overwrite.

So I'd like to improve the setting by add -o option to gcc or g++ to explicitly give the binary a name rather than let it named as "a.out". and also I'd like to use the source file name without fiel type extension as the binary name for a gvien source file. for example, for client.c the generated binary's name will be simply client.

Just need your help to tell me how exactly this could be done in vim. thanks in advance.

+2  A: 

See :help expand. If you do %:r you'll get the current filename, minus the last extension on it. Your altered commands are:

au BufRead,BufNewFile /home/me/cpp/**/**.cc set makeprg=g++\ %\ -o\ %:r
au BufRead,BufNewFile /home/me/c/**/**.c set makeprg=gcc\ %\ -o\ %:r
Conspicuous Compiler
There seem to be some extra `"` characters on the end of your commands?
too much php
@too much php: True. Copied the original command lines and just altered them. However, they worked when I tested them. Extra quote appears to do no harm, but removed it anywyay. Cheers!
Conspicuous Compiler
`"` is the comment character in Vim, like `//` in C
too much php
Ah. Had forgotten that. Thanks, @too much php.
Conspicuous Compiler