The Rules
  • Feel free to leave constructive criticism, or point out a better way to do something.
  • Personal attacks or flames, on me or anyone else, will be deleted.
  • Past history has shown that 99% of comments I can't read (i.e. those in other languages) to be spam. Therefore, any comment I can't read will be removed.
  • I'm pretty mellow concerning profanity, but excessive (as determined subjectively by me), bad language will be removed.

Thursday, August 30, 2007

Objective-C Syntax in gVim

For those who know me, it's no secret that I'm a Vim man. You know how some people are fish out of water without Visual Studio or (insert IDE here)? I'm a fish out of water without my .vimrc. Thanks to Vim's superior editing capabilities, I can get work done on any computer I need to as long as it at least has a basic Vim installation on it, but when I can use my own macros and settings, let the good times roll.

The other day I decided that since I'm also a Mac man, I ought to start wrapping my head around Objective-C. So I got a book and started in. I quickly discovered that for some reason, Vim wasn't detecting the .m filetype extension for my Objective-C source files. So I did some digging around through the Vim documentation and found the following:

You need in your home directory (or whatever your $VIMRUNTIME directory is) a directory named ".vim". Inside this directory you'll need to make a file called "filetype.vim". Paste the following into that file:







"Check to see if the filetype was automagically identified by Vim

if exists("did_load_filetypes")

finish

else

augroup filetypedetect

au! BufRead,BufNewFile *.m setfiletype objc

augroup END
endif



What happens, or at least what happened to me, is that for some reason Vim didn't automagically detect the ".m", or it was conflicting with another filetype for some reason. What the above snippet of vimscript does is ask Vim if it loaded a filetype. In my case, it hadn't. So it skips the "finish" command and moves to the "augroup" section, checking the file extension you gave the file (in this case ".m" and says, "oh hey, that's an Objective-C file", and then sets the filetype as such.

Now I know that some of you may start yelling at me telling me that this is already there on Vim's documentation, blah blah blah. I posted this as a convenience for those who don't want to spend hours digging for it.

6 comments:

Unknown said...

WOW.. Thanks. I'm glad i didn't have to digg around for this.. Google helped bring it.. I wouldn't have thought to look in the help for something like this! Post more tips as you come accross them.

Anonymous said...

vim identifies '.m' as Matlab files (whhich happen to have the same extension '.m' as ObjectiveC files)

Alex said...

@lysium: correct. But as I don't use Matlab, and I do use Objective-C, I'm ok with clobbering the Matlab syntax.

Anonymous said...

I found that the reason this was failing is that Vim was only looking at the first 10 lines of a file to determine if it was an Objective C file. Specifically, it was looking for #include or #import statements in order to consider it an Objective C file. The problem is, many of the files generated by XCode have comment headers at the front of the file which are much longer than 10 lines, so Vim will never find an #import or #include in those files.

Solution:

edit your filetype.vim file in the shared vim directory (mine was at /usr/share/vim/vim72/filetype.vim). In that file there is a function named FTm. Search for "FTm" (case sensitive) until you find it. The 2nd line of the function looks like this:

while n < 10

Simply change that to:

while n < 100

Save the file, and you're done.

This will make Vim search the first 100 lines of the file instead of the first 10. Fixed my filetype detection on the first try.

Hopefully this will help someone else with the same problem (particularly if they ever really have a need to edit both Objective C and Matlab files).

holland said...

Yes, thank you very much. Google is great, but without people like you it really wouldn't be anywhere as useful as it is today.

Anonymous said...

Thanks a lot! Just what I needed!

Cheers,
Andy