Runs Previous Command with Replace

By  on  

For some reason most of my typos happen within iTerm, which is kind of funny when you consider most commands are super short.  gut push doesn't do what I want, nor does hg ammend or any other command spelled wrong.  Then you have to either retype the whole thing or press the up key, place the cursor in the right spot, then update the bad text.  Blah.

I recently learned a cool trick for executing the previous command but with a text replacement shortcut:

# `gut push` -- Oooops!
^gut^git   # Replaces "gut" with "git" and executes previous command!

# `hg ammend` -- Oooops!
^ammend^amend

No need to retype and no need to fix the text within the entire command.  Neat way to save time and get things done!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Hot Effect: MooTools Drag Opacity

    As you should already know, the best visual features of a website are usually held within the most subtle of details. One simple trick that usually makes a big different is the use of opacity and fading. Another awesome MooTools functionality is...

  • By
    Introducing MooTools ScrollSpy

    I've been excited to release this plugin for a long time. MooTools ScrollSpy is a unique but simple MooTools plugin that listens to page scrolling and fires events based on where the user has scrolled to in the page. Now you can fire specific...

Discussion

  1. Really cool trick, David! Hadn’t come across this one yet. I’m the curious type so I just had to know what this is called, and any other details… It looks like after a little research that this is a Bash feature, and it’s called “quick substitution”. Its documented under the “Event Designators” section of Bash documentation:

    http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators

    The documentation specifies the usage as:

    ^string1^string2^

    But I just tried it out myself and it looks like it works just fine without the trailing caret character. By the way, it’s probably worth noting that this will only replace the first instance of “string1” in the command, so if you misspelled something twice in the command (seems unlikely, I know) you would have to use:

    !!:gs/string1/string2/
  2. caleb

    If you use zsh, you can enable auto-correction, so if you type

    gut commit -a
    

    then it will say

    zsh: correct 'gut' to 'git' [nyae]? 
    
  3. ishan

    This is a great tip.

    P.S. Lately, most of my php/css/js related google searches have been showing your site near the top. I have picked up a lot of new things from your site. Whatever you are doing with SEO and content, it is amazing.

    All the best.
    PPS. I hope one day your have email newsletters.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!