开发者

how to strip file type extension in vim

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 ex开发者_如何学Ctension 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜