Vim can be customized to provide auto-completion for PHP using its built-in omnifunc feature. While Vim doesn't natively offer completion for PHP out of the box, enabling omnifunc allows developers to efficiently complete functions, methods, and variables in their PHP code without the need for external plugins.

Once configured, omnifunc for PHP provides completion suggestions for common PHP functions and variables. This can help speed up development by reducing the need for constant references and manual typing of long function names. The integration of omnifunc preserves Vim's lightweight performance while adding useful code completion for PHP projects.

By linking omnifunc to PHP files, you can quickly enable context-aware completions, making the coding process smoother and more efficient. Follow these steps to configure omnifunc for PHP auto-completion in Vim.

Step-by-step video guide:

Steps to configure PHP auto-completion in Vim:

  1. Open any PHP file in Vim.

    Use an existing or newly created `.php` file to test the setup and verify that omnifunc works as expected before altering the configuration permanently.

  2. Manually set omnifunc to the PHP completion function.
    :set omnifunc=phpcomplete#CompletePHP
  3. Enter insert mode.

    Press the `i` key to switch to insert mode in Vim.

  4. Trigger omnifunc completion with <C-x><C-o>.
  5. Review and select the suggested completions.

    Navigate through the suggestions using the arrow keys and press `Enter` to select the desired completion.

  6. If the completion works, open your vimrc configuration file.
    vim ~/.vimrc
  7. Add a new autocmd to enable omnifunc for PHP files.
    autocmd FileType php setlocal omnifunc=phpcomplete#CompletePHP
  8. Save and close the vimrc file.

    Use `:wq` to save the changes in Vim.

  9. Reopen any PHP file to verify the configuration.

    After reopening, omnifunc will automatically trigger for files with the `.php` extension.

  10. (Optional) Create a key mapping to trigger omnifunc completion more easily.
    inoremap <silent><expr> <Tab> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>"

    This key mapping allows the use of `<Tab>` to trigger omnifunc and cycle through available suggestions efficiently.

Discuss the article:

Comment anonymously. Login not required.