8

I use Vim every day to write shell scripts. I have been reading about the quickfix window, and I think it could speed up my productivity in the edit-run-fix cycle.

If I understood properly, I have to write my own errorformat function in order to Vim to be able to catch the errors and introduce them into the quickfix window. But this seems to be really complicated.

Is there an easier/more convenient way to take advantage of the quickfix window in Vim when writing Bash scripts?

2
  • 1
    Does your script produce line number annotated error messages at runtime that you want to examine, or do you want to use shellcheck to show compile time warnings in the quickfix window? Commented May 18, 2014 at 19:53
  • Yeah, I think I need to use shellcheck. I will try to implement the solution by Michael below. Thanks. Commented May 20, 2014 at 8:01

1 Answer 1

11

Vim's quickfix window is designed to speed up the edit-compile-edit cycle. Since Bash scripts do not get compiled, we have to substitute something else for that step that can point out errors in the current script.

What you want is a static analysis tool for Bash scripts. There are two good ones: shellcheck and checkbashisms. You'll want to install at least shellcheck, as it's the more comprehensive of the pair, but installing checkbashisms will help catch a few more issues.

To integrate those two tools into Vim, you need a plugin called Syntastic. Check the project page for installation instructions.

Once you've got everything installed, you'll be able to get immediate feedback on basic issues in your Bash script:

Vim window with Syntastic + shellcheck

  • Use :SyntasticCheck to force the checker to run
  • If you want the "quickfix" window to appear, run :Errors
Sign up to request clarification or add additional context in comments.

4 Comments

If the shebang is #!/bin/sh, ShellCheck will actually warn about most of the same things checkbashisms does.
@thatotherguy Good to know. I'm pretty sure I've had checkbashisms warn me of things that shellcheck didn't, but I agree, shellcheck is by far the more useful of the pair.
While useful for other reasons, the above is hardly an answer for debugging. The answers above provide static code analysis but do not offer debugging. Debugging would provide some method of instrumentation to step through the code interactively while it is running, providing opportunity to inspect variables and view the execution process.
@Banoona Debugging can take place at compile time or static analysis time. To your point, debugging most commonly refers to runtime debugging. I'm not aware of any runtime debugging tool for any language that integrates with the Vim quickfix window, since it's not really designed for that. I think "how do I perform runtime debugging of bash scripts" would be a great SO question, but a different question than what was asked here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.