Skip to content

Instantly share code, notes, and snippets.

@kkoomen
Last active August 17, 2019 16:05
Show Gist options
  • Save kkoomen/6468772c7fbbf727e5fa8b0a5b9b56f0 to your computer and use it in GitHub Desktop.
Save kkoomen/6468772c7fbbf727e5fa8b0a5b9b56f0 to your computer and use it in GitHub Desktop.
Vim command to convert PHP syntax array() to []

Vim

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.

Command-line

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

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