{"id":112,"date":"2025-03-01T14:58:39","date_gmt":"2025-03-01T14:58:39","guid":{"rendered":"https:\/\/haco.club\/?p=112"},"modified":"2025-03-01T14:58:39","modified_gmt":"2025-03-01T14:58:39","slug":"a-quick-and-easy-guide-to-tmux","status":"publish","type":"post","link":"https:\/\/haco.club\/?p=112","title":{"rendered":"A Quick and Easy Guide to tmux"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\">https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux<\/a><\/p>\n<\/blockquote>\n\n\n\n<p>I love working with the command line. I think there\u2019s hardly any more productive and versatile tool for a software developer than the terminal. The additional hacker\/wizard\/neckbeard kind of feeling you get when using a terminal comes for free, what\u2019s not to love?<\/p>\n\n\n\n<p>Over the years I\u2019ve tried to streamline and customize my command line experience to be more convenient, more fun to use or just to look rad. One of the most important tools to drive my daily command line experience is&nbsp;<code>tmux<\/code>. Check this out:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/hamvocke.com\/_astro\/tmux.D4cBv9kl_266hBL.webp\" alt=\"tmux in action\"\/><\/figure>\n\n\n\n<p>This screenshot shows nothing less than the best thing since sliced bread. It\u2019s&nbsp;<a href=\"https:\/\/tmux.github.io\/\">tmux<\/a>, a so-called&nbsp;<em>terminal multiplexer<\/em>. Simply speaking, tmux acts as a window manager within your terminal<sup><a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fn-1\">1<\/a><\/sup>&nbsp;and allows you to create multiple windows and panes within a single terminal window.<\/p>\n\n\n\n<p>This post will give you the same quick introduction to tmux and its possibilities, followed by a 10 minute hands-on guide to set up and get to know tmux yourself. If you\u2019ve got 10 minutes to spare and want to earn street cred with your nerd friends: read on and become proficient with tmux!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"whats-tmux\">What&#8217;s tmux?<\/h2>\n\n\n\n<p>tmux\u2019s authors describe it as a&nbsp;<em>terminal multiplexer<\/em>. Behind this fancy term hides a simple concept: Within one terminal window you can open multiple windows and split-views (called&nbsp;<em>\u201cpanes\u201d<\/em>&nbsp;in tmux lingo). Each pane will contain its own, independently running shell instance (bash, zsh, whatever you\u2019re using). This allows you to have multiple terminal commands and applications running side by side without the need to open multiple terminal emulator windows.<\/p>\n\n\n\n<p>On top of that tmux keeps these windows and panes in a&nbsp;<em>session<\/em>. You can exit a session at any point. This is called&nbsp;<em>\u201cdetaching\u201d<\/em>. tmux will keep this session alive until you kill the tmux server (e.g. when you reboot)<sup><a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fn-2\">2<\/a><\/sup>. This is incredibly useful because at any later point in time you can pick that session up exactly from where you left it by simply&nbsp;<em>\u201cattaching\u201d<\/em>&nbsp;to that session.<\/p>\n\n\n\n<p>If you\u2019ve ever worked with remote servers or a Raspberry Pi over ssh you can guess where this will be useful: When you lose your ssh connection the tmux session will simply be detached but will keep running on the server in the background including all the processes that run within your session. To continue your session simply ssh to the server again and attach to the running session.<\/p>\n\n\n\n<p>tmux is helpful when working on a remote machine but it shines just as much when you\u2019re working locally. Not only for its window management features but also for the session handling. Personally I find myself detaching from sessions when I\u2019m switching context. I\u2019ll just start a new session for my new task and attach to the old session whenever I want to continue with my old task.<\/p>\n\n\n\n<p>You see that tmux basically offers two big features:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Window management in your terminal<\/li>\n\n\n\n<li>and session management<\/li>\n<\/ol>\n\n\n\n<p>If you are familiar with&nbsp;<a href=\"https:\/\/www.gnu.org\/software\/screen\">GNU Screen<\/a>&nbsp;this might all sound familiar. Think of tmux as an easier-to-use and a little more powerful alternative to Screen (obviously I\u2019m being opinionated here).<\/p>\n\n\n\n<p>Enough with the concepts. Let\u2019s get our hands dirty!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-started\">Getting Started<\/h2>\n\n\n\n<p>This hands-on guide will get you up and running with tmux pretty quickly. It will only cover the basic features which should be more than enough to get started and be productive with tmux. Simply open your terminal and follow the instructions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"installation\">Installation<\/h3>\n\n\n\n<p>Fortunately installing tmux is pretty straightforward on most operating systems: a simple&nbsp;<code>sudo apt-get install tmux<\/code>&nbsp;(Ubuntu, WSL and derivatives) or&nbsp;<code>brew install tmux<\/code>&nbsp;(Mac) should be sufficient.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"starting-your-first-session\">Starting Your First Session<\/h3>\n\n\n\n<p>For your first session simply start&nbsp;<code>tmux<\/code>&nbsp;with a fresh session:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux<\/code><\/pre>\n\n\n\n<p>This will create a new tmux session with a nice all-green status bar at the bottom:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/hamvocke.com\/_astro\/tmux_blank.Dr18TZ6y_1nzylF.webp\" alt=\"vanilla tmux on startup\"\/><\/figure>\n\n\n\n<p>The status bar is an important part of tmux. Apart from the currently opened windows (on the left) it also shows some system information like date and time (on the right). The status bar can also be customized and I\u2019ve seen some really fancy stuff around (upcoming calendar events, battery status, to name a few) but this is something we\u2019ll leave for later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"splitting-panes\">Splitting Panes<\/h3>\n\n\n\n<p>Now that we\u2019ve created our first session we can get a feeling for panes. When you create a new session, tmux will by default start with one window and a single panel inside. We want a nice split-screen, so let\u2019s split this bad boy.<\/p>\n\n\n\n<p>All commands in tmux are triggered by a&nbsp;<strong>prefix key<\/strong>&nbsp;followed by a&nbsp;<strong>command key<\/strong>&nbsp;(all you&nbsp;<code>emacs<\/code>&nbsp;weirdos out there will feel right at home). By default, tmux uses&nbsp;<code>C-b<\/code>&nbsp;as prefix key. This notation might read a little weird if you\u2019re not used to it. In \u201cemacs\u201d notation&nbsp;<code>C-<\/code>&nbsp;means \u201cpress and hold the&nbsp;<code>Ctrl<\/code>&nbsp;key\u201d<sup><a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fn-3\">3<\/a><\/sup>. Thus&nbsp;<code>C-b<\/code>&nbsp;simply means press the&nbsp;<code>Ctrl<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;keys at the same time.<\/p>\n\n\n\n<p>The shortcut to split panes into a left and a right pane is&nbsp;<code>C-b %<\/code>. This means: to split your single pane into a left and a right pane you press&nbsp;<code>Ctrl<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;at the same time, release both, and then type the&nbsp;<code>%<\/code>&nbsp;key. Voil\u00e0! You\u2019ve just invoked your first tmux command and split your pane in two.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/hamvocke.com\/_astro\/tmux_split.b845PJCE_Z2bALRv.webp\" alt=\"tmux with two split panes\"\/><\/figure>\n\n\n\n<p>Where there\u2019s a split into left and right, there\u2019s also a split into top and bottom pane. To split a pane into top and bottom panes use the&nbsp;<code>C-b \"<\/code>&nbsp;shortcut.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"navigating-panes\">Navigating Panes<\/h3>\n\n\n\n<p>Right now we\u2019re trapped in the newly created pane. But we really really want to go back to the left one. Easy peasy: Switching to a different pane uses the&nbsp;<code>C-b &lt;arrow key&gt;<\/code>&nbsp;shortcut, where &lt;arrow key&gt; is the arrow key pointing to the pane you want to switch to. In our case we want to switch to the panel on the left so it\u2019s&nbsp;<code>C-b left<\/code>&nbsp;for us. Just once more, so that we fully understand this: This means you press&nbsp;<code>Ctrl<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;(your prefix) followed by the&nbsp;<code>left<\/code>&nbsp;arrow key to get to the pane on the left.<\/p>\n\n\n\n<p>You can now go ahead and split each of your new panels even further. Feel free to experiment and split your panes like a maniac to get a feeling for it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"closing-panes\">Closing Panes<\/h3>\n\n\n\n<p>Closing a pane is as simple as closing a regular terminal session. Either type&nbsp;<code>exit<\/code>&nbsp;or hit&nbsp;<code>Ctrl-d<\/code>&nbsp;and it\u2019s gone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-windows\">Creating Windows<\/h3>\n\n\n\n<p>Windows in tmux can be compared to creating new virtual desktops; if you\u2019ve ever worked with one of the major Linux desktop environments (KDE, Gnome) or a Mac you\u2019ll hopefully find this analogy helpful.<\/p>\n\n\n\n<p>Creating new windows is as easy as typing&nbsp;<code>C-b c<\/code>&nbsp;(one last time: that\u2019s&nbsp;<code>Ctrl<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;at once, then&nbsp;<code>c<\/code>). The new window will then be presented to you in tmux\u2019s status bar.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/hamvocke.com\/_astro\/tmux_window.BaMbDeGW_2eEFmx.webp\" alt=\"tmux with two windows\"\/><\/figure>\n\n\n\n<p>You can now divide the pane in your new window as you like. Or don\u2019t. That\u2019s up to you.<\/p>\n\n\n\n<p>To switch to the&nbsp;<em>previous<\/em>&nbsp;window (according to the order in your status bar) use&nbsp;<code>C-b p<\/code>, to switch to the&nbsp;<em>next<\/em>&nbsp;window use&nbsp;<code>C-b n<\/code>. If you\u2019ve created many windows you might find it useful to go to a window directly by typing its number (the status bar will tell you which window has which number), just use&nbsp;<code>C-b &lt;number&gt;<\/code>&nbsp;where &lt;number&gt; is the number in front of the window\u2019s name in your status bar.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"session-handling\">Session Handling<\/h3>\n\n\n\n<p>If you\u2019re done with your session you can either get rid of it by simply exiting all the panes inside or you can keep the session in the background for later reuse.<\/p>\n\n\n\n<p>To detach your current session use&nbsp;<code>C-b d<\/code>. You can also use&nbsp;<code>C-b D<\/code>&nbsp;to let tmux give you a choice which of your sessions you want to detach. Detaching from a session will leave everything you\u2019re doing in that session running in the background. You can come back to this session at a later point in time.<\/p>\n\n\n\n<p>To re-attach to a session and continue where you left you need to figure out which session you want to attach to first. List the currently running sessions by using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux ls<\/code><\/pre>\n\n\n\n<p>This will give you a list of all running sessions, which in our example should be something like<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>0: 2 windows (created Sat Aug 15 17:55:34 2015) [199&#215;44] (detached)<\/p>\n<\/blockquote>\n\n\n\n<p>To connect to that session you start tmux again but this time tell it which session to attach to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux attach -t 0<\/code><\/pre>\n\n\n\n<p>Note that the&nbsp;<code>-t 0<\/code>&nbsp;is the parameter that tells tmux which session to attach to. \u201c0\u201d is the first part of your&nbsp;<code>tmux ls<\/code>&nbsp;output.<\/p>\n\n\n\n<p>If you prefer to give your sessions a more meaningful name (instead of a numerical one starting with 0) you can create your next session using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux new -s database<\/code><\/pre>\n\n\n\n<p>This will create a new session with the name \u201cdatabase\u201d.<\/p>\n\n\n\n<p>You could also rename your existing session:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tmux rename-session -t 0 database<\/code><\/pre>\n\n\n\n<p>The next time you attach to that session you simply use&nbsp;<code>tmux attach -t database<\/code>. If you\u2019re using multiple sessions at once this can become an essential feature.<\/p>\n\n\n\n<p>And that\u2019s it! Congratulations, you\u2019ve just completed your first tmux session and got your hands dirty with its window and session management. Yes, there\u2019s more stuff tmux can do. But what you\u2019ve just learned should be everything to start using tmux in the future.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-tmux\">Why tmux?<\/h2>\n\n\n\n<p>A response that I get quite often is: \u201cGreat, I get it. But why should I use tmux and its weird key combinations instead of just using iTerm2?\u201d<\/p>\n\n\n\n<p>And you\u2019re right, when it\u2019s only basic window management,&nbsp;<a href=\"http:\/\/iterm2.com\/\">iTerm<\/a>&nbsp;for Mac supports tabs and panes as well. For Linux there\u2019s&nbsp;<a href=\"https:\/\/gnome-terminator.org\/\">Terminator<\/a>. So why would anyone feel the urge to learn some archaic technology in this day and age?<\/p>\n\n\n\n<p>Well, obviously there\u2019s the bragging rights. But if that doesn\u2019t convince you, there are a couple of actual reasons why I favor tmux over iTerm et al.:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>session handling: detaching from and attaching to sessions helps me with context switching and remote working<\/li>\n\n\n\n<li>platform independence: I can use tmux on my Macbook, my Linux notebook, servers, Raspberry Pis, you name it.<\/li>\n\n\n\n<li>customizable: there are many ways I can customize the look and behavior of my tmux environment. And I can sync this across different platforms using a simple dotfile<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"moving-on\">Moving on<\/h2>\n\n\n\n<p>If you\u2019re curious to learn what else tmux can do that\u2019s a great thing. And luckily most of the stuff is quite simple to discover. Just type&nbsp;<code>C-b ?<\/code>&nbsp;to see a list of all available commands and start experimenting.<\/p>\n\n\n\n<p>Some of the commands that I\u2019m using myself quite often are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>C-b z<\/code>: make a pane go full screen. Hit\u00a0<code>C-b z<\/code>\u00a0again to shrink it back to its previous size<\/li>\n\n\n\n<li><code>C-b C-&lt;arrow key><\/code>: Resize pane in direction of &lt;arrow key><sup><a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fn-4\">4<\/a><\/sup><\/li>\n\n\n\n<li><code>C-b ,<\/code>: Rename the current window<\/li>\n<\/ul>\n\n\n\n<p>On top of that there are plenty of resources out there that help you getting further with tmux. People like me are blogging about their tmux experience and share what they\u2019ve discovered. You can find people sharing their tmux configurations in their dotfiles repos on Github. There\u2019s even a&nbsp;<a href=\"https:\/\/pragprog.com\/book\/bhtmux\/tmux\">book by Brian Hogan<\/a>&nbsp;dedicated to tmux.<\/p>\n\n\n\n<p>For now it\u2019s best to discover some stuff on your own. Experiment, fool around and maybe try to use tmux for your daily work. It takes a while to get used to and you\u2019ll probably feel slow when you start out. I encourage you to keep using it. Get a feeling for its functionality and in no time you\u2019ll find out that your work with the terminal will be pure bliss and refreshingly fast.<\/p>\n\n\n\n<p>If you\u2019re interested in customizing your tmux experience I recommend that you read my&nbsp;<a href=\"https:\/\/hamvocke.com\/blog\/a-guide-to-customizing-your-tmux-conf\/\">Guide to Customizing your tmux.conf<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"footnote-label\">Footnotes<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>If you\u2019re coming from a Linux background you\u2019re probably familiar with the concept of window managers like\u00a0<a href=\"http:\/\/openbox.org\/wiki\/Main_Page\">Openbox<\/a>,\u00a0<a href=\"http:\/\/i3wm.org\/\">i3<\/a>\u00a0or\u00a0<a href=\"http:\/\/awesome.naquadah.org\/wiki\/FAQ\">awesome<\/a>\u00a0<a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fnref-1\">\u21a9<\/a><\/li>\n\n\n\n<li>tmux is based on a client-server architecture. While this is rarely relevant, it\u2019s helpful to understand how stuff works. The tmux server keeps track of all the running sessions. You will only ever work with the tmux client and create new sessions or connect to existing ones.\u00a0<a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fnref-2\">\u21a9<\/a><\/li>\n\n\n\n<li>there could also be\u00a0<code>M-<\/code>\u00a0which is the same only for the\u00a0<code>Meta<\/code>\u00a0key (i.e.\u00a0<code>Alt<\/code>\u00a0on most keyboards).\u00a0<a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fnref-3\">\u21a9<\/a><\/li>\n\n\n\n<li>This is an interesting one. Yes, you need\u00a0<code>Ctrl<\/code>\u00a0in your prefix and also your command key. I find it easiest to only release the\u00a0<code>b<\/code>\u00a0key after the prefix key and keep\u00a0<code>Ctrl<\/code>\u00a0pressed all the time while I hit the arrow key repeatedly. Experiment a little bit to get a feeling for it.\u00a0<a href=\"https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux\/#user-content-fnref-4\">\u21a9<\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/tmuxcheatsheet.com\">Tmux Cheat Sheet &amp; Quick Reference<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/hamvocke.com\/blog\/a-quick-and-easy-guide-to-tmux I love working with the command line. I think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-tutotial","tag-tmux"],"_links":{"self":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":1,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}