php, UNIX, vim

UNIX Tip of the day: Strange behavior editing PHP files with vim

I have been struggling with this weird behavior in vim for the last month or so. I just re-joined Automattic in September. I have been using vim on many different computers and environments (linux, max, cygwin) for over 15 years, and I have never experienced this before. I only seem to have this issue when using vim on my development server. I tried searching the interwebs several times over the last month without avail. Today I finally decided to ask some colleagues if any of them had experienced the issue. As I was writing up the issue, I discovered the root cause, and a solution!

It is difficult to describe. Basically, when editing php files, if I try to type a method call of an object, the formatting gets messed up. For example, if I try to type $this->foo, it ends up displaying on the screen as $thi->foo – after typing the > character, the s disappears. However, if I write the buffer and reopen the file, I can see that it is actually there. As you can imagine, this is very annoying.

In order to fully document the issue, I wanted to also share my .vimrc file to help others debug. It also occurred to me that the issue could be due to GNU screen. I have experience other issues like that in the past. So I decided to see if I could replicate the behavior running outside of screen. It turns out that the behavior was also broken, but in a slightly different way. Instead of deleting the s character as above, a visual bell was triggered! I tried searching the interwebs again about this weird visual bell behavior, and ran across a Google groups posting with the answer. The issue is that the > character was trying to match to an opening < character, and probably not finding one, since I was deep inside a <?php block. This is controlled by the showmatch feature in vim. I was able to exclude matching of angle brackets <> by adding the following in my .vimrc file

" Disable matching of <> in PHP files because it causes strange behavior
" when trying to type method names of objects
autocmd BufRead *.php set  mps-=<:>

I hope that this can help others who may have had the same issue.