Get the Previous Expression Value in IEx

<p>When using Elixir, I&rsquo;ve long missed the special <code>_</code> helper from <a href="https://en.wikipedia.org/wiki/Interactive_Ruby_Shell">irb</a> that returns the result of the previous expression. Sometimes I actually type it out of pure habit.</p> <p>Little did I know that Elixir has the same thing, except better.</p> <p>IEx.Helpers has a function <code>v(n \\ -1)</code> that returns the value of the nth expression in the session history. So <code>v</code> alone returns the previous expression and <code>v(-2)</code> returns the one before it.</p> <div class="highlight"><pre class="highlight elixir"><code><span class="n">iex</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">&gt;</span> <span class="s2">"abc"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">&gt;</span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">v</span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="s2">"abc"</span> </code></pre></div>