Search code examples
gitaliasgit-bashmingw-w64less-unix

Git for Windows Bash shell 'less' command displays garbage while displaying formatted text from Git log call


I've installed Git for Windows and am generally pleased with the feature set in the Bash shell it provides. I'm having trouble with an alias that works fine everywhere else I've tried it.

The alias:

alias gl='git log --graph --format='\''%Cred%h%Creset %s    %C(yellow)%an | %C(cyan)%ad%Creset %C(green bold)%d%Creset'\''' | less

In Cygwin, the MacOS bash terminal, and of course in a Linux shell, the alias produces nice results.

alias executed under Cygwin

In GFW's Bash shell, the output is pretty mangled:

alias under Git For Windows Bash

If I run the command directly, without an alias, it works correctly. So I know it's not that GFW's Bash shell doesn't know how to parse the command as I've written it.

My question: How do I get this alias to work correctly under Git For Windows?

I'm open to using an alternative to an alias. If a linux function or a true Git alias will work better I'm comfortable with those alternatives. It would be nice to figure out why this is happening, though.

UPDATE/DISCOVERY The problem doesn't appear to be with interpreting the alias at all. The output displays correctly if I remove the pipe to 'less'. So it looks like the 'less' command as implemented in the Git Bash Shell interprets the escape sequences differently (literally, I'd guess) than other implementations. As a result, no amount of wrapping the call in functions or such will address the core issue.


Solution

  • As the comments stated, the mangled stuff is color escape sequences. To get less to display them properly, use less -R.

    From the man page,

    -R or --RAW-CONTROL-CHARS Like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases. ANSI "color" escape sequences are sequences of the form:

    ESC [ ... m
    

    where the "..." is zero or more color specification characters For the purpose of keeping track of screen appearance, ANSI color escape sequences are assumed to not move the cursor. You can make less think that characters other than "m" can end ANSI color escape sequences by setting the environment variable LESSANSIENDCHARS to the list of characters which can end a color escape sequence. And you can make less think that characters other than the standard ones may appear between the ESC and the m by setting the environment variable LESSANSIMIDCHARS to the list of characters which can appear.