Is it possible after editing a ruby program in Vim to run it while still in the editor, not using the command line?
6 Answers
From Vim, you can run the current buffer with :
:!ruby %
It might be helpful or not depending on your use case.
Similarly, you invoke any shell command by using :!command
Yes it is possible. For example lets say i want to write in a document word hello 100000 Now i won't do that manually but i will use ruby script and run it in vim. In terminal create a new file by typing vim script.rb Type this code:
10000.times do
puts "hello"
end
Before exiting Press escape then type this:
:w ! ruby > testfile.txt
press enter What this will do is that it will run the script in the vim and add the returned string into the text file named testfile. Make sure you have the file in the same directory or you can specify the directory along with the name.
Comments
You can use this function.
function RunWith (command)
execute "w"
execute "!clear;" . a:command . " " . expand("%")
endfunction
And then, just create shortcut which is only works for ruby. You should to go to the ruby file and press this shortcut :)
autocmd FileType ruby nmap <Leader>e :call RunWith("ruby")<cr>
You can create shortcut for any file type. This is javascript example:
autocmd FileType js nmap <Leader>e :call RunWith("node")<cr>