Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Last active January 1, 2016 22:29
Show Gist options
  • Save boonebgorges/8210459 to your computer and use it in GitHub Desktop.
Save boonebgorges/8210459 to your computer and use it in GitHub Desktop.
.vimrc command for WordPress-style whitespace correction in current buffer
command! WPCodingStandards call WPCodingStandards()
function! WPCodingStandards()
" Space after opening paren
execute '%s/(\([^\t) ]\)/( \1/ge'
" Space before closing paren
execute '%s/\([^\t( ]\))/\1 )/ge'
" No spaces when casting types
execute '%s/(\s*\(array\|int\|object\|string\|float\)\s*)/(\1)/ge'
" Tabs not spaces
execute '%s/^ \([^\t ]\)/\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\t\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\t\1/ge'
execute '%s/^ \([^\t ]\)/\t\t\t\t\t\t\t\1/ge'
" control structures
execute '%s/\(while\|foreach\|if\|else\|for\|do\|try\)(/\1 (/ge'
execute '%s/}else/} else/ge'
execute '%s/else{/else {/ge'
" brackets
execute '%s/){/) {/ge'
" commas should have trailing spaces in argument lists
execute '%s/,\$/, $/ge'
endfunction
noremap <F8> :WPCodingStandards<cr>
@boonebgorges
Copy link
Author

Update: improved handling for () fixes that were canceling out other spaces

@wturrell
Copy link

Hi. This is really useful, but the spaces inside braces breaks regular expressions; e.g. something like:

return preg_replace( '/-([0-9]*)x([0-9]*)\.(jpg|jpeg|png|gif|webp)$/', ".$3", $url );

becomes:

return preg_replace( '/-( [0-9]* )x( [0-9]* )\.( jpg|jpeg|png|gif|webp )$/', ".$3", $url );

and fails to match. Any ideas for how to leave these alone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment