Tips N’ Tricks
Over the years, I've picked up some nifty little terminal tips. They aren't critical, but they help improve the developer experience of using the terminal.
Cycling and toggling commands
Many terminal applications will keep a log of every command you've run in a given session. You can cycle through previous commands using the "up" arrow.
If I know I've run a command recently, it's usually faster to hit "up" a couple times rather than typing it out from scratch!
Here's one more amazing little trick I learned a while back: the -
character.
Suppose we want to bounce back and forth between two directories with cd
. We can do that by typing out the whole path, over and over and over:
Clearing the terminal
Like a clear desk, a clear terminal can lead to a clear mind.
There are a few ways to accomplish this. There's a clear
command, which will erase all previously-entered commands, and making it seem like you just started a new terminal session.
There's also a universal shortcut, ctrl
+ L
. This has the same effect as the clear
command. It should work across macOS, Windows, and Linux.
This command/shortcut is implemented within Bash/Zsh. It's part of the shell environment. This means that it only works while the shell is idle, when you have a prompt waiting to receive instructions.
Certain terminal applications also implement their own shortcuts, and these shortcuts can work even while the shell is busy. Here are the list of shortcuts I'm aware of:
- On macOS, across just about any shell (Terminal.app, iTerm2, Hyper), the shortcut is
⌘
+k
- If you use Hyper on non-macOS platforms, the shortcut is
ctrl
+shift
+k
.
These application-level shortcuts are way better. You can use them even when the shell is busy.
For example, let's say you're running a dev server. This is a long-running process, and so the ctrl
+ L
shortcut won't work. As you work on the project, lots of messages will be logged in the terminal window. The application shortcuts allow you to clear away stale logs, as if archiving old emails. This is really helpful, and a great example of how modern terminal applications make our lives easier.
Thanks to Aleksandr and Joseph Cagle for helping me understand how this works on non-macOS platforms!
Aliases
Every now and then, I'll find myself typing out the same command over and over. If this command is long or complex, it's annoying to have to type it out every time, and to remember it verbatim.
Bash and Zsh support aliases, a way of creating custom shortcuts. For example, I can set it up so that whenever I enter hi
, it automatically runs echo "Hello World!"
:
Setting up aliases is a bit beyond the scope of this tutorial, and the instructions are a bit different depending on your shell language. Here are some helpful tutorials that go into more depth:
Switching to a GUI file explorer
Unless you've reached black-belt status with the terminal, there will be times when you want to open the working directory in a GUI file explorer.
On macOS, the open .
command will do this:
The open
command is generally used to open a file, the same way double-clicking a file opens it in a GUI file explorer.
When we try to open a directory, however, it'll choose to pop open a new Finder window, showing the contents of that directory.
And since the dot character (.
) refers to the current directory, open .
allows us to switch from the terminal to Finder, to continue our work outside of the terminal.
On Windows, you can use explorer .
On Linux, xdg-open
can be used to open files, or the current directory, though this might not work across all Linux distributions.
Chaining commands
Whenever I clone a new project from Github, I generally want to do two things in a row:
npm install
, to fetch third-party dependenciesnpm run dev
, to boot up a local development server
The npm install
command typically takes a few minutes. I don't have the attention span to sit and watch dependencies download, and so I'll often distract myself with Twitter. The next thing I know, 20 minutes have passed, and I totally forgot I was going to start a dev server. 😬
We can solve this problem using chaining. Here's how it works:
The &&
operator allows us to chain multiple commands together. The first command will be executed, npm install
. The moment it finishes, the second command will be run automatically, starting up the dev server.
Once I got the hang of chaining, I started using it everywhere. I'll often queue up a bunch of Git commands:
git add . && git commit -m "Stuff" && git push origin main
Terminal tiling and tabs
Alright, so let's talk about how to keep our workspace organized.
Running a dev server with npm run dev
is a long-running process. I often have dev servers run uninterrupted for weeks at a time!
When a terminal session is busy on a task, it isn't able to accept additional commands. Remember, the prompt is used to show that the terminal is waiting for a command; if we don't see a prompt, we can't run anything in that given session!
Fortunately, modern terminal applications make it easy to run many terminal sessions in the same application.
In Hyper, we can split the window into multiple vertical panes by selecting Shell -> Split down. On macOS, the shortcut is Shift
+ ⌘
+ d
. This creates two independent sessions:
By splitting the window into multiple sessions, the top session can focus on running the dev server, and highlighting errors and other important information. The bottom session can be used to run shorter tasks.
Sometimes, projects will require multiple long-running tasks; maybe we have a dev server and a test watcher. In that case, we'd split the window into 3 sessions.
In Hyper, we can also create multiple tabs. New tabs can be created with Shell -> New Tab. On macOS, the shortcut is the same as it is to create new tabs in a web browser: ⌘
+ t
.
When do we use tabs vs. tiles? I like to have 1 tab per project. Each tab can be split into as many sessions are required for that specific project.