The more I work in software development, the more I love working with the command line. I notice I’m becoming increasingly productive with it. When dealing with Git, for example, I used to do almost everything in a GUI program, whereas now I use the command line for 90% of the actions. But the command line will make you more productive with everything, not just source control. There are several reasons why it’s so efficient. More efficient than using GUI programs in most cases. Here are some of those reasons.
- Typing and using the keyboard is usually faster than going through menus and clicking on buttons, once you get used to it.
- You can automate common commands and tasks, which makes it even faster.
- You can customize a lot for your individual machine and environment.
The command line has a cool factor too. Real developers use the command line. While wearing a hoodie of course.
For me, working with the command line is not an obvious matter. If you’ve been dealing with Linux systems, the command line might feel second nature to you. But if you grew up with Windows and .NET development, like me, the command line wasn’t used. Everything was kind of done in Visual Studio. Running build/debug/tests- in Visual Studio. The source control, which was usually TFS (now Azure DevOps)- used from Visual Studio. Package management meant using NuGet manager in, you guessed it, Visual Studio. The database was SQL Server of course, that has a dedicated GUI app SSMS . But mostly we just used Visual Studio.
The world has changed and so did my day job. Right now, I’m working mostly in TypeScript and Node.js. But even .NET developers are living in the command line nowadays. I learned a lot of cool tricks in the last few years. Techniques that made a big difference in my efficiency and workflow organization, and I think they’ll do that for you as well. So let’s go over them.
1. Use Command Chaining
There are lots of use cases when you have a fixed set of commands to execute one after another. That might be
npm install followed by
npm build. Or
git fetch --all followed by
git merge origin/[my-branch]. It feels silly to have to wait for the first command to finish and then run the second command. What am I supposed to do in the meantime? Stare at the screen for 10 seconds?
Luckily, shell languages allow entering multiple commands in one expression. In Windows command prompt, you can use the
& character to chain commands. If you type
&&, the second command will execute only if the first command finished successfully. You can use
&& to chain as many commands as you want, not just two. Here are some examples of frequent combinations I use:
git clone [repo] && yarn install && yarn build
npm build && npm run test
git fetch --all && git merge origin/[my-branch]
git commit -a -m [message] && git push
I believe Bash uses the same chaining characters. Unfortunately, PowerShell has no such functionality (yet).
2. Chain a sound effect at the end of a command
A command’s execution, or several chained commands, can take a long time to finish. You can patiently watch as the computer displays pretty ASCII characters on the terminal screen, or try getting some kind of push notification when the script’s finished. I found a simple method for that: I chain at the end of the command an additional command that plays a short beep sound. When I hear the beep, I know my script’s finished.
mycommand & ^G
Note that I’ve used a single
& character because I want to hear the sound regardless of whether the command succeeded.
Instead of using some mp3 file, which will open a music player by default, you can type
Ctrl + G in Windows, which will show as
^G and make a standard “error” sound.
3. Create command aliases
Don’t get me wrong, I don’t mind typing, I just find it distasteful to write the same commands over and over again. But there’s an easy solution for that: using aliases for common commands. For example, you can set
gf to execute
git fetch --all, and
gfm to run
git fetch --all && git merge origin/main & ^G.
The command alias can accept arguments. For example, I have
n for Notepad++, which means I can write
n [somefile] and it will open that file in Notepad++. I also set
yarn. I don’t see why I have to type four letters when I can type just one.
An easy way to create aliases in Windows is to create
.bat files for each alias. Place all your command alias files in a folder that exists in the
Path environment variable, and they will always be available from any shell window.
For commands that require arguments, you can use
%1 for the first argument,
%2 for the second one, etc. So in the case of
n, which I use for Notepad++, my script
n.bat has this content:
start "" "C:\Program Files\Notepad++\notepad++.exe" %1 (the empty string is for the Window title, which I don’t want to change).
Here are some of my scripts that I declare open-source for anyone to use:
|frc||start "" “C:\Program Files (x86)\FreeCommander XE\FreeCommander.exe” %1|
|g||git %1 %2 %3 %4 %5|
|gaa||git add .|
|gc||git commit -m %1 %2 %3|
|gca||git add . & git commit -m %1 %2 %3|
|gdt||git difftool %1|
|gex||gitex openrepo .|
|gfm||git fetch –all && git merge origin/main & ^G|
|gmt||git mergetool %1|
|y||yarn %1 %2 %3 %4 %5|
|ycb||yarn clean & yarn & yarn build & ^G|
4. Use AutoHotKey abbreviations
This is more of a general advice for any kind of typing situation, but it’s particularly useful for the command line.
You might know the excellent AutoHotKey tool. It allows you to create scripts that can be triggered with keyboard shortcuts. One of its useful features is abbreviation support. I can bind an abbreviation like “afaik” to change to the full “as far as I know” text every time I type those letters.
I got some useful abbreviations for my blogging and everyday use. Some of them are vst for “Visual Studio”, vsc for “Visual Studio Code “, m@ for my email, and nrt for “Not relevant, thanks.” to use whenever I get an email from a recruiter. For the command line, there are some other use cases. Here are a few ideas:
- Abbreviate commonly used folders.
- Abbreviate domain-specific phrases, like
gcfor “Garbage Collector”, or
- Abbreviate frequently used command lines, or part of command lines, that you want to modify before executing (otherwise, you’re better off with an alias script).
- Abbreviate project names.
Adding abbreviations is as easy as writing something like
::afaik::as far as I know in the .ahk script file.
5. Copy-paste terminal text with the keyboard
One of the reasons the command line is so productive is because it encourages using just the keyboard. Copying text from the terminal, which is a common action, can be done just with the keyboard, without the mouse. At least in Windows Command Prompt. It doesn’t work in Terminal or in PowerShell, which is one of the reasons I’m still using good old CMD. To do that, just press Shift + arrow keys, without any special magic shortcut.
Unfortunately, as you can see in the screenshot, the selection is not what you actually need. You’ll usually need the first line of the selection. To get it requires pasting the clipboard to something like Notepad, copying the first line, and then pasting again in the command line. What a pain, right? Might as well use the mouse.
I found a nice solution for that with AutoHotKey by writing a small script that copies just the first line of the selection. Actually, I asked ChatGPT to make the script for me, which worked out nicely.
; Copy the first line of selection ^#c:: ; Ctrl + WinKey + C Clipboard := "" ; Clear the clipboard Send ^c ; Simulate Ctrl + C to copy the selected text ClipWait ; Wait for the clipboard to contain data Clipboard := SubStr(StrSplit(Clipboard, "`n").1, 1, -1) ; Cut to the first line, excluding the last character (EOL) Return
6. Quickly switch between your most used folders
In my day job, I got multiple repos where I constantly use the command line. But it could have been a single repo, with multiple projects, to the same effect. One of the problems, in this case, is quickly switching to the folder you need in the in the shell window.
If you’re using the Terminal app, you can create dedicated profiles for each folder. A profile includes a name, a starting directory, and an icon, which provide a good way to quickly switch between common folders. You can even set tab colors, though these don’t persist, unfortunately.
But I’m using the good old Command Prompt, which makes things more difficult. The solution I found is to create shortcuts for my commonly used directories. I added colorful icons, so once I got used to the colors, it became very easy to differentiate between the windows. I then pinned those shortcuts to the taskbar and now I can use the
WinKey + [number] to open them. So
WinKey + 3,
WinKey + 4, and
WinKey + 5 open the command prompt for my 3 main repos on all my development machines.
To have a shortcut to a command prompt that’s opened in a specific directory, set the target of the shortcut to:
C:\Windows\System32\cmd.exe /k cd /d [starting-directory]. You can also go to the shortcut’s advanced settings and check “Run as administrator” if needed.
Another way to quickly switch between folders is to create command aliases that “CD” to the most commonly used ones. For example, let’s say you have repos at
D:\Dev\SideProject. You can create three .bat files with the following content:
|cd1.bat||cd C:\Git\Repo1 & C:|
|cd2.bat||cd C:\Git\Repo2 & C:|
|cdsp.bat||cd D:\Dev\SideProject & D:|
With this trick, you can use the same command prompt window for everything.
Another way to achieve this is with AutoHotKey abbreviations like this one:
::cd1::cd D:\Git\Repo1 & D:.
7. Reuse a personal repo on all your machines
We went over several techniques like command aliases, AutoHotKey scripts, and colorful shortcuts. It takes some work to create scripts and shortcuts that are useful to you. When you have multiple machines, it wouldn’t be very effective to manually maintain the same scripts up to date on all of them. And even if you have a single dev machine, I wouldn’t want you to live in fear that your automations would be lost if some disaster happens.
I suggest keeping a personal private repo just for these kinds of automation. Every time you make a change in a script, or add a new shortcut, push your change. Then, just pull from time to time in your other machines.
Another way of going about this is keeping your automation files in a folder that’s synced with your cloud. In a way, it’s even better because you don’t have to manually push and pull changes. It just syncs automatically. In other ways, it’s not great. There’s no history for example and it feels more prone to problems. But, I’m curious to hear if anyone’s tried it.
That’s all my wisdom on this matter, hope it helped you in some way. If you have some command line usage tips, please share them in the comments. Cheers.