Developer Productivity

Improve the tmux Navigation Bash Script

ThePrimeagen

ThePrimeagen

terminal
Developer Productivity

Check out a free preview of the full Developer Productivity course

The "Improve the 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 demonstrates how to navigate to a previous session and discusses ways to make the previously built navigation script more useful. Having the script available in commonly used workspaces such as zsh and keeping executables in a local bin file are also demonstrated in this segment..

Preview
Close

Transcript from the "Improve the tmux Navigation Bash Script" Lesson

[00:00:00]
>> So we got to just do a couple more parts, really. So A, navigation tip. If you wanna go to your previous session <prefix> -L, if you're familiar with Vim, this is identical to alternate file, right? So if I go here, let's exit out of here exit, exit, exit, exit, exit, exit, there we go, <prefix> -L, I just went back to tmp.

[00:00:20]
<prefix> -L, I go back to that Ansible one. And I can just go back and forth, back and forth, right? So it's identical to alternate file, it's very, very useful. So if I need to jump somewhere to do something quickly, I can do that and then ultimate session right back to where I was and continue on my kind of my merry way right here.

[00:00:40]
But we do have a problem though, we haven't really made this thing easy to use yet. Meaning that we have this cool script that's just in a folder somewhere and we want it to be useful. So one thing that I did right away, there's a lot of ways you can kinda make this useful.

[00:00:56]
I put them in kind of, I would guess, funny locations, but I like the two locations I put mine. A, I have mine in my init Vim, I know this sounds weird, I have it in VM-ware, let's see, what is it? Little f, no it's a <C- f>, there, that one.

[00:01:11]
This one, I always know that if I'm using Vim, I'm already in a tmux session, I have already kind of made this reality to myself. Now remember, I also showed you that if you create a new window, you can pass a command into it. So I have my sessionizer available in the global path, meaning that it's available in one of my paths and so therefore, it's globally there, I can use it at any point.

[00:01:32]
So therefore, all I have to do is tmux new window with this thing. When that thing runs, it will then create the fuzzy finder do all that and then remember, new window creates new window, so I'll actually leave Vim. And it's right here, notice it's right here, that's where we're at, it actually left Vim and it's over here.

[00:01:50]
The moment I close this, it goes back, it closes that window and goes back to Vim. Very, very useful. You could probably build something actually somewhat similar in VS Code that'll communicate to tmux. I don't think it'd be very, very hard, same with IntelliJ. But it's just a different kind of environment, but this nonetheless allows me to do it from Vim.

[00:02:08]
Now, the reason why I chose Vim is because often I'm in Vim, but also I chose one other location. Let's see, I have it in my zsh profile. And the reason why I have it in my zsh profile is that if I'm not in Vim, I'm in a terminal.

[00:02:24]
And so when I'm in a terminal, if I press F, it's just gonna have that fuzzy find happen right there, I just use my shell to do this. Now that I have my shell, I can then do that and then that will open up the new session. So it didn't close my previous window, cuz my previous window I was just using, so just shows this right here.

[00:02:44]
So it's kind of my two ways, that's kinda where I put it, that was my strategy for making this very, very useful for me. Zsh has a vi-mode option. Nice, yes.
>> Sorry, did you just press F?
>> No, I pressed Ctrl+F.
>> Ctrl+F, okay.
>> Yeah, here, yeah, look, bindkey.

[00:03:04]
See, it's that thing. It's Ctrl+F. So I press Ctrl+F and I send literally into this sessionizer with a new line, so I just execute it, right? That's all I'm doing, so technically, if we're gonna be honest here, I'm in the middle of doing this and then I press Ctrl+F, not that useful, right?

[00:03:20]
That's probably not the best way I wanna go, but nonetheless, it works very, very well for me. I know the edges, I'm fine with this. I could technically created something that was a little bit better. I could have used tmux, but the problem is, is that I'm not always in tmux, right?

[00:03:36]
Really right when I start, I'm not in tmux, so I want to be able to just open up a terminal, right, Ctrl+F, and then go to work, whatever, right? Work, tmp, boom, I'm in exactly where I wanna be. So I just wanted it available in zsh and then I knew from there on out, I'm gonna either be in a terminal or in Vim, and so that's kind of why I chose where I chose it.

[00:03:54]
This is a little bit more theory than practice, right? I'm kind of giving you how I thought about a problem, not necessarily how you should do it. Maybe you should use i3 to help you out. You could use i3, Rofi window, who knows what you could use? There's a million ways you can solve this problem, I'm just trying to show you a way to solve this problem and a way that I thought was pretty nice.

[00:04:14]
And that really makes the thing super easy, but not only that, remember this whole Ansible thing we've been doing now for a while. If you look inside my dotfiles, when it calls stows and stows my dotfiles, I have a folder called bin, which has a folder called local, which has a folder called bin, which has these three things and one of them is this one right here.

[00:04:35]
So here's mine, mine's a little bit bigger than yours. But that's all it does, it actually puts this into my, what is it called, my local bin, right? And so everything in here is things that are available for me to be able to execute right here. So I have the Python formater right here.

[00:04:53]
It's just that I have my Python language server, I have screenkey, cheat sheet I have all them all right here, so I can easily use these anywhere within my system and they install nicely for Ubuntu. So that's kinda my general plan for using those and that's how I've made it work with stow, which is how I get it installed via Ansible.

[00:05:12]
So I know the moment that I have Ansible up and running, I can just use that, that's kind of our first tmux script, we actually did something there, right? I feel like we genuinely accomplished something that does kinda solve that terminal problem, right? In a sense, you have all these terminals everywhere and usually they're associated with an individual project.

[00:05:29]
You can create windows for each one of the things you need to run inside of a folder and now you can navigate to the folders you want with just three, four keystrokes and you're there. And you can fuzzy find for them, makes it really, really easy. To me this seems like a good way to solve that problem.

[00:05:46]
It really allows you to be able to move quickly, at least in my book, this has been incredibly helpful, for solving that problem. Nonetheless, that's just how I go about trying to solve these problems cuz really, that terminal problem with all the terminals, I mean, it's plagued me for a long time.

[00:06:00]
I've tried so many different ways to solve this and it's been really hard coming up with the right one. I've tried many different styles, I've tried many different ways to get this done and this to me seems like one of the best ways to really make it so that I have a very seamless integration with me and how I interact with my running processes.

[00:06:18]
Super easy to find, I really like it. There's other ways you could do it, just for fun, you know how we have tmux list sessions, right now only have one. If you have all of your sessions named, you have all your windows named, you create those, you could technically pipe into FCF all the sessions, get out the one you want to select, and then use the targeting mechanism to target that exact item in the session window and pane.

[00:06:42]
So you could really get pretty fine grain with how you want to do things. I haven't done that yet, I haven't needed it. I don't really have a single project that I run eight terminals in. But I mean, there could come a day where that is the case, and I want to be able to jump there pretty darn quick.

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