Skip to content

Instantly share code, notes, and snippets.

@krowe
Last active December 14, 2021 16:15
Show Gist options
  • Save krowe/702d74de6b4b74b0ccc5 to your computer and use it in GitHub Desktop.
Save krowe/702d74de6b4b74b0ccc5 to your computer and use it in GitHub Desktop.
This guide shows you how to make a more GNU friendly Windows environment

Guide to a more GNU-like Windows CLI

Note: Most of this is now outdated information. Now that WSL exists it is the prefered way to use Linux on a Windows machine. The Windows 11 command line also does a lot of the things I used ConEmu for by default. It's still better but not as much so. The Windows app store now remembers your software so that you can easily redownload it so my personal need for Chocolately is all but gone as well.

This is a guide to setting up a Windows command line environment which supports many of the commonly used features available in a GNU\Linux environment. It is most useful to people whom work in mixed environments and would like a more seamless experience when switching back and forth but is also useful to those who've never used the GNU tools before.

This will consist of adding a package manager, the GNU tools library, a DOSKEY replacement for better command line completion and a better terminal application.

Step 1: Install Chocolatey

If you want to also upgrade your installation process to a more datacenter friendly system such as that used by most GNU distributions then Chocolatey is the answer. You may install each of these applications manually if you like but Chocolatey makes doing this much easier and also makes installing most other open source Windows applications easier as well.

Chocolatey Website

The downside to using Chocolatey is that it doesn't give you the same level of control over the install process as you have when using traditional means. It also often fails to uninstall applications completely. If you can work around this then it is a good option.

To install Chocolatey run this in a command window (all one line) to download and install the downloader:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Chocolatey is also not very flexible and will always install to C:\Chocolatey.

If you already have 7-Zip installed you can either add it to your path or you can uninstall it and install it with Chocolatey:

cinst 7zip

7-Zip is used by most Chocolatey packages so it must be in your path. You should now be ready to begin using Chocolatey for installing and uninstalling applications. You may want to add C:\Chocolatey\bin to your $PATH environment variable.

Step 2: Configure GnuWin

GnuWin is a project which ports many of the most common Gnu console applications to Windows. It is a rather difficult to manage all of the applications manually so the installer will use the sister project, GetGnuWin32 to install it.

GnuWin Chocolatey Packages OR GetGnuWin32 Information Page

To install GnuWin it is easiest to use Chocolatey. Just type cinst GnuWin to install the files. Chocolatey will install the GnuWin files to the following location:

C:\GnuWin

I like to make a fix to the ls command which allows it to automatically adjust the width of the display to the current window width and also tells ls to color the output like it does in most Linux environments by default. To do this I create a batch file which I will later make an alias for:

@echo off
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set CONSOLE_WIDTH=%%W
ls --color -w %CONSOLE_WIDTH% %*

I name this C:\GnuWin\bin\ls.bat and make the alias with another batch file I call profile.bat and place in the same directory.

@echo off
DOSKEY ls=ls.bat $*
DOSKEY cd~=cd %USERPROFILE%
DOSKEY cd!=cd %USERPROFILE%\AppData
DOSKEY cd#=cd %USERPROFILE%\Desktop
DOSKEY cd@=cd %USERPROFILE%\Documents
"%ProgramFiles(x86)%\clink\0.3.1\clink_x64.exe" inject -q -p "%USERPROFILE%\AppData\Local\clink\color" 

To help inject clink also add cli.bat:

@echo off
"%ProgramFiles(x86)%\clink\0.3.1\clink.bat" inject

This will be used in the next steps. When you've confirmed that the tools have been correctly installed you are ready for the next step.

Step 3: Configure ConEmu

ConEmu is an improved console emulator for Windows. It provides a much better experience including some things which help provide what we want in a Linux style environment.

ConEmu Chocolatey Packages OR Download ConEmu

To install this type cinst ConEmu at the command line. This will install to C:\Program Files\ConEmu and a shortcut will be created for you on the desktop.

Now we want to modify the task setting in ConEmu to add more functionality. To do this click the down arrow to the right of the plus icon in the toolbar and click Setup Tasks....

The first thing to do is modify the default task called {cmd}. Change the command to read: cmd.exe /K C:\GnuWin\bin\profile.bat. I also set the default directory by setting the Task parameters to something like: /dir %USERPROFILE%.

Now we need to tell ConEmu to use this by default. To do this use the next settings page up called Startup. On this page there are 4 radio buttons. Select the third option and then use the drop-down to select the {cmd} item which you've just edited.

Another handy thing to do now is to go back to the Task parameters settings and setup an entry for Putty if you use it, to do this use C:\Chocolatey\lib\putty.portable.0.63\tools\PUTTY.exe for the command and Putty for the title. You may need to verify this location by looking in C:\Chocolatey\lib for the actual putty version you've installed.

Step 4: Configure CLink

CLink is a Windows application which provides a more BASH-like command line completion mechanism than DOSKEY does.

CLink Chocolatey Packages OR Download CLink

To install this type cinst Devbox-Clink at the command prompt. You will likely see an error the first time you install this. The solution is to run the command again. This should fix the problem. It will be placed at %ProgramFiles(x86)%\clink\0.3.1. If you put it somewhere else then you'll need to modify the appropriate line in your profile.bat file which you created earlier.

The file %USERPROFILE%\AppData\Local\clink\settings is the configuration file you can use to further customize CLink.

Unfortunately, color support isn't supported in the default windows command line interpreter. If we add colors to clink we'll need to remove the clink autorun support which is provided by default. We can still use it by first copying the current settings files to a new folder. This folder will allow us to provide color options when using the the ConEmu terminal:

mkdir %USERPROFILE%\AppData\Local\clink\color
cp %USERPROFILE%\AppData\Local\clink\* %USERPROFILE%\AppData\Local\clink\color

We will use this folder for specifying the color setup. Now we can disable the autorun so that we don't automatically color the prompt. This must be ran as an administrator:

"%ProgramFiles(x86)%\clink\0.3.1\clink.bat" autorun -u

Now we can set the color options use the following colors in %USERPROFILE%\AppData\Local\clink\color\settings:

  • 0 black
  • 1 blue
  • 2 green
  • 3 aqua
  • 4 red
  • 5 purple
  • 6 yellow
  • 7 white
  • 8 grey
  • 9 light blue
  • 10 light green
  • 11 light aqua
  • 12 light red
  • 13 light purple
  • 14 light yellow
  • 15 bright white

I personally set match_colour = 3 and prompt_colour = 8.

If you are in the default terminal and you'd like to use clink features you now must first run this command to inject it:

cli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment