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.