Working with Background Jobs
This guide will teach you how to leverage Unix based background jobs to suspend Vim session and then how to return to them.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So, in the last few guides we've talked about how we can use and access multiple files whether it's using the split pane feature to see multiple files at the same time or using the command+e feature which lets you switch between files. However, there are also times where you actually want to close out of a file, but you don't want to actually close out of the state that it's in.

So, for example say that I have my application controller file right here and I've added some kind of custom method but I don't actually have my implementation yet so I just have some, just have a little bit, of work that has to be done, so, I don't wanna save and close this 'cause I don't wanna actually have this effect the system. What I can do is run control+z, and what control+z's going to do is it's actually going to suspend that job so instead of me having to save and quit and then potentially change the behavior of my files and change the behavior of the application, by doing control+z I can just suspend the job.

So, what that means is that the, and this is not technically vim, this is something that is available throughout all of UNIX, but it's tied very closely with vim usage so I wanted to kinda walk you through how this worked. So, the way that this is now running is in the background, so, if I type in jobs right here at the UNIX termminal, this is going to show me the jobs that I have suspended.

large

So now I could do something else, so I could open up our gem file and I could do the same thing. I could hit control+z and now if I type in jobs you can see that I have two jobs right here.

large

Now, in order to access these there's a few ways you can do it. First, I could just type in FG, which is going to say bring the job to the foreground. This is gonna bring the very latest job back, so, that's one way of doing it. But if you want to pick out a specific job then you can pass FG in argument. So, I can pass FG, space, %, and then the job number.

fg %

So, the way this works is you can see right here that we have job numbers and if you had ten jobs running simultaneously you'd have ten numbers. And so you can pass in FG %1, make sure there's a space in between those two, and there you can see that it opens up our application controller file. And now I could do something like actually put some type of implementation code in there. And once it's ready then I can save the file and close it, and now as you can see, if I type in jobs, you can see that I only have one job suspended.

large

So, I can just type in FG and if I wanna close it out then I can just quit, and so that is how you can use multiple files by leveraging UNIX's background job system with vim.