7 Tips to Speed up Your Terminal Workflow

7 Tips to Speed up Your Terminal Workflow

If you’re anything like me, you probably spend most of your time in the terminal doing all stuff related to your development workflow. Becoming faster at doing those stuff is really a huge improvement for your productivity.

From the first time I started using the terminal, I kept looking for ways to speed up my workflow in it. I have figured out many ones, but here are my top 7 tips that I want to share with you.

1. Create your custom aliases

Most of the commands in the terminal are long and verbose. So having some aliases that shorten your most used commands is a helpful thing.

For example, one of the most commands I always use is git status. Instead of writing this, I have created an alias that allows me to use gs instead — Which I think much faster and easier to type.

Creating your own aliases is so easy. All you have to do is to go to your ~/.zshrc if you’re using zsh or ~/.bashrc if you’re using bash and create your aliases using this syntax:

alias yourAlias='Full Command'

For example, if you want to create an alias that allows you to use ll instead of li -l, you would do something like this:

alias ll='li -l'

After you create all aliases you need, I’m sure you’ll end up with a messy ~/.zshrc file. So it’s instead better to put all of your aliases into a dedicated file.

To do that, create that file and call it for example ~/.aliases then reference it in your ~/.zshrc using this:

. ~/.aliases

After that, you can include all of your aliases in that file, which is, in my opinion, a lot cleaner.

2. Create your custom functions

This is somewhat similar to the previous one. However, with functions you can have more flexibility. Functions, as you know, can take arguments and do logic. So there’re certainly some cases where you would need to automate some of your tasks using functions.

Creating functions is not as straightforward as aliases. You have a little more work to do. And actually there are many ways to do that. Either you use the native shell scripting or use another language for that (e.g. PHP, Ruby, Python, etc).

3. Use “z” instead of “cd”

Say that you usually go to your awesome-app directory which is at path: ~/code/projects/awesome-app. Normally, you would change your directory using cd ~/code/projects/awesome-app. However, with z you only have to write this:

z awesome-app

In case you don’t know, is a really awesome shell script that allows you to traverse the file system in no time.

Isn’t it awesome (say awesome 10 times fast)! The way it works is very simple. All it does is recording all the paths you usually go to, based on frequency. This means that the first time you try z awesome-app it won’t work. But after you traverse to it using cd at least once, it’ll be recorded, so you’ll then be able to use z on it.

It’s so easy to install it. All you have to do is this:

cd ~
git clone https://github.com/rupa/z.git

Then add the following in your .zshrc or .bashrc:

. ~/z/z.sh

That’s it!

4. Use “!!” and “$_” to access history data

There are some symbols in the terminal that you can use to access some useful history information. !! and $_ are just two examples that I use a lot.

!! is used to bring the last command you executed in your terminal. For example:

li ~/Code/Projects
!! (now equals to: li ~/Code/Projects)

As you can imagine this would be useful in cases where you want to use the last command but with a little addition. For example, to precede the command with sudo you would write this:

sudo !!

The second one is $_ which is used to access the most recent parameter you used in your last command. For example:

cp this tothat
$_ (equals to tothat)

A useful use for this is when you want to apply multiple sequential commands on the same parameter. For example:

mkdir test
cd $_

5. Use “&&” to chain commands

This tip can save you a lot of time. You likely know it, but what you might not know is where this would be useful.

Imagine that you want to run multiple sequential commands that depend on each other. And some of them may take long time to finish.

Normally, you would have to wait for each one to finish in order to start the next one. But what if you forgot to do that? Then the whole process will take even longer to finish (unless you’re waiting for each one to finish, or you have some kind of notification to tell you that). Instead, you can chain all the commands and press “Enter” without worrying about anything.

An example would be installing something from the source, like PHP:

./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql &&
make && make install && cp php.ini-dist /usr/local/lib/php.ini

Another quick common example is to create a directory and cd into it:

mkdir test-dir && cd $_

Look how I used $_ instead of writing the full name test-dir. It’s cool, isn’t it?

Before we move on to the next tip, I want you to know that there is another way to chain commands. Which is by using semicolon like this:

First; Second

The difference between them is that with ; you run the next one regardless of the success of the previous one. However, when using && the next command runs only if the previous one finished successfully.

6. Manipulate files very fast with “>” and “>>”

These operators are among the first things we learn when we start using the command line. However, we don’t often find much useful uses to them.

Having said that, I’m going to show you a really neat trick that will make you love using the terminal! It’s a combination of the cat command and the > operator.

If you've ever used node in your projects, you know that you have to create package.json with an empty curly braces {} in order to save the packages you pull in using npm install with save: npm install x -S.

Normally, what you would do is to create that package.json and open it in your editor, then add those empty curly braces, then save it and run your npm install.

Look at how I can do it with this single command:

cat > package.json
{

}
^C

Note that ^C means that I hit control-c.

This is just one example. You would likely find other cases like this one.

So the > operator is used to write on files. But what does >> do? Well, it’s to append content to files. So imagine that you have some file that you want append some string to it without opening it in your editor. With >> you can do this:

echo "some string" >> myfile.txt

Or you can even fetch content from a file and append it to another file:

cat src-file.txt >> dest-file.txt

7. Use “| pbcopy”

You’ll often find many cases where you would use this one. It’s used to copy any content you’re about to display on your terminal. For example, say you want to copy all the text inside file.txt. You’ll first use cat to get all content from that file then pipe it through pbcopy to copy it:

cat file.txt | pbcopy

I find myself use this one a lot when I want to copy my public RSA key:

cat ~/.ssh/id_rsa.pub | pbcopy

Do you know other tips?

Obviously, there are many other tips to speed up your terminal workflow. These are just the ones I find myself use the most in my day-to-day workflow.

I’m sure you have your own tips and shortcuts you use for doing things faster in the terminal. So it would be great if you take the time to share some of them with us — I really love to learn from others.

Terminal Workflow
Taha Shashtari

About Taha Shashtari

I'm a freelance web developer. Laravel & VueJS are my main tools these days and I love building stuff using them. I write constantly on this blog to share my knowledge and thoughts on things related to web development... Let's be friends on twitter.