3

I currently use Ricardo Signes's plugin to allow me to use a single command to both check my Perl syntax and save my code only if it is syntactically correct. However, isn't there a simpler way to do this without a plugin and/or a lot of code?

I can save and then check the syntax for already saved files:

:w %<bar>! perl -c %

But that saves it no matter if the syntax is correct or not.

I read somewhere that this was supposed to be easy. Now, I've been Googling for it, but haven't been able to find it.

4
  • Note that perl -c is not safe to run on untrusted code.
    – derobert
    Commented Mar 25, 2015 at 18:34
  • @derobert What do you mean by untrusted? Commented Mar 25, 2015 at 20:58
  • perl -c executes parts of the passed script. For example, try feeding this through perl -c: BEGIN { print "oh noes\n" } See the perlrun manpage for details.
    – derobert
    Commented Mar 25, 2015 at 21:04
  • @derobert Yes. That is true. Commented Mar 25, 2015 at 22:09

2 Answers 2

5

The mentioned plugin either uses a custom :Write command, or employs autocmds to hook into the saving. Those are the two possible integrations into Vim.

In the end, any custom solution will probably re-implement large parts of the plugin. The most lightweight approach I can think of is executing the external check command first, and then only executing the :write in case that prior command succeeded:

:exe 'write ! perl -c' | if ! v:shell_error | w | endif
1
  • Oh, you're right, that would check the previously saved version, not so useful. I've changed the command to send the current contents to Perl. Commented Mar 24, 2015 at 19:08
0

I have found that the syntasic plugin works wonders for this. It uses an auto command that on every write will parse it through a syntax checker (like jshint or perl -c) and the result is placed in VIM's gutter so you see right off where the errors are. There are many more features. It has save numerous problems from heading off to production. It's even caught problems I didn't even know I had.

Syntastic is a syntax checking plugin for Vim that runs files through external syntax checkers and displays any resulting errors to the user. This can be done on demand, or automatically as files are saved. If syntax errors are detected, the user is notified and is happy because they didn't have to compile their code or execute their script to find them.

… syntastic has checking plugins for ActionScript, Ada, API Blueprint, AppleScript, AsciiDoc, ASM, BEMHTML, Bro, Bourne shell, C, C++, C#, Cabal, Chef, CoffeeScript, Coco, Coq, CSS, Cucumber, CUDA, D, Dart, DocBook, Dust, Elixir, Erlang, eRuby, Fortran, Gentoo metadata, GLSL, Go, Haml, Haskell, Haxe, Handlebars, HSS, HTML, Java, JavaScript, JSON, JSX, LESS, Lex, Limbo, LISP, LLVM intermediate language, Lua, Markdown, MATLAB, Mercury, NASM, Objective-C, Objective-C++, OCaml, Perl, Perl POD, PHP, gettext Portable Object, OS X and iOS property lists, Puppet, Python, R, Racket, Relax NG, reStructuredText, RPM spec, Ruby, SASS/SCSS, Scala, Slim, SML, Tcl, TeX, Texinfo, Twig, TypeScript, Vala, Verilog, VHDL, VimL, xHtml, XML, XSLT, YACC, YAML, z80, Zope page templates, and zsh. See the wiki for details about the corresponding supported checkers.

Screenshot of Syntastic

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.