Tmux
In this quick article i will talk about tmux, why you should use it, and how, have fun :)
Let's start with a small definition of Tmux, its basically a terminal multiplexer, what does that mean? it means that it can help you manage multiple terminals and windows (hence multi :D), anyway we use it just to navigate around in the terminal WAY more easily and do multiple stuff which will get to later...
Installation
For installation, if you don't have it you can just do
sudo apt install tmux
And that's it.
Main Purpose
Now Tmux has many cool things to offer, but of the most useful things about that, that not all multiplexers give, that it runs the window in a process and not a session, this is handy because as we all know processes can run in background, which means we can just save the Tmux session we have now, and reconnect later.
Another vital example is if we remotely connected to a server (using SSH) we can lunch Tmux there and start working, if our connection dies or something happens, we can reconnect back, and everything will be in place because everything is saved as it is. This is handy in managing remote servers, or even more fun, working with pivots during a penetration test.
The Fun
Now let's actually start using it and let's explain some settings so we practice it using the settings that we want, to create a Tmux sessions we do:
tmux new -s "RandomName"
This will create a session for us with this "RandomName", you can set a keyboard shortcut for that, to make life more easier.
Now say we have launched our session and in the first window we started a process that still running, and we don't want to set it to the background, let's open a new window (in the same session).
To do so we need to use a thing called "Prefix Key" this is the ultimate key that you will be spamming to do almost everything in Tmux, by default this key is "ctrl + b" which can be annoying but i will showcase later how we can customize all keys in Tmux.
Now that we hit the prefix key, this means we can use the special moves we have, for example if you hit "c" you will create a new windows, and you can notice that in the bar down below
As we can see we have created a new window with number 1 and we moved to it directly, now say we want to go back to our first windows AGAIN we have to hit the prefix key "ctrl + b" then specify it's number which is 0.
So we know now using numbers we can navigate through windows, what else can we do?
Hitting prefix key then "x" will let us kill (delete) a window if we are done with it.
Now seeing this, ok we can create windows bla bla bla, BUT we can also create sub windows kinda? what i mean here is splitting the terminal itself so we can do AND see multiple stuff at the same time which is SUPER useful during a lot of scenarios.
To do so, again we hit our magical prefix key and we have two options "%" to split horizontally and "
to do it vertically, (now bare with me i know these keybindings are weird but we will change them later dw).
So let's say i have a windows that i split horizontally then again vertically it would look like:
And this gives sooo many options for us during our day-to-day practice or work or else, now to move through panes it's really simple just hit the prefix key and then move using arrows to what ever pane you want, and if you are done with a pane, treat them like windows, you can just prefix + "x" to kill the pane and leave the rest if you need them.
Another thing we can do is moving through the keyboard LITERALLY, in Tmux we can't use the mouse so if we need to move up in the windows and copy stuff, we need to enter copy mode, this may sound hard or dumb to you but it is more efficient, you do everything faster if your hands are always on the keyboard and not juggling between devices every now and often.
To enter copy mode we hit our prefix key and then hit "[" now we can see our cursor moves freely on the window and can go up, this has multiple uses too, for example now hitting "/" will let us search through stuff, so search for any word that might have appeared earlier in some command or output and go to it, eases your life when your doing so many stuff.
And when you want to actually copy you hit space bar and start highlighting stuff, when you done you just hit Enter and that's it. (note that in copy mode you can also move in pages not just lines using the FN key).
And if you REALLY wanna use your mouse then you can use it normally, and to paste stuff, you do prefix then "]" but don't worry about that we will enable normal pasting later (ctrl + v ).
Now you probably have multiple panes in your terminal, but what if you want to zoom in one of them and focus there? well just hit the prefix key then "z" this will make the pane your using full screen and ignore the other panes, want to go back? move freely with arrows like we said earlier and you will zoom out.
Configuration
Now i will show you my config file, if you like the setting you can just copy paste, but i will briefly explain what they are so if you wish to do a custom setting.
Here we have the Tmux configuration which you should save in ~/.tmux.conf
and if it's the first time your loading Tmux with new settings, you should do:
tmux -f ~/.tmux.conf
To enable them, anyway, here we are setting the prefix key to "alt + w" because it's so much easier and you can press it really quickly without moving too much unlike "ctrl + b".
Also we are setting the copy thing, so whenever we copy thing from Tmux it will save it to our system clipboard to use it freely outside of the Tmux windows.
After that it's not much stuff im just removing the "
and "%" because i didn't like them and prefer to use "=" for horizontal splits, and "-" for vertical ones.
NOTE: you have to add the pane current path options, so each new windows is made from the same directory you are in right now, this saves so much trouble trust me.
And that's it! sorry this maybe was too long, but trust me it's worth it, using Tmux helps really in increasing your efficiency during your work once you get the grip of it, if you have any further questions, don't hesitate to get in touch!
See ya around and happy hacking :)
Last updated