Running a Terminal Session in Vim
In this lesson you'll learn how to run terminal commands directly from within Vim, this includes the ability to run Ruby code, RSpec tests, and any other Terminal command.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

This is gonna be a very fun episode. I absolutely love some of these shortcuts on the Ruby side, and so, I think you're gonna enjoy them as well because they really can help improve your workflow when it comes to becoming a Ruby developer. So, let's walk through a process with, let me give a process like if we were working with Sublime, since I already have a Sublime instance up right here.

So we have a file called some and it has a method called sample and it prints this method out. Let's see this is on the desktop, so now if I want to run this code, I'd have to open up a terminal session and navigate to the desktop and then I can run Ruby, followed by some, and you can see it prints out asdf.

large

And then if I come and make some other change to it, so add some more text to it, I have to switch back here, run it again and now this is showing the updated output.

large

Now this isn't a horrible situation, but if you're finding that you switch back and forth between the terminal and your text editor, then vim has a much cleaner solution for doing this. So, I'm going to open up our Ruby file right here and I have this my method, and it just prints out hey there. Now instead, I definitely could do something like this where I quit out of it and run Ruby, and the Ruby file and it prints out hey there, however, there is a better way of doing this.

So, what I can do is have my file open and down in the command area I can type in the colon followed by the bang and then simply type in any kind of terminal command that I want. So, I can type in Ruby Ruby.rb, run it and look at that, it printed out hey there right here just like when we were in this kind of situation, and then it says press enter or type a command to continue.

large

So, if I type in return, it takes me right back to the file.

large

I didn't have to quit, I didn't have to switch between applications, anything like that. Now if I say I want to make a change and say hey there again, I can hit save, and now also if I hit the command area or if I press : down in the command area, then if I press my up arrow you can see that it actually gives me the ability to run the identical command so I don't even have to retype Ruby and then the Ruby file name because it keeps a history of all my terminal commands.

large

So if I run this again, you can see that it says hey there again and it brings me right back here. When I found out how to do this, this was such a huge time saver for me because if you can imagine if you're a rails developer and you follow processes such as TDD then what you can do is you can have all of your rails code right here and all your files open, but instead of having to switch back between your code file in the terminal.

You could run something like a command and then you could run r-spec and you can even run r-spec with a specific feature or you know some kind of special path, hit return and then it will run all your tests right here and then you won't have to switch between applications or quit out of all of these files and then get back in. It is a phenomenal way of improving your workflow. So this is something that as a ruby developer was a huge time saver and it also made things fun too.

The first time I did that I thought it was one of the coolest things how I could run terminal commands from inside of Vim. I know it changes Windows in a sense where you know it kind of hides the code and switches to the terminal, but I didn't have to switch applications, I didn't have to click anything, I didn't have to do a single thing. I was able to run commands the same way I'd run a command with adding the setting to Vim or anything like that.

So that is how you can run terminal commands directly within Vim.