<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tmux on Chris Rants at Clouds</title>
    <link>https://chris.funderburg.me/tags/tmux/</link>
    <description>Recent content in Tmux on Chris Rants at Clouds</description>
    <image>
      <title>Chris Rants at Clouds</title>
      <url>https://chris.funderburg.me/me.jpg</url>
      <link>https://chris.funderburg.me/me.jpg</link>
    </image>
    <generator>Hugo -- 0.160.1</generator>
    <language>en-gb</language>
    <lastBuildDate>Tue, 06 Aug 2024 16:10:20 +0100</lastBuildDate>
    <atom:link href="https://chris.funderburg.me/tags/tmux/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>06/Aug/2024 - Tmux: Your Terminal&#39;s Superpower</title>
      <link>https://chris.funderburg.me/posts/2024/08/06/tmux-intro/</link>
      <pubDate>Tue, 06 Aug 2024 16:10:20 +0100</pubDate>
      <guid>https://chris.funderburg.me/posts/2024/08/06/tmux-intro/</guid>
      <description>An introduction to the linux/unix/Mac tool called &amp;ldquo;tmux&amp;rdquo;</description>
      <content:encoded><![CDATA[<p>I&rsquo;ve been a Linux admin since the late 90s, and for many many years I&rsquo;ve heard the
command &ldquo;tmux&rdquo; mentioned.  I knew it had <em>something</em> to do with terminals, and
that it might be something like <code>screen</code>, but I never really paid any attention.
But a few days ago it was mentioned in an article and I thought, hey - I should
check it out.  So I did!</p>
<h2 id="firstly-what-is-tmux">Firstly, what is tmux?</h2>
<p>Tmux, short for terminal multiplexer, is a powerful tool that allows you to
manage multiple terminal sessions within a single terminal window. It&rsquo;s like
having multiple desktops for your command line, enabling you to switch between
different tasks or projects without losing your work.</p>
<p>Compared with <code>screen</code>, tmux is generally considered to have an easier to use
status bar, better naming in the individual sessions, and  you can easily set up
different text/styles for current window, windows with activity, etc. and you
can put things on the left and right of the status bar, including shell commands
that can be run at a specified interval (default 15s).</p>
<h2 id="why-use-such-a-thing">Why use such a thing?</h2>
<ul>
<li>Efficiency: You can run multiple commands or applications simultaneously in
different panes within a single terminal window.</li>
<li>Persistence: Detach from a running session and reattach later, even after
closing your terminal or losing your connection. Imagine having to run a long
database command, but being connected by a flaky signal.</li>
<li>Organisation: Manage multiple projects or tasks in separate windows or sessions.</li>
<li>Screen Sharing: Share specific terminal sessions with others.</li>
</ul>
<h2 id="installation">Installation</h2>
<p>Tmux is often pre-installed on most Linux distributions and macOS. To check if
it&rsquo;s installed, open your terminal and type:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux -V
</span></span></code></pre></div><p>If installed, that should return the version.  If it&rsquo;s not installed, you can
usually install it using your package manager:</p>
<h3 id="debian--ubuntu">Debian / Ubuntu</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">sudo apt install tmux
</span></span></code></pre></div><h3 id="fedora--redhat--centos">Fedora / RedHat / CentOS</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">sudo yum install tmux
</span></span></code></pre></div><h3 id="macos-using-homebrew">MacOS (using homebrew)</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">brew install tmux
</span></span></code></pre></div><h2 id="starting-and-stopping">Starting and Stopping</h2>
<p>tmux is based around sessions. To start a new session in tmux, simply type
<code>tmux new-session</code> in your terminal. Once you are in tmux, it will mostly look
like any other terminal session, however there will be a green bar at the
bottom.  This is the default view after starting a new session:</p>
<p><img alt="starting" loading="lazy" src="/posts/2024/08/06/tmux-new.png" title="Default view after starting new session"></p>
<p>To get out, you can type <code>exit</code> if you’re in a single pane, and you’ll return
from where you were.</p>
<p>An important note is that <code>exit</code> is not the only way to get out, and usually not
the best way. For that you have <code>detach</code>. However before you get to that, you
first have to cover the prefix&hellip;</p>
<h3 id="using-prefix">Using Prefix</h3>
<p>All commands in tmux require the prefix shortcut, which by default is <code>ctrl+b</code>.
You&rsquo;ll be using the prefix a lot, so best to just commit it to memory. After
entering <code>ctrl+b</code> you can then run a tmux command, or type <code>:</code> to get a tmux
prompt.  “<strong>ctrl+b :</strong>” to get tmux command prompt:</p>
<p><img alt="commands" loading="lazy" src="/posts/2024/08/06/tmux-command.png" title="“ctrl+b :” to get tmux command prompt"></p>
<p>When entering the prefix, tmux itself will not change in any way. So, if you
enter <code>ctrl+b</code> and nothing changes, that does not necessarily mean you typed it
wrong.</p>
<h3 id="attach-detach--kill">Attach, Detach &amp; Kill</h3>
<p>As mentioned, a better way to get out of a session without exiting out of
everything is to <code>detach</code> the session. To do this, you first enter the prefix
command and then the detach shortcut of <code>d</code>:</p>
<pre tabindex="0"><code>ctrl+b d
</code></pre><p>This will detach the current session and return you to your normal shell.</p>
<p>However, just because you’re out doesn’t mean your session is closed. The
detached session is still running and available, allowing you to pick up where
you left off. To check what sessions are active you can run:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux ls
</span></span></code></pre></div><p>The tmux sessions will each have a number associated with them on the left-hand
side (zero indexed as nature intended). This number can be used to attach and
get back into this same session. For example, for session number 3 you would
type:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux attach-session -t <span class="m">3</span>
</span></span></code></pre></div><p>or you can go to the last created session with:</p>
<pre tabindex="0"><code>tmux a
</code></pre><h3 id="naming-sessions">Naming Sessions</h3>
<p>Now you <em>could</em> just rely the session numbers, but it would make your life much
easier if you give your sessions names based on their intended use.</p>
<p>To start a new session with a specific name you can just do this:</p>
<pre tabindex="0"><code>tmux new -s [name of session]
</code></pre><p>With named sessions in place, now when you do <code>tmux ls</code> you see the session name
instead. Likewise, you can then attach a session by using the name:</p>
<pre tabindex="0"><code>tmux a -t [name of session]
</code></pre><p>Note that you substituted <code>a</code> for <code>attach-session</code> to help save on keystrokes.</p>
<h2 id="managing-panes">Managing Panes</h2>
<p>In a GUI desktop environment, you have windows. In tmux, you have panes. Like
windows in a GUI, these panes allow you to interact with multiple applications
and similarly can be opened, closed, resized and moved.</p>
<p>Unlike a standard GUI desktop, these panes are tiled, and are primarily managed
by tmux shortcuts as opposed to a mouse (<em>although mouse functionality can be
added</em>). To create a new pane you simply split the screen horizontally or
vertically.</p>
<p>To split a pane horizontally:</p>
<pre tabindex="0"><code>ctrl+b &#34;
</code></pre><p>To split pane vertically:</p>
<pre tabindex="0"><code>ctrl+b %
</code></pre><p><img alt="split" loading="lazy" src="/posts/2024/08/06/tmux-split.png" title="tmux split horizontally, with lower pane split vertically"></p>
<p>You can split panes further using the same methodology. For example, in the
screenshot above, the screen was first split horizontally using <code>ctrl+b &quot;</code> and
then split vertically within the lower pane using <code>ctrl+b %</code>.</p>
<p>To move from pane to pane, simply use the prefix followed by the arrow key:</p>
<pre tabindex="0"><code>ctrl+b [arrow key]
</code></pre><h3 id="resizing-panes">Resizing Panes</h3>
<p>Let&rsquo;s say you need a little extra space in one of our panes, and want
to expand the pane down a few lines. For this, you will go into the tmux prompt:</p>
<pre tabindex="0"><code>ctrl+b :
</code></pre><p>From there you can type resize-pane followed by a direction flag: <code>-U</code> for up,
<code>-D</code> for down <code>-L</code> for left and <code>-R</code> for right. The last part is the number of
lines to move it over by.</p>
<p>As an example, if you are in the top pane and want to expand it down by 2 lines,
you would do the following:</p>
<pre tabindex="0"><code>ctrl+b :
resize-pane -D 2
</code></pre><p><img alt="split" loading="lazy" src="/posts/2024/08/06/tmux-resize.png" title="resize-pane -D 2 being entered"></p>
<h2 id="going-further">Going Further</h2>
<p>By mastering tmux, you&rsquo;ll significantly improve your command-line efficiency and
productivity. Start experimenting with these commands and discover how tmux can
transform your terminal experience.</p>
<p>The possibilities here are just the tip of the iceberg. If you are ready to go
even further down the rabbit-hole, the below links should help fill the gaps:</p>
<ul>
<li><a href="https://gist.github.com/MohamedAlaa/2961058" target="_blank" >Cheatsheet</a> by <a href="https://github.com/MohamedAlaa" target="_blank" >MohamedAlaa</a></li>
<li><a href="https://github.com/jimeh/tmux-themepack" target="_blank" >tmux-themepack</a> by <a href="https://github.com/jimeh" target="_blank" >Jim Myhrberg</a></li>
</ul>
<p>And my personal fave:</p>
<ul>
<li><a href="https://github.com/gpakosz/.tmux" target="_blank" >Oh My Tmux!</a> by <a href="https://github.com/gpakosz" target="_blank" >Gregory Pakosz</a></li>
</ul>
<p>I&rsquo;m personally quite interested in deep diving into an alleged integration with
iTerm2 on my Macbook, and understanding what plugins can do with it.  I&rsquo;ll
certainly be trying this instead when I would have previously reached for
<code>screen</code>.</p>
<h2 id="a-cheatsheet">A Cheatsheet</h2>
<h3 id="start-a-new-named-session">Start a new named session:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux new -s <span class="o">[</span>session name<span class="o">]</span>
</span></span></code></pre></div><h3 id="detach-from-a-session">Detach from a session:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">ctrl+b d
</span></span></code></pre></div><h3 id="list-all-sessions">List all sessions:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux ls
</span></span></code></pre></div><h3 id="attach-to-a-named-session">Attach to a named session:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux a -t <span class="o">[</span>name of session<span class="o">]</span>
</span></span></code></pre></div><h3 id="kill-a-named-session">Kill a named session:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux kill-session -t <span class="o">[</span>name of session<span class="o">]</span>
</span></span></code></pre></div><h3 id="split-panes-horizontally">Split panes horizontally:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">ctrl+b <span class="s2">&#34;
</span></span></span></code></pre></div><h3 id="split-panes-vertically">Split panes vertically:</h3>
<pre tabindex="0"><code>ctrl+b %
</code></pre><h3 id="kill-the-current-pane">Kill the current pane:</h3>
<pre tabindex="0"><code>ctrl+b x
</code></pre><h3 id="move-to-another-pane">Move to another pane:</h3>
<pre tabindex="0"><code>ctrl+b [arrow key]
</code></pre><h3 id="cycle-through-panes">Cycle through panes:</h3>
<pre tabindex="0"><code>ctrl+b o
</code></pre><h3 id="cycle-just-between-previous-and-current-pane">Cycle just between previous and current pane:</h3>
<pre tabindex="0"><code>ctrl+b ;
</code></pre><h3 id="kill-tmux-server-along-with-all-sessions">Kill tmux server, along with all sessions:</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tmux kill-server
</span></span></code></pre></div>]]></content:encoded>
    </item>
  </channel>
</rss>
