Last Updated: February 25, 2016
·
116
· jonathanreeve

vim mapping for updating php whitepsace to WordPress standards

WordPress coding standards require spaces around parentheses, like this: if ( 'bar' == $foo ), but some people prefer to write it spaceless, like this: if('bar'==$foo). To update PHP whitespace to WP standards, I created this mapping:

map \ss :s/(\([^ )]\)/( \1/g<CR>:s/\([^ (]\))/\1 )/g

Explanation:

  • :s/ - substitute the following
  • ( - literal beginning parenthesis
  • \( - beginning of group to be referenced later
  • [^ )] - match anything except spaces and end parentheses, since we don't want to respace function calls like function().
  • \) - end of group to be referenced later
  • / - replace with the following
  • ( - replace with an opening parenthesis and a space
  • \1 - and add the first group referenced earlier
  • /g - do this for every occurence on this line
  • <CR> - press enter
  • ... and do the same thing for closing parentheses.

To use it, put the line in your .vimrc, and type \ss for respacing a line of php.