Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this line in my .vimrc, which means "when I save a .rb file, run it through ruby -c" (the ruby interpreter's error checking).

autocmd BufWritePost *.rb !ruby -c <afile>

When I save that file, I always see output at the bottom of the screen, so I get used to it and start ignoring it. What I want is to only see output if there are errors.

I can see that when there are errors, after it says what they are, at the bottom, it says "shell returned 1."

How can I modify this line so that it only shows a message if the shell returns 1? Is there a way to conditionally surpress output from a shell command run in vim?

share|improve this question
up vote 2 down vote accepted

You should definitely look at Syntastic plugin. It automatically checks the syntax of the file in current buffer on save, supports many filetypes (ruby, python, php, etc out of the box) and does exactly what you need - it shows output only in case of errors.

share|improve this answer
Syntastic has become one of my biggest time-saving tools since you told me about it. Just wanted to note that generally the error-checking is done by calling out to an external program, which may need to be installed. You can find out what it needs by looking in the syntax_checker file. For example, for Javascript checking you need to have JSLint (jsl command line utility) installed:… – Nathan Long Dec 8 '11 at 19:10

By the time Vim sees the exit status of the command, the command has already sent its output, if any, to the terminal, so you can't conditionally suppress the command's output by testing the exit status.

The ruby(1) man page says that -c causes ruby to send the message "Syntax OK" to standard output if there are no errors. It appears from testing that syntax errors are reported to standard error. Therefore you could suppress the "Syntax OK" message by redirecting standard output to /dev/null and suppress the echoing of the command by using :silent, like this.

autocmd BufWritePost *.rb silent! !ruby -c <afile> > /dev/null
share|improve this answer
When I do this, I don't see error output, either. Do you? – Nathan Long Feb 8 '11 at 21:57
I do. I just copied that line and pasted it into a vim session and tried it again. However, vim doesn't seem to be expecting any output from a BufWritePost autocommand, so the output appears to be written by ruby directly to /dev/tty rather than being managed by vim's display routines. The bottom of my terminal window contains this, "bar.rb" 1L, 10C writtenbar.rb:1: syntax error, and the display is out of sync with the cursor. – garyjohn Feb 8 '11 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .