Developer Productivity

Create a tmux Navigation Bash Script



Developer Productivity

Check out a free preview of the full Developer Productivity course

The "Create a tmux Navigation Bash Script" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen live codes a navigation bash script, that creates new sessions based on directory names, using the core utility basename, has-session, new-session, and switch-client. The core utility basename is also used to print the last element of a file path.


Transcript from the "Create a tmux Navigation Bash Script" Lesson

>> Now we need to do something with this, right? We want to capture this value because we want to work on this value. So let's go like this. Let's call it session and we put little, some people like text, some people like dollar sign, I don't really know what I like.

But effectively anything inside of a dollar, parentheses is executed and the standard out is then passed into this variable. This is how you can think about it. So since SZF, whatever we select, what we passed out, we now have it saved as session. There's another fun thing right here, which is something called basename.

If you don't know what basename is, it's another core util. Here we'll go like this, session name, oops, space, session_name=, we'll do another one of these little dollar signs and go session. What this effectively does, I think you probably should have that around there. Oops, effectively what happens right here is it's gonna take whatever's passed in, like a file, and it's going to take, it's like the final thing instead of its file path, it's basename.

Awesome, so now, not only do I have the full absolute path to what I'm trying to work towards, I also have just the folder's name. This is fantastic, right? And so to prove it, let's go like this, echo session name, right? Let's go. Wait, where are you? Session_name, there we go.

Awesome. Go back here. Let's re-execute this one. Let's just select ansible. Notice adjusted .ansible, the folder that I did have right there. Awesome, this is exactly what we wanted, right? We are now finding the thing. So we have this part done, which I'd argue is probably the harder part of the things to do if you didn't know what you're doing is grabbing this.

We are fuzzy finding. We're awesome. Now all we need to do is actually just create the tmux utility to do this. So, obviously I forgot to go forward in our little script. Let's create the script, right. I can't go to the next part. Don't look at the next part.

All right, so let's go back to our script. Awesome, let's delete this line. Now instead what we need to do is we need to figure out some things about tmux, right? One thing we can do is we can do this, we can ask tmux, do you already have this session?

So a fun little thing is called has-session. So tmux has-session, and you can provide it a target, so that I can just call it session_name, right? There we go. This will return to me zero if it has it or an error if it doesn't have it, which means that I can do a if not has-session.

Then I should be able to jump in here and go, okay, it doesn't have the session, let's create the session. tmux new-session, right? And then I can give it a name. Let's give it the name, session_name, right? And then we can give it the directory, which remember, that's what we selected from find.

So session, yeah. Yeah, there we go. We get those two things, and of course, we need the detach it. So if we're in tmux, we're not exploding. You can't net sessions, all that kind of stuff. Awesome, we have it. So now by the time we're up to this point, we know for a fact, we have a tmux session with the given name.

And since we've already checked, we knew it wasn't there. We're guaranteed right here to have it. So we just need to do a little switch-client -t session_name, right? There we go. This alone will just make everything work, right? Now obviously I am making some assumptions in the script.

I'm trying not to get it super complicated. But I am assuming that we're already in tmux and running for this script to work. Now, you don't have to have it that way. I'm just trying to keep things a little bit more simple so we don't overly complicate it with every possible ending here.

So there we go. So we made a very simple script. So I think all these commands are things we've pretty much typed in except for has-session. So all of these should be very familiar at this point. We know how new-session works. We know that switch-client allows you to programmatically switch clients to a session_name.

We know you can only have one session_name, the session_names have to be unique, right? So we're using our folders for uniqueness of session_names. Now, there are some extra rules around this that we haven't quite done yet. I should actually probably do that right now anyways. Session_names do not contain periods.

It can only contain certain amount of characters. And there's a fun little programme called tr. I know I'm kinda going a little off the rails here. So check this out. If I go foo, I can actually replace with tr. Anything that isn't o, I can turn it into a 1, so f11 instead of foo.

tr just simply takes in the standard end, and if it sees any of these characters specified in the first part, it replaces them with their corresponding ones in the second part. To make this more obvious, let's put f right here and put 2 right here, 211, right? It makes a lot of sense.

tr is fantastic and I use it surprisingly more than I ever thought I would actually have used it. Awesome, so let's go back here. So anytime we see a period, let's replace it with an underscore. There we go. Awesome, so now we can make sure that we have valid session names at any point.

Perfect. So now let's close this. And now let's re-execute our sessionizer. So remember, it's gonna give us all these different names. And we should be able to go like this. Let's go to music. And now we've created a session called Music, with one window in the Music directory.

That's pretty awesome, right? We just did that so dang fast, it went exactly where we wanted to do it. And we still have our previous one that we are just in, whatever that one is. We have way too many sessions open at this point. We are right here in tmp, right?

That's where we're, right here we just got done executing this. And awesome, it worked. So let's jump back to where we were. And now you'll notice something, can't find session : Music. What just happened there? Well, remember how we did talk for a second about the redirect. I probably should have given a slightly better answer here.

Yeah, what's happening is that when it can't find it, it actually prints out can't find it to the standard error. So I can actually take 2 and just pipe it to a place called dev null, which just means pipe it to the garbage can, right? Pipe it to Michael Scott's filing cabinet.

It's exactly where it wants to be [SOUND] and then we can do it again, let's open up a new place that we haven't done. How about documents? So now let's go back from where we came. You'll notice no more error, everything's great. Our script is amazing. It's not littering.

It's fantastic. This is just exciting. Is this not just exciting? I hope that you guys feel how exciting that was, we were able to create these sessions with no problems. But there is obviously some disconnections here, right? How do you execute this program in a reliable and easy way?

Right now it's just in some sort of folder, we actually have to navigate there and execute it, that's not that great. When I execute it and I go somewhere say, ansible, how do you go back? What's the easy way to go back other than doing w, and trying to search through all these things, right?

But we're getting pretty close to making the script really good, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now