Do you want a powerful, flexible, and automatized terminal experience? Let me introduce you to tmux. It’s one of the most important tool for my development environment. If you never heard about tmux, fear not, this article will explain the core ideas:
While reading, I strongly advise you to have tmux open on the side to play along with it. I advise you as well to create your own cheatsheet with the commands you’ll learn today. They will be easier to memorize if you write them, and you’ll have a personalized reference when your memory will fail you. A last thing: it can be useful if you know already the basics of Vim, since the configuration we will build together mimic some Vim keystrokes. It’s however not mandatory. What’s tmux?Installing tmuxBefore I can show you what’s tmux, we surprisingly need to install it. If you use a Unix/Linux based system, you can find it via your usual package manager:
What Is tmux?tmux is a terminal multiplexer. You can create multiple tmux If you already know GNU-screen (another terminal multiplexer), tmux is similar but more powerful and easier to config. To understand the concept, let’s try to create a tmux You can list every Now, let’s run this in your terminal:
This loop will run forever if you don’t stop it! Now, close the terminal. Did the loop stopped? Not at all! It continues to run in the background, because the tmux You don’t believe me? You’re right; you need to experiment by yourself.
Your infinite loop is back, and you can see that it was still running in the background! Let’s really kill it this time, by pressing “That’s nice and all your zombie stories, but what’s the point?” could ask many readers. Well, let’s find out. Why Using tmux?Background OperationsAs we saw above, you can detach a tmux Let’s imagine that you need to run a very long script on your remote server. You could:
More Terminals! Everywhere!Tmux allows you to create multiple terminals on a single screen. This is the functionality I use the most. You might think: “Well, great, many terminal emulators can do the same, like terminator”. That’s true, but tmux is more powerful and consume less resources. You can configure it easily and precisely, according to your specific needs. It works very well with Vim too, which makes it mandatory if you want to create a mouseless development environment. Protection Against Terminal CrashesSince your tmux Saving tmux SessionsIt’s possible to save tmux Remote Pair ProgrammingA tmux How to use tmux?General OrganizationLet’s see in more details how to use tmux. This part should answer many potential questions you might have, at that point. Here’s an example what kind of hierarchy you can with tmux: When you launch tmux, it will create a tmux ServerThe tmux server manage every single tmux SessionsWe spoke about WindowsIn tmux, a PanesYou can split your tmux Workflowtmux KeystrokesLet’s speak about the workflow itself. It’s very easy to use tmux with the keyboard only: that’s why it’s part of my mouseless development environment. Another great strength! However, tmux needs a way to separate its own shortcuts with the CLIs’s shortcuts running in the different If they overlap, you might accomplish different actions in tmux and in Vim, at the same time. This is sometimes what we want, but most of the time it’s confusing. That’s why most of the keystrokes in tmux need to be done after entering a prefix key. This prefix key is Let’s try it: go back to a tmux If you think that the keystrokes are awkward and difficult to remember, no worries: we will change them later. I will represent, in this article, the keystrokes which must be hit after the prefix keyas follow: Command PromptYou can execute special tmux commands via a command prompt:
You can execute many commands in tmux via some keystrokes or via this command prompt. Managing tmux SessionsHere are the most useful tmux commands to manage your
If you use a tool to automate the creation of your Configuring tmuxLet’s now create our own configuration to make tmux keystrokes more user-friendly. This configuration is not the best-configuration-in-the-universe-and-beyond, but adding step by step what you might need and playing with tmux at the same time will teach you how tmux works. I encourage you to personalize this configuration following your own needs. It’s time to configure tmux. Create the configuration file Changing The Default Prefix KeyThe default prefix key I prefer using Let’s modify the prefix key by adding the following in the configuration file:
Here, the It can be useful if you want to try multiple prefix key on the fly. Reloading tmux Config FileEach time you change your config file, you need to reload it in tmux to apply the changes. Let’s set a keystroke to reload the config file easily:
Here, we have two commands combined into one, on the second line:
Let’s stop tmux by typing From now on, each time you want to see the effect of your configuration changes, you need to reload the config file. Mouse ModeWhat? A mouse in a mouseless environment? Well, using the mouse is sometimes practical, especially since you might use it in other software, like your browser. Enabling the mouse allows you to use it for:
Let’s add it in the configuration:
You can do everything with your keyboard too, and I encourage you to do so. Splitting PanesOne of the most common operation you’ll do in tmux is splitting a
You might think that something is reversed here, between my keystroke The flag Resizing PanesYou can resize the Navigating PanesTo navigate from
The flag Why using More History! MORE!One of the functionality I love in tmux is being able to have thousands of lines from the terminal outputs. You need to add the following to your config file:
You can then perform some search on the past output very easily, especially if you use the search plugins I describe below. WindowsWe know how to split the current Let’s add some more keystrokes to our config file:
The keystroke You can see some tabs in the status line, at the bottom left of your tmux When you have more than one You can see that a little star in the You can notice as well that the To stay consistent, let’s do the same with the
You might think it’s annoying to do
Copy ModeLet’s speak briefly about tmux modes. There are two of them:
We actually use the default mode of tmux. You can think of it as the equivalent of the Insert Mode in Vim. It allows you to type whatever you want in the different When you are in copy mode, you have the possibility to navigate in your terminal, using Emacs or Vi key bindings. Since I’m in love with Vi/Vim, let’s add in the config:
Now, let’s try the copy mode! Hit You can see that you’re in copy mode thanks to the two numbers on the top right corner of your From there, you can use the vanilla Vim keystrokes to navigate your current terminal output. Again, I wrote about the basic Vim keystrokes in this article. You can use, for example:
Exactly like in Vim! Unsurprisingly, you will use copy mode mainly to copy content. I you tried to copy terminal outputs already from tmux with your mouse, you might be confused: it doesn’t really work. Indeed, tmux by default doesn’t copy anything in your system clipboard, but in one of its Let’s configure that now:
To stay consistent with Vim’s default keybinding, we did the following:
Then, we pipe the action to
Be aware as well that you can past what’s in the tmux And voila! Now, copy pasting in tmux will normally work like a charm. You can as well copy using the mouse: select what you want and enter Integrate Your tmux With VimThere are some more configuration you need in order for tmux to work seemingly with Vim. If you don’t use Vim, you can pass this section. First, if you use Neovim in tmux, you might experience a cursor problem: it doesn’t change from rectangle to pipe when you go from Normal Mode to Insert Mode. Adding this line in your configuration might help:
Another improvement you might want: navigating between tmux This config will do exactly that:
That’s all! Now you have a personalized tmux configuration you can modify to fit your precise needs. There are a lot of tmux configuration available out there, if you need some inspiration. You might think that it will take you forever to remember these keystrokes, but I believe they are overall pretty logical. Moreover, if you have written your own cheatsheet, I bet you know most of them already. tmux ColorsRight now, the status bar of tmux is quite ugly: you can’t really see properly the You can personalize the status bar of tmux extensively. Changing tmux colors can help to bring clarity in the status bar. One popular theme you can try is You can look at my own personalized design too as well, for inspiration. Enhancing tmux With PluginsEven if we have now a very powerful tool, we can improve tmux even more by adding some useful plugins. tmux Plugins ManagerTo manage our tmux plugins, we need the tmux Plugin Manager. To install it, follow these steps:
You can add any plugin you want in your config and install them with A Better Search With CopyCatAs we saw, tmux copy mode allow you to search in the output of the current What are the cool benefits, you might ask?
Like in Vim, you can use To install copycat, copy this line to your config:
Then, you need to reload your tmux config and hit Fuzzy Search With fzf And ExtraktoYou’ll need fzf touse this plugin. If you don’t have it already, I advise you to install it: it’s a very powerful tool which will enhance your terminal experience even more! In order to install extrakto, add the following to your config file:
Then, reload it and hit You can now fuzzy search the output of your terminal using Automating The Creation of tmux SessionsThe power of tmux doesn’t stop here. There are many tools out there which allow you to automate tmux You can automate everything, from the number of The two most known tools to create Here’s a simple example of a
This configuration set up a You need to create every This configuration is simple, but you can personalize your tmux The Portal to the tmux World Is Now Wide OpenNow that you’ve played around with tmux you can use it to its maximum. Don’t hesitate to try new configurations or coupling it with tools and plugins to make the experience personalized. To dig more into tmux, I would definitely recommend you to look at the well written manual ( Related Sources |
|
来自: astrotycoon > 《待分类》