DEV Community

Cadu Ribeiro
Cadu Ribeiro

Posted on • Originally published at thecode.pub on

Evaluate your ruby code directly from VIM

When I am writing code, usually I want to evaluate some piece of code. I used to do the following actions:

  • Copy and paste my code to IRB (or run my ruby script file directly from the terminal).
  • When using tmux, send my code directly from vim to tmux with the vim-tmux-runner plugin.

The first option needs an extra work of copying and pasting content. I prefer the second option, but sometimes I forgot to attach the VTR pane and get errors.

Now Iā€™m using https://github.com/JoshCheek/seeing_is_believing along with https://github.com/t9md/vim-ruby-xmpfilter plugin

I set my .vimrc with the following content (I use Plug to manage my dependencies):

Plug 't9md/vim-ruby-xmpfilter'

" Enable seeing-is-believing mappings only for Ruby
let g:xmpfilter\_cmd = "seeing\_is\_believing"

autocmd FileType ruby nmap \<buffer\> \<F4\> \<Plug\>(seeing\_is\_believing-mark)
autocmd FileType ruby xmap \<buffer\> \<F4\> \<Plug\>(seeing\_is\_believing-mark)
autocmd FileType ruby imap \<buffer\> \<F4\> \<Plug\>(seeing\_is\_believing-mark)

autocmd FileType ruby nmap \<buffer\> \<F6\> \<Plug\>(seeing\_is\_believing-clean)
autocmd FileType ruby xmap \<buffer\> \<F6\> \<Plug\>(seeing\_is\_believing-clean)
autocmd FileType ruby imap \<buffer\> \<F6\> \<Plug\>(seeing\_is\_believing-clean)

autocmd FileType ruby nmap \<buffer\> \<F5\> \<Plug\>(seeing\_is\_believing-run)
autocmd FileType ruby xmap \<buffer\> \<F5\> \<Plug\>(seeing\_is\_believing-run)
autocmd FileType ruby imap \<buffer\> \<F5\> \<Plug\>(seeing\_is\_believing-run)
Enter fullscreen mode Exit fullscreen mode

Now I can visual select my code, use F4 to mark and that line will be evaluated, press F5 and get the result of that code. After, I can clean all marks with F6 .

Cheers,

šŸ»


Top comments (0)