Preface
Entering long winded commands with many parameters, paths and redirection in the shell/terminal can be time consuming, repetitive and prone to human error. Luckily on both Windows and on Linux you can provide simple abbreviations for long commands.
Windows
On Windows the DOSKEY command (provided by default with the operating system) is pretty versatile and can be used to easily configure macros and command abbreviations. DOSKEY is actually resident during all Windows command line sessions and is the underlying system responsible for maintaining the command history (which can be navigate with the updown/pgup/pgdown keys). Below I have listed some example usages of DOSKEY;
# Note: The $G character is a special doskey escape character for the # redirect character '>' which allows it to be specified as part of # the command without redirecting the doskey command itself. doskey /history # Show history of all previously run commands doskey /reinstall # Clear command history doskey here=explorer . # Add 'here' macro to open an explorer window at the current location doskey desktop=cd "%USERPROFILE%\Desktop" # Add 'desktop' macro which takes you to the current user's desktop doskey mp3list=dir %CD%\*.mp3 /S /O:N $G mp3list.txt # Add 'mp3list' macro to recursively and alphabetically list all .mp3 # files below the current directory, inside mp3list.txt
Note: Any macros configured using doskey are only active for the active command line session. To create macros which persist between sessions consider placing all macro creation commands in a batch script, you then have two options:
- Create a shortcut which opens the command line and runs your batch script immediately (shortcut location = %comspec% /k %path_to_script%)
- Use an alternative command line environment such as Console2 which can be configured to automatically run your batch script on start-up.
Linux/OSX
On Unix-esque systems it’s pretty much the same kind of setup using the alias command. Some example usages are listed below;
# Note: All alias entries are wrapped in single quotes so there is no requirement for special alias # escape characters for piping / redirecting streams alias here='nautilus ${PWD}' # Add 'here' macro to open a nautilus window at the current location type here # Prints the expanded version of the 'here' macro unalias here # Unregister the 'here' alias alias blog='firefox https://tomtech999.wordpress.com' # Add a 'blog' alias to open your blog in firefox alias dumpenv='export -p | grep "/" > env.txt' # Dump all bash environment variables containing a path to 'paths.txt'
Note: As with DOSKEY on Windows, any macros configured using ‘alias’ are only active for the active bash shell session. The easiest way to make aliases persist between bash sessions is to add all of your alias commands to the bashrc shell configuration file located in your home directory (~/.bashrc ). This script is executed whenever you open a bash shell and can be used to automatically register aliases for all bash sessions.
References
Microsoft DOSKEY Documentation
10 Handy Bash Aliases
Hak5 Linux Temrinal 101 – Create Your Own Commands
30 Handy Bash Shell Aliases For Linux/Unix/MacOSX