Tmux basics and windows0:00
One last thing I want to chat about before we configure NeoVim is tmux. Let's jump into our terminal and start it by running the tmux command. We can tell we're in tmux because of this green bar down the bottom of the screen that shows the current session name, the currently open windows, and some status information on the right which we can customize. To issue commands to tmux, we need to enter a prefix key to let tmux know that the command is intended for tmux and not for whatever program we're running inside it. The default prefix is ctrl-b, so to create a new window, we could press ctrl-b and then c, and you'll see down the bottom we now have two open windows. To switch between these windows, we can use ctrl-b and then the window number, so ctrl-b
Panes, sessions, detach1:13
To switch between panes, we can use ctrl-b and then the arrow keys to select the pane we'd like. Not every command in tmux has a default keybinding, so to access all of the commands available, we can use ctrl-b and then the colon, and this drops us down to a little command prompt you can see down the bottom, and we can type something like new-session, and we can use tab completion here, and then let's give this session a name of Laracasts, and so now you'll see we have a whole separate session. To switch between sessions, we can use ctrl-b s, and that will list all of the sessions, and we can switch between these and we'll get a little preview showing the splits and the contents of the window.
and we can switch between these and we'll get a little preview showing the splits and the contents of the window. One really cool feature of tmux is the ability to detach, so if I say ctrl-b and then d, that will detach from our current session and drop us back to our shell. If we run tmux again, it'll reopen our session right where we left off, and this even works if we close our terminal, so if I close this over here and then reopen a brand new terminal and run tmux again, then you'll see once again we're right where we left off. Alright, let's close this so we can start configuring. To close a pane, we can use ctrl-b x to kill the current pane, and it asks us to confirm, so we'll say yes.
To close a pane, we can use ctrl-b x to kill the current pane, and it asks us to confirm, so we'll say yes. Alternatively, if you close whatever program is running inside a window, it will also close the window. So in this case, both of these windows are running zsh, and we can exit zsh by doing ctrl-d, and that closes one of them, or we can type in exit, and that will also quit. Now, we did have another session open, so let's just quickly kill the entire server by saying tmux kill-server, and then let's clear this. Now let's talk about configuring tmux.
Create tmux config file2:44
by saying tmux kill-server, and then let's clear this. Now let's talk about configuring tmux. So let's jump into our .files repo, so I placed mine in here, and if you remember from previously, we had an install script and a directory for our terminal configuration. So let's go ahead and create a new directory for our tmux configuration, and then inside here, let's create tmux.conf. Now I could probably create an entire separate LaraCast series just on configuring and using tmux, so I'm going to take a bit of a shortcut here and jump over to GitHub, where I can pull my current tmux config from my .files. So let's switch back here, and then we'll paste that in.
pull my current tmux config from my .files. So let's switch back here, and then we'll paste that in. Now I wouldn't recommend just copying someone else's config verbatim. Instead, have a look through different people's configs, see the settings they use, and understand what they do, and decide if that is right for you. Now we'll come back to this later, so let's just close this for now, and open up our install script. We'll use Shift-G to jump to the bottom of the file, and then we'll press O to open a new line, and let's create our symlink, so we'll say ln -s .files tmux. Now tmux is expecting a single configuration file rather than a full directory, so we're
new line, and let's create our symlink, so we'll say ln -s .files .tmux.conf. Now tmux is expecting a single configuration file rather than a full directory, so we're going to symlink the config file specifically, and tmux is looking for this in the home directory in .tmux.conf. Let's press Shift + O to put a new line above, and we'll also just go ahead and remove that file in case it already exists, and then let's save and quit, and run our install script. Alright, that looks good, but let's confirm it by listing the contents of our home directory, and we'll filter that down to show any things with tmux in the name. So here we can see our .tmux.conf file is symlinked to our .files directory. Alright, let's start up tmux, and we can see our configuration took place because our status
Review tmux settings4:27
So here we can see our .tmux.conf file is symlinked to our .files directory. Alright, let's start up tmux, and we can see our configuration took place because our status bar is now a different color. Let's open up our tmux.config now, and we'll go through some of the settings I use. So first of all, I use vi mode, which just adds some vi key bindings. This is personal preference, obviously. I increase the history limit, so if you run a command that has a whole bunch of output, and you want to be able to scroll back up to see what was at the top, this just increases the amount of history that tmux will store. This one here is kind of interesting.
Build project switcher script8:35
And that's what this T script does. So I can press Ctrl-Space and then Shift-F, and it'll run this T command in a new window. I also have this other one here where I can say Ctrl-Space and Shift-D, and it will run the T script, but it'll pass in a directory, and we'll see how that works shortly. So let's go ahead and create this T script. So I'm going to jump over to this other window we already created here. So in our .files directory, let's go ahead and make a directory called scripts, and inside scripts let's create a script called T. Now I'm going to jump back over to GitHub and copy this one from my .files repo and paste it in here.
Now I'm going to jump back over to GitHub and copy this one from my .files repo and paste it in here. And then let's walk through just what this is doing. So up the top here, if we pass in an argument to our script, that will become the selected item automatically. Otherwise, we build up a list of items. And so I've set this to search for every directory in my code directory to a maximum depth of 2, and then I also append onto that the temp directory, because I like to be able to quickly switch to a temporary session in the temporary directory where I can just create a little test project.
switch to a temporary session in the temporary directory where I can just create a little test project. We then pipe these items into FCF, which is a fuzzy finding tool, so we can quickly search between them. Now for this demo, I'm just going to come over here and filter this down to Laracasts and change the max depth to 1, just so I don't show everything on my computer. So once we have our selected item, we then grab the basename of the item. So if we've got a full path, basename just gives us just the directory name. And then we go and replace any dots with an underscore, because tmux doesn't like to have a dot in the session name.
And then we go and replace any dots with an underscore, because tmux doesn't like to have a dot in the session name. We then ask tmux to switch to that session, and that session may not exist, but if it does exist, then this script will exit and everything's fine. But if it doesn't exist, then tmux will go ahead and create that session with the name and the directory we want and switch to that. So let's go ahead and save and quit this, and let's also make that executable. So we'll chmod +x scripts/t. Let's also create a symlink for this. So let's open our install script again, come back down to the bottom, and let's create
Let's also create a symlink for this. So let's open our install script again, come back down to the bottom, and let's create a symlink from .files/scripts to home.local/bin. And I'll show you how this is set up just in a minute. So let's go ahead and quickly create our remove command to remove this if it already happens to exist. And one other thing we may need to do here is this bin directory probably doesn't exist by default. So let's go ahead and add a makedir command. So we'll say makedir home.local/bin.
So let's go ahead and add a makedir command. So we'll say makedir home.local/bin. Now even this .local directory may not exist, so I'm going to pass in the -p flag, which just makes sure makedir will make any required parent directories. All right, so let's save this, and let's run our install script again. Now we place that symlink in home.local/bin. Now the reason I've done that is because I've added that directory to my PATH. And if I say echo $PATH, we'll see that this .local/bin is the first entry in my PATH. And that means I can just run t on its own without having to specify the full path, and
Test script and workflow12:06
All right, so now we're ready to test this script out. So let's go control space and then shift F, and you'll see now it's given us a listing of all the files that I've filtered down to. So I just said everything in the laracast directory and the temp directory. So I can use the arrows, or I can also just type Laravel, and then once I press enter, it will create a brand new session in that Laravel directory. So if I do a print working directory, you'll see we're already in here, and down the bottom you can see our session name is also Laravel. If I switch back to a different session, so we'll say control B S and go back to our default session here.
If I switch back to a different session, so we'll say Ctrl+B and go back to our default session here. If I was to go Ctrl+Space, Shift+F again, and we get our same listing. If I choose the Laravel option this time, instead of creating that session, it'll just switch to it. And that's how I manage all the different projects that I work on. In the next lesson, we'll start configuring NeoVim.
