Getting Your Macro on with DOSKEY

I’m a big fan of the command line (much to the mirth of some of my co-workers). I do a lot of stuff through Git, Grunt and the like so I’ve usually got a Cmder window open somewhere.

When using Git, there are a bunch of commands that I use frequently so it’s handy to have a shortcut to these.

DOSKEY is a Windows utility that adds things like command history (via up and down arrows) and macro functionality for Windows XP and beyond (and 9 as well).

It’s the macro functionality that we’ll be looking at.

DOSKEY Usage

To add a macro (alias for you Linux types) using DOSKEY, the syntax is just:

doskey myshortcutcommand=my real command

So, if we wanted to use gp instead of git pull, the command would be:

doskey gp=git pull

Viewing Existing Macros

If you’re using Cmder, you’ll already have some macros set up. You can view these (and any you’ve added) using:

doskey /macros:all

Or, we can just add a macro to make it easier to remember:

doskey macros=doskey /macros:all

Passing Parameters

For some macros, we’ll want to pass parameters through from the macro to the original command. This is done using $*.

Let’s say we wanted to create an alias for git log so we could call things like gl -5. This would look like:

doskey gl=git log $*

Then we can call gl -5 to get the last 5 items.

Or, if we wanted to list commits in a custom format, we could do something like this:

doskey gl=git log --pretty=format:"%C(yellow)%h%Cblue%d %Creset%s%Cgreen [%cn, %ar]" $*

This format then produces output like this:

C:projectskwilson.me.uk (post-updates)
λ gl -5
2581a75 New post. [Kevin Wilson, 3 weeks ago]
f899823 Next batch of post updates. [Kevin Wilson, 3 weeks ago]
8a3e9cc Added short text summary support. [Kevin Wilson, 3 weeks ago]
c235841 Updated heading font size hierarchy. [Kevin Wilson, 3 weeks ago]
d548533 Mass replace of layouts. [Kevin Wilson, 4 weeks ago]

Deleting a Macro

If you decide you want to delete a macro, it’s simply a case of setting the macro name to equal nothing:

doskey someoldalias=

Launching Apps

The last thing that I tend to use this for is launching various apps as needed.

Sublime Text

doskey subl=subl="C:Program FilesSublime Text 3subl.exe" $*

allows you to use

subl somefile.txt

Notepad++

doskey np="C:Program Files (x86)Notepad++notepad++.exe" $*

allows you to use:

np somefile.txt

Explorer

doskey e.=explorer .

allows you to use:

e.

to launch an explorer window in the current directory.

Go Play

So go and create some macros and let me know about any good ones you come up with.

Kevin Wilson

.NET developer, JavaScript enthusiast, Android user, Pebble wearer, sometime musician and occasional cook.

Leave a Reply

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