5

Some languages have flow control or loop structures that require a few words, for example, shell has

if [...]
then
  ...
fi

Is it possible to use the syntax highlighting features of vim to indicate that there is an error, if, for example a fi is detected before a then? And if it is, how to do it for that example?

1
  • 1
    I don't know if it answers your question, but there's a vim plugin called syntastic that can run your file through an external syntax checker and displays the errors it has detected : github.com/scrooloose/syntastic
    – saginaw
    Commented Nov 10, 2015 at 22:05

1 Answer 1

5

With some languages, the syntax highlighting can show an error, if the syntax rules define them. For example, the C syntax definition in my system includes an error for invalid octal numbers:

syn match   cOctalError display contained "0\o*[89]\d*"

enter image description here

Or in shell scripts, an unmatched fi:

enter image description here

More commonly, this is achieved via syntax checking plugins which show messages. For example, the syntastic plugin can indicate lines with errors, and the error message given by the program used to check the syntax:

enter image description here

Admittedly, the error is a bit cryptic, and in some cases, can be quite long-winded. But with a bit of practice, this will help you spot errors quickly enough.

6
  • 2
    Shellcheck is a lot more useful for detecting problems than sh -n, and it's supported by syntastic.
    – lcd047
    Commented Nov 11, 2015 at 6:07
  • @lcd047 Thanks for the tip. I hope the AUR script for that doesn't make me build it. Haskell programs scare me these days.
    – muru
    Commented Nov 11, 2015 at 6:09
  • They should. Oh yes, they should. :)
    – lcd047
    Commented Nov 11, 2015 at 6:16
  • Thanks for that. Indeed, finding an unmatched closing (like fi) is quite simple. However I still haven't seen how to get that middle then with a syntax file. I'll try the recommended plugins. Commented Nov 12, 2015 at 6:38
  • @bilbo_pingouin oh, not that simple. Try a new shell script with just an unmatched fi in one line. Syntax highlighting in Vim is a messy business, it isn't powerful enough for a lot of things.
    – muru
    Commented Nov 12, 2015 at 8:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.