Developer Productivity

Create a tmux Navigation Bash Script

ThePrimeagen

ThePrimeagen

terminal
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.

Preview
Close

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

[00:00:00]
>> 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.

[00:00:11]
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.

[00:00:31]
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.

[00:00:57]
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.

[00:01:14]
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.

[00:01:34]
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.

[00:01:48]
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?

[00:02:03]
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.

[00:02:20]
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.

[00:02:38]
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.

[00:02:54]
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.

[00:03:11]
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.

[00:03:28]
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.

[00:03:41]
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.

[00:03:59]
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.

[00:04:12]
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.

[00:04:28]
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.

[00:04:47]
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.

[00:05:02]
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?

[00:05:16]
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.

[00:05:33]
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.

[00:05:46]
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.

[00:06:00]
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?

[00:06:14]
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?

[00:06:29]
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