Vim 8's new
:terminal command allows for asynchronously running a terminal job in a Vim pane or window. Learn the basics of this awesome new feature to get immediate feedback without leaving your editor or breaking your flow.
Instructor: [00:00] Terminal support was released in Vim 8. We can check for support by saying echo has('terminal'). If a 1 is returned, it's supported. If a 0was returned, it's not.
[00:11] I have a project here with a package.json. I've opened a terminal in the current window by passing the ++curwin option to the terminal command. I can run commands in a live terminal. I can say npm list to show my dependencies.
[00:29] I want to interact with Vim outside of this terminal, so I'm going to prefix my commands with CTRL-W. I want to open up a new split with the code from my project, so I'm going to say CTRL-W :, give it the command v new. In here, I can run commands as normal. I can just say :edit index.js.
[00:55] In here, I have a few dependencies. I'm using Highland and Chalk. As we saw above, neither of these dependencies are installed. I can go back to the running terminal by doing CTRL-W l. I can give it the command npm install Highland.
[01:11] I'm going to close this terminal by giving the exit command directly to the shell. That's going to close that window as well. I'm going to open up another split and say vertical new. I'm going to say terminal node index.js. This is going to run in a new terminal that's going to be opened up because I haven't passed it ++curwin as one of the options.
[01:34] We can see here that we don't have Chalk installed. What I'm going to do now is I'm going to install Chalk, but I'm going to run the npm install command in a hidden job. I'm going to say terminal ++hidden npm install Chalk.
[01:52] I could also give it the ++open option. That would hide the output from this terminal job until the job is finished, at which point it would open it. That's always a little surprising to me when that happens. It tends to break my flow, so I'm just going to leave this hidden. I'm going to let it close on its own. I'm going to forget about it.
[02:11] Nothing seems to happen from here, but if I say terminal current window node index.js, now my code is running. According to the code on the right, I'm going to pass in a file name to std in. I'm going to say sample01.txt. That's a file that I know I have. We can see the contents of the file on the right. This is the first sample file. I probably have a sample02.txt.
[02:39] Great. I can keep going with that. I'm still just going to be guessing what I have, unless I've remembered it. I'm going to close this command. Rather than guess in a shell, I'm going to generate a list of these files and pass them directly by running an actual command in a terminal. I'm going to say terminal find in this directory files whose names begin with sample. Here we go.
[03:04] I'm going to select all these files, and I'm going to pass these as a range to the terminal command. I'm going to say range terminal node index.js. The selected lines are going to be passed directly to the job. You can see that the code on the right received each of these.