roydq
roydq

Reputation: 312

Ruby syntax checking in vim

I use vim with various plugins for editing ruby code. I have proper syntax highlighting set up but I was wondering if anyone knew of a way to get ruby syntax checking, similar to what you might see in an IDE such as visual studio, radrails, etc?

Something that would simply turn stuff red or break highlighting when I'm missing an 'end' or have an improperly constructed line of code would be sweet.

I googled and came across this plugin, http://github.com/scrooloose/syntastic/tree/master but I was wondering if anyone had any better suggestions.

Upvotes: 14

Views: 12318

Answers (6)

Andrew
Andrew

Reputation: 5729

I think the existing answers are outdated. A ruby compiler plugin comes with vim now, so all you should need to do now is run :compiler ruby, or add this command to your ~/.vim/ftplugin/ruby.vim file (creating it if necessary). Then, not only will running :make syntax check your code, it will put you in vim quickfix mode, enabling you to jump directly to errors.

I say "should" because the compiler plugin misses the point a bit and doesn't set makeprg sensibly. Here is what I actually put in ~/.vim/ftplugin/ruby.vim:

compiler ruby
setlocal makeprg=ruby\ -wc\ %

I suggested that this be the default.

Upvotes: 10

Adam Libuša
Adam Libuša

Reputation: 735

awesome_person is right, ":w !ruby -c" will do. To make it more convenient, add this line in your ~/.vimrc:

autocmd FileType ruby map <F9> :w<CR>:!ruby -c %<CR>

Then the syntax gets checked on pressing F9 key.

Upvotes: 13

awesome_person
awesome_person

Reputation: 1263

You can syntax check the current buffer in ruby without downloading any plugins. The command is:

:w !ruby -c

Upvotes: 23

thnetos
thnetos

Reputation: 1326

Check out using the CTK plugin: http://www.vim.org/scripts/script.php?script_id=2618

and try this in your .vimrc:

" Compile Ruby code after writing (show warnings/errors)
function! Compile()
  " don't compile if it's an Rspec file (extra warnings)
  let name = expand('<afile>')
  if name !~ 'spec'
    CC
  endif
endfunction
autocmd BufWritePost *.rb call Compile()

That (with ctk.vim) will perform a compile every time when the file is saved.

Upvotes: 0

austinfromboston
austinfromboston

Reputation: 3780

Seems like a few people have tried this, but no one has tried too hard. Personally I recommend using autotest in a separate terminal window.

Try this, if you just want syntex errors.

If you want errors that happen at runtime as well, you might like this instead.

Upvotes: 0

Kenny
Kenny

Reputation: 794

The rails.vim plugin does excellent syntax highlighting, as long as you stay within some of its formatting confines. Couple this with auto-indentation (ggVG=) and you should be able to get a general sense of if the document is well formed, or not.

Upvotes: 0

Related Questions