cmderdev / cmder

Lovely console emulator package for Windows
https://cmder.app
MIT License
25.86k stars 2.03k forks source link

Support comments in /config/aliases #286

Closed rmorrin closed 7 years ago

rmorrin commented 9 years ago

By default, doskey macro files don't support comments. There is however, a workaround that may be worth investigating: http://ben.versionzero.org/wiki/Doskey_Macros.

While certainly not essential, it would be useful for those of us that have a large number of aliases defined to be able to organize the chaos with some comments!

cm3 commented 8 years ago

:: worked for me in /config/aliases also.

e.g.

::ls=ls --show-control-chars -F --color $* 

isn't it what you mentioned?

cf. :: is a hack based on flag grammar : and I thought some cmder bat files used that technique.

rmorrin commented 8 years ago

@cm3 This works well for temporarily disabling aliases, as you mentioned.

I was looking to use comments for organisational purposes:

:: convenience
ls=ls --color $*
lsa=ls -a --color $*
pwd=cd
clear=cls

:: git shortcuts
gs=git status $*
gd=git diff $*
gc=git commit $*
gca=git commit -a $*

...

Unfortunately, this shows an Invalid macro definition. message when Cmder starts.

cm3 commented 8 years ago

Your code (before ...) worked for me. No Invalid macro definition. error. ('ls' is not recognized as an internal or external command operable program or batch file. but that is another problem.)

I'm using code below and this works fine.

:: Linux compatible

clear=cls
pwd=cd
ls=dir /B $*
mv=move $*
unalias=alias /d $1

:: http://fossil-scm.org/

f=fossil $*

:: Not nano but https://bitbucket.org/wantora/greenpad

nano="C:\Users\User\bin\greenpad\GreenPad.exe" $*

:::: trash box
:: ls=ls --show-control-chars -F --color $*
:: gl=git log --oneline --all --graph --decorate  $*
:: e.=explorer .
:: history=cat %CMDER_ROOT%\config\.history

I suspect that :

nikolas6 commented 7 years ago

I had the same problem with cmder_mini v1.3.1 (Windows 7). Using :: in user-aliases.cmd shows Invalid macro definition.

I use ;= rem This is a comment instead, just like the default settings in user-aliases.cmd. With that it does not show the invalid message anymore.

cm3 commented 7 years ago

Seeing user-aliases.cmd in your comment, I realize that init.bat has been updated (https://github.com/cmderdev/cmder/commits/master/vendor/init.bat) and mechanism of user alias has been changed. That could be the problem in the last conversation with rmorrin. When cmder is updated, old init.bat is kept, in other words, I used old mechanism.

Now, I updated init.bat and your (@nikolas6) TIPS worked :) btw, why does it works? ; and = are separators in batch program, and ...?

rmorrin commented 7 years ago

Looks like this issue is now resolved with the new user-aliases.cmd.

As @nikolas6 suggested, I can use ;= rem <Comment> to organise the file. 👍

nikolas6 commented 7 years ago

@cm3 I don't know what the ;= is for, and I couldn't find any information about it. The new user-aliases.cmd even has something like ;= Add aliases below here, and it does not throw out any error message.

cm3 commented 7 years ago

@nikolas6 now I found the answer. Explanation below is just for your information.

user-aliases.cmd is read as command script first, and then, is read as Doskey macro. This part implement that trick.

;= @echo off
;= rem Call DOSKEY and use this file as the macrofile
;= %SystemRoot%\system32\doskey /listsize=1000 /macrofile=%0%
;= rem In batch mode, jump to the end of the file
;= goto:eof

A row begin with :: is interpreted as (pseudo-)comment in command script, but not in Doskey macro. Therefore, Invalid macro definition. error occurs. ;= is ignored in command script, and a row begin with ;= is interpreted as (pseudo-)comment Doskey macro. ;= itself is just ignored in command script, and trailing commands are not ignored. Therefore, before goto:eof, command rem is needed for comment row. But after goto:eof, those rows are ignored in the first step ("read as command script first"), so there is no rem in ;= Add aliases below here.

Why ;= function as (pseudo-)comment? This is alias assignment about ;. When you type ; in cmder, you'll see 'Add' is not recognized as an internal or external command, operable program or batch file. because ; is the alias of Add aliases below here now. For avoiding this, you can add an row with just ;=, no trailing comment. This row cancels the alias assignment.

References:

nikolas6 commented 7 years ago

@cm3 make sense now. thanks for the detail explanation.