Cmder: create alias with more than one command

Created on 14 Aug 2015  路  30Comments  路  Source: cmderdev/cmder

I'm trying to create an alias like this:

gclean=gclean=git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

The problem is that it only executes the first command. Any thoughts?

馃憖 Awaiting Response

Most helpful comment

@AbdelrahmanHafez Whitespace is not relevant here -- with doskey macros you need to use $b as the pipe character, otherwise you are just piping the output of 'create an alias for ping -t 8.8.8.8' -- which is nothing.

That said, your example does not work for me either. I just tried a few variations piping output to awk (e.g. even a simple echo) and can't get it to work in an alias. This may be a case where writing a simple bat file and putting it in your path is the way to go.

All 30 comments

Use && instead of |.
You also have gclean= twice. That's probably not intended either.

ops, double gclean was wrong :)
Thanks!

Sorry, but this doesn't work. I need to pipe one command output to the next command, not simply run all of them.

Oops, my bad.. Misread the question. Not entirely sure how to do what you want, but http://ss64.com/nt/syntax-redirection.html might have what you need.

that's exactly the point. This command runs ok on cmder (git branch --merged | grep -v "\*" | xargs -n 1 git branch -d), but if I put this as an alias, it runs just the first command

$T is the command seperator, allowing you to string several commands together into one alias.
For more information, read DOSKEY/?
(https://github.com/cmderdev/cmder/blob/master/bin/alias.bat)

Thanks @jolet

@luisrudge was there a solution to this in the end? As far as I can tell, none of the methods mentioned here actually allow piped commands in aliases.

Nope. $T doesn't work for me.

this works if I type into cmder directly: git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d

but both of these aliases are failing:
gclean=git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d
gclean=git branch --merged $t grep -v "\*" $t grep -v master $t grep -v dev $t xargs -n 1 git branch -d

Nope. $T doesn't work for me.

that's because $t is a doskey argument. conemu/cmder's alias doesn't really use doskey at all. so you would have to add it to the init task or put it in a batch file somewhere in your path.

@viletouch Cmder cmd sessions do indeed use doskey. $T is definitely valid I have several aliases that use it.

@luisrudge your question is misphrased. If you were wanting to concatenate commands $t is the answer. $t is doskey for &.

The question should be how to create alias that uses pipes. Not sure I have ever tried pipes in a doskey alias but you could try escaping the pipes with either a '" or a '^'. Most likely alias.bat can't do this and you would need to manually edit the file.

The question should be how to create alias that uses pipes. Not sure I have ever tried pipes in a doskey

doskey ccl="cd|clip" works fine (try it!)

as well as alias ccl="dir|clip" in conemu/cmder. except it doesn't support doskey macros ($t)

in short, wether it uses doskey or not to create the alias, macros (or concatenated commands) doesn't work from the environment page ...yet
eg. alias cd=cd $1$tdir

Yes, I can confirm that piping does in fact work in Cmder aliases. The problem I was that I'd put spaces either side of the pipe character. alias history=tac %cmder_root%\config\.history|less works, whereas alias history=tac %cmder_root%\config\.history | less doesn't.

However, the alias has to be created manually using a text editor, otherwise the output of the alias command is piped.

@viletouch Then why does this work for me:

C:\Users\dgames.DTG\cmder
位 alias test
test=dir $T Dir c:\

C:\Users\dgames.DTG\cmder
位 test
 Volume in drive C is Windows
 Volume Serial Number is 2C73-1044

 Directory of C:\Users\dgames.DTG\cmderdev

03/20/2016  07:59 AM    <DIR>          .
03/20/2016  07:59 AM    <DIR>          ..
11/07/2015  11:01 AM                68 .gitattributes
03/06/2016  06:14 PM               226 .gitignore
03/02/2016  07:21 PM             1,285 appveyor.yml
03/06/2016  06:20 PM    <DIR>          bin
01/04/2016  04:59 PM    <DIR>          build
03/06/2016  06:14 PM               424 Cmder.bat
03/20/2016  08:28 AM    <DIR>          config
11/07/2015  11:01 AM             1,822 CONTRIBUTING.md
11/07/2015  11:01 AM    <DIR>          icons
11/07/2015  11:31 AM    <DIR>          launcher
03/02/2016  07:45 PM               424 MyCmder.cmd
03/02/2016  07:30 PM               302 packignore
03/20/2016  07:58 AM            10,207 README.md
03/06/2016  03:52 PM    <DIR>          scripts
03/20/2016  08:54 AM    <DIR>          vendor
01/04/2016  04:58 PM                 0 Version v1.2.9
               9 File(s)         14,758 bytes
               9 Dir(s)  21,213,876,224 bytes free

 Volume in drive C is Windows
 Volume Serial Number is 2C73-1044

 Directory of c:\

12/05/2015  09:36 AM    <SYMLINK>      .aliases [C:\Users\dgames.DTG\.yadr4win\aliases.doskey]
12/05/2015  09:36 AM    <SYMLINK>      .aliases.sh [C:\Users\dgames.DTG\.yadr4win\aliases.sh]
12/05/2015  09:30 AM    <SYMLINK>      .aliases.sh.yadr4win [C:\Users\dgames.DTG\.yadr4win\aliases.sh]
12/05/2015  09:30 AM    <SYMLINK>      .aliases.yadr4win [C:\Users\dgames.DTG\.yadr4win\aliases.doskey]
08/07/2015  11:16 AM    <DIR>          Firmware
11/25/2015  08:51 AM    <DIR>          git-sdk-64
12/18/2015  09:23 AM    <DIR>          Intel
11/01/2015  09:20 AM             4,256 log.txt
10/30/2015  02:24 AM    <DIR>          PerfLogs
01/04/2016  05:38 PM    <DIR>          Program Files
02/01/2016  08:36 PM    <DIR>          Program Files (x86)
11/16/2015  07:26 PM    <DIR>          recovered
01/04/2016  05:33 PM    <DIR>          tools
12/18/2015  09:26 AM    <DIR>          Users
03/07/2016  09:04 AM    <DIR>          Windows
01/23/2016  12:02 PM    <DIR>          Windows.old
               5 File(s)          4,256 bytes
              11 Dir(s)  21,213,876,224 bytes free

This also works:

C:\Users\dgames.DTG\cmderdev (better_aliases)
位 alias test
test=dir $1 $T Dir c:\

Also if you noticed in my original response I said alias.bat could not handle pipes, "Most likely alias.bat can't do this and you would need to manually edit the file."

@daxgames that's... intriguing!

the sintax it accepts is all over the place and more importantly fails silently

it won't accept spaces around the = or | symbols...but it requires spaces around the $t variable (!) while every other variable requires NOT to use spaces other than what the command it is used in requires

thanks dax ,this means a lot of articles and docs need to be updated (no, seriously, try googling that)

This doesn't work:
gclean=git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d
gclean=git branch --merged|grep -v "\*"|grep -v master|grep -v dev|xargs -n 1 git branch -d

Output of 'alias gclean'?

fatal: branch name required

oh, no. sorry. it actually works! I think this message shows only when I don't have any merged branch. Sorry!

This works:

gclean=git branch --merged|grep -v "\*"|grep -v master|grep -v dev|xargs -n 1 git branch -d

Not what I asked for. Type 'alias gclean[enter]'

On March 21, 2016 3:35:46 PM Lu铆s Rudge [email protected] wrote:

fatal: branch name required


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
https://github.com/cmderdev/cmder/issues/595#issuecomment-199465875

位 alias gclean
Insufficient parameters.
Usage:
        alias name=full command
        $* allows the alias to assume all the parameters of the supplied command.
        $1-$9 Allows you to seperate parameter by number, much like %1 in batch.
        $T is the command seperator, allowing you to string several commands together into one alias.
        For more information, read DOSKEY/?

Oh OK. Glad it's working for you.

Try the latest development branch of Cmder. 'alias gclean[enter]' should print the contents of the alias.

Just an fyi at this point.

Removing spaces before and after the pipe doesn't solve the problem for me.
@glucas at #632 suggested using $b instead of pipes.
p=ping -t 8.8.8.8|awk '{print $5}'
p=ping -t 8.8.8.8 | awk '{print $5}'
While both of these work normally if manually typed into the command line, they only run the first command when used as aliases.
p=ping -t 8.8.8.8 $b awk '{print $5}'
Gives me Bad parameter awk.

@AbdelrahmanHafez Whitespace is not relevant here -- with doskey macros you need to use $b as the pipe character, otherwise you are just piping the output of 'create an alias for ping -t 8.8.8.8' -- which is nothing.

That said, your example does not work for me either. I just tried a few variations piping output to awk (e.g. even a simple echo) and can't get it to work in an alias. This may be a case where writing a simple bat file and putting it in your path is the way to go.

TL;DR: awk/gawk not working in aliases is a ConEmu issue, not Cmder issue.

Just a bump to this issue, I can't for the life of me get awk/gawk to work when I add it to the user-aliases.cmd file. It works fine in the cmder terminal, but as an alias it always fails.

awktest=echo first second | awk '{print $1}'
awktest=echo first second $b awk '{print $1}'
awktest=echo first second | gawk '{print $1}'

I created an awktest.txt file with
Physics 80 Math 90 Biology 87 English 85 History 89

and added an alias:
testawk=awk '{print $1}' awktest.txt

My goal was just to create an alias for pruning git branches:
git checkout master && git remote prune origin && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
but I absolutely cannot get awk to work in a cmder alias. I tried with cmder 1.3.5 with ConEmu 180206 preview, and with ConEmu 180626 (64bit) Preview, but got the same results. Just for fun I installed the latest cmder mini 1.3.6.678 with ConEmu 180528 preview and got the same results :(

Conclusion: OK I just tried the same stuff in ConEmu, 180626 and awk doesn't work in those aliases at all. So, not a Cmder issue.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs. Thank you for your contribution.

This issue has been automatically closed due to it not having any activity since it was marked as stale. Thank you for your contribution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Joe1992w picture Joe1992w  路  3Comments

edgariscoding picture edgariscoding  路  3Comments

emesx picture emesx  路  3Comments

spooky picture spooky  路  3Comments

zsedcbm picture zsedcbm  路  3Comments