Frage

I use vim and would like it to prevent me from saving php files that has parse errors. If I would like to use for instance "php -l <file>" to achieve this, how would the autocmd in .vimrc look like?

I know I can hook into BufWritePre with something like

autocmd FileType php autocmd BufWritePre * !php -l %

but I want it to abort the "write" if the php command fails.

War es hilfreich?

Lösung

You can throw an uncaught exception in the hook and it will abort write: try

function s:RunPHP()
    execute '!php -l' shellescape(@%, 1)
    if v:shell_error | throw 'PHP failed' | endif
endfunction

function s:DefineBWPphpevent()
    augroup Run_php
        autocmd! BufWritePre <buffer> :call s:RunPHP()
    augroup END
endfunction

augroup Run_php
    autocmd!
    autocmd FileType  *  :autocmd! Run_php BufWritePre <buffer>
    autocmd FileType php :call s:DefineBWPphpevent()
augroup END

Also note

  1. shellescape: you should not use :!shell_command % as here vim does not do proper escaping and you will run into troubles if file name contains special symbols like spaces, quotes, dollar, newlines, ...
  2. <buffer> in place of * in second pattern. Your original command does not define autocommand for php files, it instead defines autocommand for all filetypes if one of the filetypes is php.
  3. autocmd grouping and banged :autocmd!s: they prevent duplicating autocommands.

By the way, what do you want to achieve by running php on old version of file (it is BufWritePre event, so it happens before file is written)? Maybe you should replace code that runs php with :w !php -l, :w !php -l - or even :w !php -l /dev/stdin depending on what php accepts (I do not have it on my system and do not want to have).

Andere Tipps

but I want it to abort the "write" if the php command fails.

As far as I know, this isn't possible with VIM. Documentation doesn't state this capability either, so I'm pretty sure that can't be done, without a hack. The question is though what you're trying to accomplish? I have a script that makes sure I have no parse errors in PHP before I commit to version control, instead of when I save the file. That's a simple pre-commit hook, perhaps that something that'll work?

If you're really interested in linting before you save the file, you might be able to rewrite "ZZ" and ":w" to a command you wrote yourself, that in turns lints the file (in case of a PHP file) and saves upon no errors. I'm not sure if you can remap the :w, and it might cause more issues than it solves though.

Perhaps a simpler solution would be to create a command on a keystroke you choose yourself (e.g. F5), and make that do what you want?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top