0

I found a useful line for my .vimrc file from here that makes any file beginning with #! on line 1 executable when I save it. Here's the line:

au BufWritePost * if getline(1) =~ "^#!" | silent !chmod +x <afile> | endif

This works great, as long as I don't have any syntax errors. But if I try to save a file with any syntax errors, if it has a shebang line on line 1 then vim gets glitchy when I save. This behavior completely goes away when I comment out this line. I don't understand the inner workings of vim well enough to diagnose this. Why does it cause glitches? Is there any way to make this more foolproof, or should I ditch this line altogether?

EDIT: To be more specific, the issues only occur when I use :w and continue editing, not when I use :wq or :x. And it appears that the line or two of error messages that appears at the bottom of the screen shifts the screen up a line or two when they pop up.

EDIT 2: I also just tried this other solution from the same web page:

function! SetExecutableBit()
  let fname = expand("%:p")
  checktime
  execute "au FileChangedShell " . fname . " :echo"
  silent !chmod a+x %
  checktime
  execute "au! FileChangedShell " . fname
endfunction
command! Xbit call SetExecutableBit()

which makes it so I can use :Xbit within vim to add the x bit. This does add the x bit successfully, but whether or not I have syntax errors, it completely clears my terminal screen (line numbers included) except for my cursor and the vim command line on the bottom.

  • 1
    Vim usually doesn't care about syntax errors; do you have any plugins like Syntastic? – Ingo Karkat Apr 5 '15 at 15:19
  • I'm working on a cluster that has a bunch of plugins installed apparently. I don't see a Syntastic plugin when I type :scriptnames in vim, but there are several syntax plugins. – Ben Lindsay Apr 7 '15 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.