You don't have to be a linux hacker to use the terminal to be productive. Here's a few tips I've picked up the last couple of years that might make you even iller in the terminal.
Just a disclaimer - I'm no terminal professional. Yet, over the 7 last years, I've learned a few very nice-to-know techniques that you might not have heard of. There's no guarantee that you'll learn anything from this, but a pretty bit chunk of my readers might learn at least a thing or two.
A little side note here - these commands are meant for OSX only. They might work on Linux or WLS, but I haven't tried them out.
Every once in a while, you have some data in the clipboard that you want to use somehow, or you want to copy the output of a command into the clipboard. Either way - it's very possible to do!
So there are two commands you need to know - pbcopy
and pbpaste
. You can use them together with the pipe operator |
and the "to file" operator >
to do really cool stuff. Here's a few examples:
To paste whatever is in your clipboard into a new file - let's say ".env" - you can do this:
$ pbpaste > .env
No more creating a new file, then opening it in vim or VSCode to paste and save - now you can just type a few characters and get the job done!
Similarly, if you want to copy a file to the clipboard, you use the pipe operator and pbcopy
. This is how it looks:
$ cat .env | pbcopy
cat
echoes out the entire contents of a file (in this instance .env
) and the pipe operator passes it as input to the pbcopy
command. Voilá - now you can copy the content of entire files without opening them!
I use this all the time when I need to copy my ssh key and paste it into GitHub or BitBucket.
You don't have to limit yourself to files though - pbcopy is much more flexible than that. Whatever you pipe in can be placed on the clipboard - so if you want to copy the result of a program (let's say an encryption program or a password generator), you just pipe it on in.
less
I often use the tiny file reader program less
to scan, search and read through files. It's really powerful if you know how to use it, but even if you're just using it for the first time, you can just use the arrow keys on your keyboard to scroll through files directly in the terminal.
Now, I use this mostly to read configuration files, and if I come across something I need to change, I've always had to close the file I was looking at, and open it in an editor like vim
.
Luckily - you can just type v
, and the default editor (typically vim
) opens up, focused on the same line you were at. Once you close vim
(provided you know how to close vim
), you're placed back in the same spot as you were as well.
Also - you can go the start of the document with g
, and to the end with shift + g
. You can search for a phrase by tapping /
, and typing out whatever you're looking for. Navigate through the occurrences by n
(forward) and N
(backwards). And there's a ton of other things you can do (which you can read about here).
open
Often times, you stumble across a file you want want to open the file's default application. That might be a video, an audio clip or perhaps a CSV file. You could exit the flow you're in, of course, and use Finder to navigate to your file and double click it - but we can do better.
If you want to open any file, you can use the open
command to get it done from the terminal:
$ open videos/screen_recording.mp4
$ open package.json
$ open images/vacation.png
I usually don't open a lot of files this way, but I do open folders! Just use open
on any folder, and you'll get started right away!
$ open ~/Documents
$ open . # opens the current folder
There are two really nice characters to know when using the terminal, and those two are -
and !
.
The -
character (dash, hyphen, whatever you call it) works well in two contexts. You can add it to the cd
command to return to the previous directory you were in, and you can add it to git checkout
to check out the previous branch you were on.
# Example of using cd -
~/Documents $ cd /usr/bin
/usr/bin $ cd -
~/Documents $
# Example of using git checkout -
(master) $ git checkout feature-branch
(feature-branch) $ git checkout -
(master) $
The exclamation mark is also pretty neat - it lets you search the history of command you've done previously, or re-use the arguments to the last command you used.
$ !cat
$ # shows you the last command you ran that started with cat
$ less a.txt b.txt
$ vim !* # opens a.txt and b.txt in vim
$ less a.txt b.txt
$ vim !$ # opens b.txt in vim
It's a neat little trick that I know for some reason, but I never use. Perhaps you'll find some use for it though!
Much more usable, however is the double exclamation point! Especially in conjunction with the sudo
command. If you've ever run a command and realized you had to run it as an administrator, you can simply do sudo !!
and re-run it with sudo!
$ chmod +x /usr/share/firmlinks
chmod: Unable to change file mode on /usr/share/firmlinks: Operation not permitted
$ sudo !!
$
Speaking of command history - you don't really need the ! command when you know about the Ctrl+R shortcut! Tap it once, and you'll be able to do an interactive autocomplete search of your entire command history!
Lastly, I want to talk about oh-my-zsh. Zsh (pronounced sea shell) is a popular alternative to the well known bash shell, and oh-my-zsh
is a small framework that adds a ton of useful functionality, aliases and plugins to make your workflow as smooth as possible.
If you haven't installed it already, you can do so by visiting their home page. Next, pop up this cheat sheet and start learning. There's a ton of useful shortcuts for git (like gst
for git status
and gc
for git commit
), but also really nifty commands like ...
for navigating two directories up or take deep/directory/tree
for creating a new directory and navigating into it.
In addition, there are tons of great themes and plugins available to make your terminal super powerful. I've had oh-my-zsh
installed for years, and I just love it more for every passing day.
These are some of my favorite tips for being productive in the terminal. There are tons of stuff I've skipped - like how to grep or exit vim - but to be honest I don't use those a lot either. I hope you found at least one new technique to add to your roster - and that you promise to share your favorite commands with me on Twitter.
All rights reserved © 2024