views:

299

answers:

2

What is the best way to set makeprg=xcodebuild in vim?

I'm using filetype line in files to indicate that the file is objective-c (as opposed to matlab or cpp) by setting up the first line of my file to:

/* vim: set filetype=objc : */

and having this in vimrc:

set modelines=1

I would probably want to use :mak to run this command in the current directory:

xcodebuild -activetarget -activeconfiguration

I usually end up setting manually xcodebuild as makeprg so that I can do :mak to compile.

I'm always in the project root directory where I have .xcodeproj files so I don't have to worry about searching for project files.

What's the best way to setup makeprg? Ftplugin? Compiler plugin?

Any ideas appreciated.

+1  A: 

I'd say ftplugin, it's very easy. Write this in .vim/ftplugin/objc.vim:

set makeprg=xcodebuild\ -activetarget\ -activeconfiguration

Also, Vim's filetype detector will consider your .m file to be Objective-C if it notices #include, #import, or /* in the first ten lines. You can write an ftdetect plugin to change the default: .vim/ftdetect/objc.vim:

autocmd BufNewFile,BufReadPost *.m set filetype=objc
jleedev
+1, I did not have `:filetype plugin on` so I guess it wasn't loading filetype plugins, I've added it and it's working now
stefanB
A: 

You could put something like the following into your .vimrc:

if len(glob( getcwd() . '/*.xcodeproj' )) > 0
    let &makeprg = 'xcodebuild'
endif

So when you start vim and there is a *.xcodeproj in the current directory, it sets the makeprg to xcodebuild.

ashcatch
This won't work if he edit files from different directories, with different filetypes, etc.I would instead use a solution like local_vimrc (/project.vim) to detect where is the file currently edited, and whether its filetype is objc to apply the correct :setlocal (and not :set!).
Luc Hermitte