# Useful tip about !aliases

3 messages from 2008-07-16 to 2008-07-16. Participants: Kevin Ballard, Johannes Schindelin.
Thread: https://gitlist.dev/t/14474

## Kevin Ballard, 2008-07-16 00:46

Subject: Useful tip about !aliases
Message-ID: <3BD7F543-7CB4-48B6-8D2C-DEA2ADC6EF5F@sb.org>
URL: https://gitlist.dev/e/3BD7F543-7CB4-48B6-8D2C-DEA2ADC6EF5F%40sb.org

```
Here's something I discovered recently about !aliases that other folks  
might find useful. The canonical form for a shell alias is something  
like

   git config alias.foo '!echo bar'

where any args given to foo, as in `git foo blah` are passed along to  
the shell, so in this case `echo bar blah` would be invoked.

Something that I find very useful is the ability to interpolate  
arguments into the middle of a command. This doesn't seem possible at  
first glance, not without a helper script. But it certainly is  
possible, with the help of shell functions:

   git config alias.reverse '!foo () { args=''; while [[ -n "$*" ]];  
do args="$1 $args"; shift; done; echo $args; }; foo'

Now if you invoke `git foo one two three` you'll get the response  
"three two one".

Here's another example. This one I particularly like. I call it 'send- 
patches', because what it does is it takes a single hash and creates  
patches out of all commits since that hash, invokes send-mail on them,  
and deletes them. It's a rather quick way of sending off patches. And  
if you pass any extra arguments, they're given to git-send-email. The  
most useful part is it adjusts the patch prefix to contain the name of  
the repository itself, so your recipient knows exactly what your patch  
is for.

   git config --global alias.send-patches '!foo () { rev="$1"; shift;  
git send-email $(git format-patch -o .mbox --no-prefix --subject- 
prefix="$(printf "PATCH: %s" $(basename $(cd "$(git rev-parse --show- 
cdup)" && pwd)))" $rev) "$@"; rm -rf .mbox; }; foo'

-Kevin Ballard

-- 
Kevin Ballard
http://kevin.sb.org
kevin@sb.org
http://www.tildesoft.com

```

## Johannes Schindelin, 2008-07-16 00:50

Subject: Re: Useful tip about !aliases
Message-ID: <alpine.DEB.1.00.0807160250120.2841@eeepc-johanness>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0807160250120.2841%40eeepc-johanness
In-Reply-To: <3BD7F543-7CB4-48B6-8D2C-DEA2ADC6EF5F@sb.org>

```
Hi,

On Tue, 15 Jul 2008, Kevin Ballard wrote:

> Here's something I discovered recently about !aliases that other folks might
> find useful. The canonical form for a shell alias is something like
> 
>  git config alias.foo '!echo bar'
> 
> where any args given to foo, as in `git foo blah` are passed along to the
> shell, so in this case `echo bar blah` would be invoked.
> 
> Something that I find very useful is the ability to interpolate arguments into
> the middle of a command. This doesn't seem possible at first glance, not
> without a helper script. But it certainly is possible, with the help of shell
> functions:
> 
>  git config alias.reverse '!foo () { args=''; while [[ -n "$*" ]]; do args="$1
> $args"; shift; done; echo $args; }; foo'

On the gitwiki, you will find a more elegant solution using "sh -c".

Ciao,
Dscho

```

## Kevin Ballard, 2008-07-16 00:58

Subject: Re: Useful tip about !aliases
Message-ID: <EC8F3D14-CAD6-4337-B8AA-FD741E6313CE@sb.org>
URL: https://gitlist.dev/e/EC8F3D14-CAD6-4337-B8AA-FD741E6313CE%40sb.org
In-Reply-To: <alpine.DEB.1.00.0807160250120.2841@eeepc-johanness>

```
On Jul 15, 2008, at 5:50 PM, Johannes Schindelin wrote:

> Hi,
>
> On Tue, 15 Jul 2008, Kevin Ballard wrote:
>
>> Here's something I discovered recently about !aliases that other  
>> folks might
>> find useful. The canonical form for a shell alias is something like
>>
>> git config alias.foo '!echo bar'
>>
>> where any args given to foo, as in `git foo blah` are passed along  
>> to the
>> shell, so in this case `echo bar blah` would be invoked.
>>
>> Something that I find very useful is the ability to interpolate  
>> arguments into
>> the middle of a command. This doesn't seem possible at first  
>> glance, not
>> without a helper script. But it certainly is possible, with the  
>> help of shell
>> functions:
>>
>> git config alias.reverse '!foo () { args=''; while [[ -n "$*" ]];  
>> do args="$1
>> $args"; shift; done; echo $args; }; foo'
>
> On the gitwiki, you will find a more elegant solution using "sh -c".

I'd forgotten you can do that, but I disagree that it's more elegant.  
It's a bit shorter, certainly, but it also invokes another process.  
Using a shell function doesn't.

-Kevin Ballard

-- 
Kevin Ballard
http://kevin.sb.org
kevin@sb.org
http://www.tildesoft.com

```
