#!/bin/bash

,

…cheat-sheet - I keep forgetting one or the other of the useful expansions and substitutions bash has to offer…
<b># expansions</b>
!$                 # expands to the end of the last command
!*                 # expands to all arguments of last command
!!                 # expands to last command
!<i>number</i>            # expands to command <i>number</i> in `history`
!-<i>number</i>           # expands to command <i>number</i> commands ago
!<i>abc</i>               # expands to the last command starting with <i>abc</i><br /><br />

<b># modifiers</b>
:p                 # only <b>p</b>rints output
:h                 # only <b>h</b>ead, removing trailing component(s)
:t                 # only <b>t</b>ail, removing leading component(s)
:r                 # <b>r</b>emoves trailing suffix of the form <i>.xxx</i>
:e                 # removes all but trailing suffix
:<i>number</i>            # <i>number</i> of arguments of last command - therefore
!:<i>number</i>*          # is from <i>number</i> to end of arguments of last command… and
!*                 # is a shortcut for <i>!:1*</i><br /><br />

<b># substitutions</b>
:s/<i>wrong</i>/<i>right</i>/    # <b>s</b>ubstitution of <i>wrong</i> with <i>right</i> in the command
:g                 # <b>g</b>lobal (as a modifier to :s) - as in <i>:gs/<i>wrong</i>/<i>right</i>/
&                  # repeats previous substitution ( :s/one/&.more/ )

^<i>wrong</i>^<i>right</i>       # replaces <i>wrong</i> with <i>right</i> in the previous command, and runs it</pre>

<h4 id="note">Additional note</h4>
<p><kbd>^R</kbd> incrementally searches your command history backwards - hit it again to get to the next occurrence. I badly miss the ability to search your buffer in xterm - rxvt-unicode offers this neat feature.</p>

sine qua nonopera and x.org 7

Comments

towolf Sunday, June 11, 2006 11:55:37 PM

# alternate mappings for "page up" and "page down" to search the history
"\e[5~": history-search-backward
"\e[6~": history-search-forward

Did you know about this one in inputrc? It’s ^R and !abc combined. Neat.

Write a comment

New comments have been disabled for this post.

June 2013
S M T W T F S
May 2013July 2013
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29