Pages

Monday, March 17, 2014

Improve command prompt experience on Windows

I always find it frustrating that command prompt on Windows is not as great as a *nix shell. So I am always on lookout for cool things that improve my command prompt experience on a Windows box. I have had fair amount of success in maintaining interest in command prompt experience on Windows by  using combination of tools, techniques and package manager for windows. I am going to talk about these today.


1. Tools


If you are like me then you would have those frustrating moments with Windows command prompt when you have to go through hoops of clicks in order to complete trivial tasks like copying and pasting commands. Limitations like these make Windows command prompt one of the least interesting tools. But you are not alone and some great people have built tools that add a new dimension to productivity of Windows Console. Here are three that I have used so far in order of my preference

Do not download any of these just yet. In the last section I talk about a windows package manager which makes installing these a breeze.

ConEmu (Console Emulator)

Here is a one line description of ConEmu from their site
ConEmu-Maximus5 is a Windows console emulator with tabs, which presents multiple consoles and simple GUI applications as one customizable GUI window with various features.

What I like most about ConEmu is it comes loaded with lot of simple features like select text to copy to clipboard etc. It also come with some default color schemes so that you do not have to fiddle with colour settings of every odd element of console as in Windows Command prompt. ConEmu is quite lightweight, and starts in no time on my VM

Scott Hanselman has a written about how he is using ConEmu which I recommend reading first if you plan on using this emulator.

Cmder

Again, here is the description of Cmder from their site
Cmder is a software package created out of pure frustration over the absence of nice console emulators on Windows. It is based on amazing software, and spiced up with the Monokai color scheme and a custom prompt layout. Looking sexy from the start.

Cmder IMO offers a better experience as compared to ConEmu and I feel is closer to Ubuntu shell and ConEmu is (I like Ubuntu shell). But Cmder is heavy and at times slows down.

Console2

Again, here is description from Console2 website

Console is a Windows console window enhancement. Console features include: multiple tabs, text editor-like text selection, different background types, alpha and color-key transparency, configurable font, different window styles

I do not have great deal of experience with Console2 so I cannot say much about this. Scott Hanselman has a review of Console2 which you should go through if you need to know more before installing.


2. Tricks


There are not tricks but only one trick so the heading a bit misleading. Never mind, this one trick is going to relieve you of lot of frustration. I was introduced to this by my friend Peter Camfield. This is combination of Doskey Macros from Ben Burnett and AutoRun settings for Windows command prompt. This trick lets us create nice shortcuts and aliases for the commands we use commonly. E.g. you can use "ls" instead of "dir" if you come from Linux background.

Copy the script from Ben's article and save it on your disk as alias.bat. Suppose you saved it a C:\alias.bat. Next, open registry editor (type "regedit" on command prompt if you do not know how) and go to following location


HKEY_CURRENT_USER\Software\Microsoft\Command Processor
 If a string value "AutoRun" exists then append the path of your alias.bat to it. If not, create new string value and set it's value to "C:\alias.bat". In my case, I was already injecting ANSICON into my console, I had to append my path as below

(if %ANSICON_VER%==^%ANSICON_VER^% "C:\Program Files\ansi151\x64\ansicon" -p)&c:\alias.bat

Restart your command prompt and aliases defined in alias.bat should be at your service now. True power comes from how smartly you define your aliases. My alias.bat file looks like below. Do not copy that file as is. I have replace couple of my path with <<instructions>>. Replace those with your paths

        
;= @echo off
;= rem Call DOSKEY and use this file as the macrofile
;= %SystemRoot%\system32\doskey /listsize=1000 /macrofile=%0%
;= title= cmd with aliases
;= rem In batch mode, jump to the end of the file
;= goto end
;= rem ******************************************************************
;= rem *   Filename: aliases.bat
;= rem *    Version: 1.0
;= rem *     Author: Ben Burnett 
;= rem *    Purpose: Simple, but useful aliases; this can be done by
;= rem *             other means--of course--but this is dead simple and 
;= rem *             works on EVERY Windows machine on the planet.
;= rem *    History: 
;= rem * 22/01/2002: File Created (Syncrude Canada).
;= rem * 01/05/2007: Updated author's address, added new macros, a 
;= rem *             history and some new helpful comments.
;= rem * 19/06/2007: Added Notepad, Explorer and Emacs macros.
;= rem * 20/06/2007: Fixed doskey macrofile= path problem: it is now not 
;= rem *             a relative path, so it can be called from anywhere.
;= rem ******************************************************************

;= Doskey aliases
h=doskey /history

;= File listing enhancements
ls=dir $*
ll=dir /w $*

;= Directory navigation
up=d:\up.bat $*
down=popd
back=popd
pd=pushd

;= Copy and move macros
cp=copy
mv=move

;= Delete macros
rm=del /p $*
rmf=del /q $*
rmtmp=del /q *~ *# 2>nul

;= Fast access to Notepad 
np="C:\Program Files (x86)\Notepad++\notepad++.exe" $* 

;= Fast access to Explorer
x=explorer .

;= List aliases
alias=doskey /macros | sort

;= Which
which=where $*

;= Project specific stuff
showsvn=start <>
code=cd /d <>

;= Reference
ss64=start http://ss64.com/nt/

;= IIS specific stuff
wp=c:\Windows\System32\inetsrv\appcmd.exe list wp
recycle=c:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:$*

;= Edit host file
hosts=gvim c:\windows\system32\drivers\etc\hosts
;= :end
;= rem ******************************************************************
;= rem * EOF - Don't remove the following line.  It clears out the ';' 
;= rem * macro. Were using it because there is no support for comments
;= rem * in a DOSKEY macro file.
;= rem ******************************************************************
;=

The above file would need following file stored at the same path as above file and named up.bat

@ECHO OFF
PUSHD .
SET NUM=%1
IF [%NUM%]==[] SET NUM=1
FOR /L %%G IN (1,1,%NUM%) DO CD ..

Here are some of the cool aliases I have defined

wp lists IIS worker processes and their PIDs
recycle <apppool name> recycle the apppool. wp gives the name of the apppool
up <number> navigates to "number" level up from current directory
x open file explorer
hosts open hosts file in notepad
code opens the source code path in file explorer
np opens notepad++

Then I have got Linux equivalent shortcuts for commonly used commands like dir, where, del etc.


3. Package Manager


If you have used Linux/Unix then your know how easy it is to install programs using repositories like apt-get. This simple utility makes working on shell on Linux a breeze and keeps you on shell. For a long time, there was nothing similar for Windows world. Fortunately the scene is changing and Chocolatey is first such package manager for Windows machines. Chocolatey is powered by NuGet and really takes NuGet to next level where it starts making since. Just head over to their website for installation instructions or if you are too lazy type in following (you would need PowerShell)
        
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Above command would download and install Chocolatey for you and it would also set all the PATHs needed. Just start a new command prompt and you are ready to get cracking with it.

You can run following commands to install one of the console emulators I mentioned in first part.

        
C:\Users\Suhas.Chatekar\cinst conemu
        
C:\Users\Suhas.Chatekar\cinst cmder
        
C:\Users\Suhas.Chatekar\cinst console2

Go give it a try and you would never feel like running away from console again. These days I install almost everything from command line using Chocolatey.

If you are using something to improve your experience on Windows command prompt then share. I would be delighted to learn from you.

2 comments:

  1. Another package manager for windows worth mentioning is scoop: http://scoop.sh/

    ReplyDelete
  2. Thanks Chris for letting me know. I had a quick look at it and it has lot of resemblance to *nix which is good. I would update my post with more details as I get my hands on it.

    ReplyDelete