Add the following to your .vimrc
:
function s:PHPConvertArrays() abort
let l:winview = winsaveview()
while search('\m\(\w\)\@<!\carray\s*(') > 0
call execute("normal! dt(%r]\<C-O>r[")
endwhile
call winrestview(l:winview)
endfunction
command! -bar -nargs=0 PHPConvertArrays call <SID>PHPConvertArrays()
Now you can simply run :PHPConvertArrays
.
If you have an existing project where you want to convert all the array()
syntax to the newer syntax []
, you can use the following command:
find /path/to/project -name "*.php" -exec sh -c "echo {}; vim '+PHPConvertArrays' '+wq' {} > /dev/null 2>&1" \;
NOTE: (recommended) if you want a faster version for large projects, use phpcbf
just like so:
phpcbf --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax --ignore=vendor --extensions=php /path/to/project