Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5955849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:10:21+00:00 2026-05-22T18:10:21+00:00

I use vim and would like it to prevent me from saving php files

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:10:22+00:00Added an answer on May 22, 2026 at 6:10 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use Vim to match a regular expression and perform a
I use vim on webfaction through putty and would like to change the colours
I use AquaEmacs. I would like to have Emacs which has a vertical hierarchy
I like to use VIM to quickly generate snip of codes from text. Now
I use vim as my editor but I would like to get some of
I use VIM editor for PHP, i know many people will point to PDT
:vimgrep looks like a really useful thing. Here's how to use it: :vim[grep][!] /{pattern}/[g][j]
I would like to use spaces instead of tabs when editing Perl scripts with
I would like to get an AND -operator to the following Vim command :command!
I would like to come up with a Vim substitution command to turn multi-line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.