Your Title - history2018-03-11T23:53:11-03:00http://example.com/tag/history.htmlAuthor HereBash History: Re-use and Edit Previous Commands - Examples and Reference2014-02-09T06:48:07-02:00http://example.com/entries/bash-history-re-use-and-edit-previous-commands-examples-and-reference<p>These are some little tips and hacks you can use to enhance your productivity while working on <strong>bash</strong>.</p>
<p>It might seem a small contribution to justify learning yet another tool but, when put together, all these things will probably help you make more effective use of your time.</p>
<h3 id="search-for-a-command-containing-a-string">Search for a command containing a string</h3>
<p>hit <code>Ctrl</code> + <code>r</code>:</p>
<div class="highlight"><pre><code class="language-" data-lang="">(reverse-i-search)
</code></pre></div>
<p>Then start typing. For example, if you type <code>cd</code>, you would probably see something like this (<code>cd /etc/apache2</code> was the last command I ran using <code>cd</code>, that&#39;s why it show up first)</p>
<div class="highlight"><pre><code class="language-" data-lang="">(reverse-i-search) cd: cd /etc/apache2/
</code></pre></div>
<p>To see the next matches, hit <code>Ctrl</code>+<code>r</code> again as many times as you want. When you find the command you want, hit <code>tab</code> so it&#39;ll be available for you to edit or run it.</p>
<h3 id="run-your-last-command-again-using-sudo">Run your last command again using sudo</h3>
<p>If you get a <strong>permission denied</strong>-like error after running a command and you have access to <code>sudo</code>, do this to run the last command using super user privileges:</p>
<div class="highlight"><pre><code class="language-" data-lang="">$ sudo !!
</code></pre></div>
<h3 id="run-your-last-command-again-replacing-strings">Run your last command again replacing strings</h3>
<p>(example using <code>svn</code> commands)</p>
<div class="highlight"><pre><code class="language-" data-lang="">$ svn diff path/to/my/file.txt
</code></pre></div>
<p>After seeing the diff result, say you want to commit that very same file. Just replace <code>&quot;diff&quot;</code> with <code>&quot;commit&quot;</code> in the last command:</p>
<div class="highlight"><pre><code class="language-" data-lang="">$ !!:gs/diff/commit/
svn commit path/to/my/file.txt
</code></pre></div>
<h3 id="references">References</h3>
<ul>
<li><a href="http://www.dzone.com/snippets/bash-re-run-previous-command">Snippet on running substitution on your last command</a></li>
</ul>