Unix ANSI colors and Windows colors in the same terminal

My windows setup is weird. Well, to be honest windows itself is a weird OS, but I make it even weirder by using Cygwin as my primary shell over there. This is not a perfect solution. In fact it is downright lousy considering that Cygwin (as wonderful as it is) is just a hackish attempt to port entire unix stack onto windows which doesn’t actually make it a good windows shell. It just gives you a unix shell that works on the platform but mostly just exists apart and besides windows and for better or worse is kind of a second class citizen.

If you are primarily a Windows user and not a filthy OS nomad like me, then using Cygwin is probably counter-productive. I’d highly recommend making Powershell your primary shell instead. Why? Because it is a first class citizen, and the proper admin tool for the OS. There is a great article somewhere by one of the engineers who developed Powershell which I never bookmarked – but trust me it exists somewhere. It touched upon the genesis of the project and how it grew out of the desire to have Unix like environment on the Microsoft platform.

You see, Unix shell is great. Everyone loves that environment, including Microsoft engineers. They liked it so much they actually made a valiant attempt to port a good chunk of the standard POSIX toolkit to Windows and they bundled them piecemeal in several Windows Server Resource Kits over time. Unfortunately they quickly realized that standard Unix tools are mostly geared towards editing and processing text files, because Unix keeps most of it’s configuration in plain text format. Porting these tools to windows doesn’t help administrators much, because they don’t deal with text files most of the time, but with weird constructs like the Registry. So while having a full POSIX compatible stack would be great, admins didn’t necessarily need it to do their job. What they needed was a shell that would give you the flexibility and robustness of unix command line environments (ie. not cmd.exe) but be able to juggle and manipulate windows internals as easily as unix tools manipulate text files. That’s how they built Powershel and that’s why you should be using it.

But to me it holds little of value because I primarily use Windows for gaming. If I do something serious on it, like programming for example, then I’d rather have it behave like unix. Which is what makes Cygwin a perfect tool for me. It allows me to use the same dot files and scripts across the board on all my machines.

Cygwin, being what it is, is not always perfect. A lot of tools I have on other platforms simply haven’t been ported to Cygwin environment and probably never will be. For example, Pandoc does not run under cygwin. It does however run under windows and you can actually call it from within the Cygwin bash shell as long as you are mindful of the paths (Cygwin does this thing where it has it’s own internal filesystem, and your Windows drives are mounted in /cygdrive durectory).

Another tool I use this way on windows is Git. When I first set it up on windows, I went with the MySysGit version rather than the Cygwin one. My reasoning was that I wanted it available natively in the OS to tools that may want to use it. When I later installed it natively in Cygwin I noticed that having two slightly different versions of Git on your system doesn’t work very well. So I decided to keep the native Windows version and just call it a day.

I use MinTTY as my primary terminal, and I noticed that whenever I called git from it I didn’t have nice colorized output I’m used to. This is mostly a MinTTY issue because it was primarily designed to be a unix shell running on Windows and as such it supports the ANSI escape codes but not Windows escape codes.

The Color Problem

This is the problem I’m describing – note the lack of color in git output.

The easy solution to this problem is to switch to the default Cygwin shell which is essentially the standard windows terminal with a back-hack that lets it render the unix ANSI color-codes. But I refuse to do that because that terminal blows. I searched long and hard for a MinTTY centric solution but it appears that while a number of people complain about it, the developers are not particularly interested in fixing it because it is hard, and since they personally never call native Windows apps from within MinTTY then it is not a real problem. Because that’s the way we do things in the open source community folks. Oh, you have problem that doesn’t actually affect me? Fuck you then. How about you write a patch and maybe I’ll consider merging it in. Actually I’ll probably just delete it but who knows.

Eventually I realized that if I actually want to have color in my Unix shell, I need a different terminal and I eventually stumbled upon ConEmu which is rather aggressively developed, all purpose Windows terminal. By all-purpose I mean it can actually work amazingly well as a Powershell terminal. By default it wraps itself around cmd.exe and gives you slightly less shitty command line experience. It supports tabbed interface, transparencies and has a nifty status line.

If you want to run Cygwin inside of it, all you need to do is to create a shortcut (or a script) that will call it like this:

C:\ConEmu\ConEmu64.exe /cmd "C:\cygwin\bin\bash.exe" --login -i

Obviously, you may need to adjust the paths a bit. I installed both Cygwin and ConEmu in the root directory on my c: drive because c:\Program Files (x86) is the worst path in the universe, and whoever decided that a crucial system path needs two spaces and two non-alphanumeric characters in it needs to be slapped upside the head with a hedgehog.

Since the ConEmu developer envisioned the terminal as a multi-purpose tool, the ANSI color code support is baked in. This means that everything just works as it should without having to do anything:

ConEmu Output

ConEmu: Both ANSI and Windows color codes work.

Hell, the default UI even comes with Solarized theme presets you can enable from the Settings menu. This was rather surprising to me, because very few Windows centric tools know or care about the Solarized project. In fact, quality of a windows tool aimed at developers can be almost solely judged by the degree to which it supports these themes. If it ships out of the box with default solarized theme, it means serious business – download and/or buy it immediately. If it has it as an option, it’s probably pretty good. If it’s missing… Well, as long as you can use custom themes it might still be OK, but it will probably suck.

ConEmu has 3 versions of the dark theme, and two of the light theme and they are all off in various ways. I know this because I’m a Solarized junky and I install it on everything and I write my scripts to output colors in such a way that it looks good in these themes. All of the included themes got the overall look right, but then failed at rendering certain colors (especially red and green and pink/purple) which are integral parts of my bash prompt. So I had to tweak it a bit:

Adjusted ConEmu Solarized

My adjusted version of ConEmu Solarized-Dark

You can compare it to the default color setup to see the changes I made. I think it is still slightly off, but at this point it doesn’t bother me anymore. Still, the creator gets points for trying. While the implementation wasn’t perfect, it was close enough that it took only a few seconds to fix.

The only real gripe I have with ConEmu is that it’s color palette is rather limited and it doesn’t let me do things like run Vim with Solarized Light theme while coloring itself according to Solarized Dark. MinTTY handles this sort of thing swimingly and without complaining. That said, when I’m working on Windows I typically use Gvim rather than the console vim so this is less of a problem than just a minor nuisance. Besides, if I do want to use Console vim, I can always fell back on MinTTY and just deal with the lack of color in git output.

I’m not actually sure if this post will be useful for anyone who isn’t me, because like I said – I’m a bit of a weirdo when it comes to my windows shell environment. I like it to work and feel the same way as my unix and mac environment. What do you guys use? Do you run Cygwin on windows? What terminal do you use? How about Powershell? Let me know in the comments.

This entry was posted in sysadmin notes and tagged . Bookmark the permalink.

5 Responses to Unix ANSI colors and Windows colors in the same terminal

  1. Maximus RUSSIAN FEDERATION Google Chrome Windows says:

    Your “only real gripe” is not a gripe at all. Because ConEmu supports “App distinct” settings, and you may set up any suitable palette (and much more) to any running (active) application. Set up light theme for vim.exe and enjoy ;)

    One more thing. ConEmu supports xterm-256 color extension. You may try with vim zenburn color scheme or anything else. Implementation may be not perfect, but seems to be working.

    Reply  |  Quote
  2. fm UNITED STATES Google Chrome Windows says:

    Just run powershell inside of conemu. You can actually run a lot of programs, including gui tools, inside conemu.

    Reply  |  Quote
  3. Luke Maciak UNITED STATES Google Chrome Linux Terminalist says:

    @ Maximus:

    Oh shit, I must have missed it. That sounds perfect. I’m going to check it out and amend the post. Thanks for the tip. :)

    Reply  |  Quote
  4. It’s rare for me to develop in Windows these days, instead leaving it to the only thing it’s any good at: playing games. When I do need to code in Windows, for the last couple years I haven’t actually used Cygwin. And it’s all thanks to the Git folks.

    As you had mentioned yourself, I reach for msysgit, which includes all the dev tools needed to build Git. This is by far the easiest way to install a barebones first-class unix development environment on Windows. I also use the official (i.e. non-cygwin) w32 build of Emacs, which, if the PATH is set up for it, automatically integrates with the msysgit stuff. Missing programs I can either get from GnuWin32 or port/compile myself within the msysgit environment. Over the years I’ve actually sloppily ported a few programs — most recently pbzip2 — to Windows this way, though I unfortunately I haven’t bothered to publish any patches. The result is usually pretty messy since it’s merely secondary to some other goal.

    Ever since I stopped using Cygwin I haven’t bothered with any terminal emulators other than the pathetic Windows terminal emulator. If I was spending more time in Windows I’d probably check into the two that you mentioned.

    Reply  |  Quote
  5. k00pa FINLAND Mozilla Firefox Windows Terminalist says:

    I use msysgit for git on windows, and if I need terminal on local machine I also use that shell, I almost always have one instance of that running.

    When I need a real shell I can connect to VPS on internets or the local one I run inside a Virtualbox.

    I use shell on windows other than Git so rarely that I never really bothered with it, most of the time I just connect to a real unix system :P

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *