I’ve recently been playing around with some iOS code, and while I find Xcode an excellent editor with a lot of great built-ins, I’m a vim user at heart. It’s the editor I’ve been using on a daily basis for years, so its tough to switch when I want to do iPhone development. Of course there are some things you just have to use Xcode for, but I’ve been able to tailor vim to do most of my dev work, as well as build and run the simulator on the command line. I couldn’t find a single cohesive tutorial on this, so these are the pieces I put together to make this work for me.
The Vim Cocoa Plugin
To start, there is a decent plugin called cocoa.vim to get some of the basics going. It attempts to do a lot of things but I’m only using it for a few specific things – the syntax highlighting, the method list, and the documentation browse functionality.
One caveat is the plugin is not very well maintained, given the last release is nearly 2 years ago. Because of this you’ll want to grab the version from my github:
git clone https://email@example.com/jgoulah/cocoa.vim.git
Install it by following the simple instructions in the README.
You’ll get the syntax highlighting by default, and the method listing is pretty straightforward. While you have a file open you can type :ListMethods and navigate the methods in the file. Of course you may want to map this to a key combo, here’s what I have in my .vimrc for this, but of course you can map to whatever you like:
map <leader>l :ListMethods
There’s another command that you can use called :CocoaDoc that will search the documentation for the keyword and open it in your browser. For example, you can type :CocoaDoc NSString to open the documentation for NSString in your browser. However OS X gives a warning every time, which is pretty annoying. You can disable this on the command line:
defaults write com.apple.LaunchServices LSQuarantine -bool NO
After you’ve run the command, restart your Finder (or reboot). Lastly, it would be annoying to type the keyword every time, so you can configure a mapping in your .vimrc so that it will try to launch the documentation for the word the cursor is on:
map <leader>d :exec("CocoaDoc ".expand("<cword>"))<CR>
One of the biggest things I miss when I’m in Xcode is the functionality you get from ctags. Of course you can right click on a function and “jump to definition”. Works fine, but feels clunky to me. I love being able to hop through code quickly with ctags commands. I’ve talked about this before, so I’m not going to give a full tutorial about ctags, but I’ll quickly go over how I made it work for my code.
First go ahead and grab the version from github that includes Objective-C support:
git clone https://github.com/mcormier/ctags-ObjC-5.8.1
Compile and install in the usual way. Now you can utilize it to generate yourself a tags file. Mine looks like this:
|for i in MyApp ios_frameworks ; do|
|echo "tagging $i"|
|/usr/local/bin/ctags -f ~/.vimtags/$i -R \|
Just a little explanation here, I’m going into my working directory and generating a tags file for both MyApp and ios_frameworks. ios_frameworks is just a symlink that points to /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks
If you’re interested in how I load up these tag files you can check out my .vimrc on github.
Another handy feature from I wanted to emulate was flipping between the header and source file. Luckily there is yet another vim plugin that will do this for you called a.vim.
Grab it off github and install it by dropping it in your ~/.vim/plugin directory:
git clone https://github.com/vim-scripts/a.vim.git cp a.vim/plugin/a.vim ~/.vim/plugin
There’s a tiny bit of configuration that you can put in .vim/ftplugin/objc.vim:
let g:alternateExtensions_m = "h" let g:alternateExtensions_h = "m" map <leader>s :A
This just tells it which files to use for source and header files and creates another shortcut for us to swap between the two.
Build and Run
So now that we’ve got most of the basic things that help us edit code, its time to build the code. You can use xcodebuild for this on the command line. If you type xcodebuild –usage you can see the variety of options. Here’s what worked for me to build my target app, noting that I setup everything in Xcode first and made sure it built there. After that you can just specify the target, sdk, and configuration. This will create a build under the Debug configuration for the iPhone simulator:
xcodebuild -target "MyApp Enterprise" -configuration Debug -project MyApp.xcodeproj -sdk iphonesimulator5.0
And now to run the app. Back to github there’s a nice little app called iphonesim. Download that and just build it with xcode:
git clone https://github.com/jhaynie/iphonesim.git open iphonesim.xcodeproj
Put the resulting binary somewhere in your PATH like /usr/bin.
Now we can launch the app we just built in the simulator:
iphonesim launch /Users/jgoulah/wdir/MyApp/build/Debug-iphonesimulator/MyAppEnterprise.app
Last thing you’ll want are logs. Another thing that is arguably more easily accessible in Xcode, but we actually can get the console log. If you’re using something like NSLog to debug this will grab all of that output. You’ll have to add a couple lines to your main.m source file:
NSString *logPath = @"/some/path/to/output.log"; freopen([logPath fileSystemRepresentation], "a", stderr);
And then you can run tail -f /some/path/to/output.log
This is how I use vim to compile iPhone apps. Your mileage may vary and there are many ways to do this, it just happens to be the path that worked well for me without having to pull my hair out too much. Xcode is a great editor and there are some things that you will have to use it for such as the debugging capabilities and running the app on the phone itself. Of course even these are possible with some work, so if anyone has tips feel free to leave a comment.