This article was published on October 23, 2021

9 time-saving tricks for your command line

Navigate your terminal like a productivity wizard


9 time-saving tricks for your command line Image by: Shutterstock

How do you write great code? By being efficient. If you want to create something awesome, you’ll have to eliminate the time dumps that slow you down. With just a few tricks, you can speed up your work and focus on what matters.

1. Create aliases

Every shell comes with a file called ~/.bashrc or ~/.bash_profile. Which one you’ll use depends on your system. This post explains it in detail.

Now think about commands that you use a lot. Typing them each time is arduous, and you can mess things up if you’re prone to making typos. That is what aliases are there for: They replace your original command by a shortcut.

alias $preferredAlias='$commandToAlias'

For example, I have a Dropbox folder that I need to access quite often. As I don’t like typing too many characters, I have this alias in my ~/.bash_profile:

The <3 of EU tech

The latest rumblings from the EU tech scene, a story from our wise ol' founder Boris, and some questionable AI art. It's free, every week, in your inbox. Sign up now!

alias topbox="cd ~/Dropbox/top-eft/"

Some other useful aliases are:

alias mv="mv -i"
alias cp="cp -i"

The option -i indicates that you’ll be prompted before you overwrite a file. If you’ve ever accidentally overwritten something important, you know what I’m talking about.

If you’re using a lot of aliases, it might make sense to create a separate file. Just pack all your aliases into a new file called ~/.bash_aliases. Now, you need to tell your original configuration file where the aliases live. For that, add the following lines to the top of your ~/.bash_profile or ~/.bashrc:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

And you’re done!

Whenever you’ve edited your bash file, make sure to run the relevant of the following two commands:
source ~/.bash_profile
source ~/.bashrc

This way, you’re telling your system to adhere to your changes from now on. If you open a new window, it will source the file automatically.

2. Pimp your prompt

Your command line prompt is the first thing you’ll see when you start working, and probably the last thing you look at before you leave. So it makes sense to tailor it to your preferences.

I’m a minimalist, so I only include the current directory in my prompt. Therefore, in my ~/.bash_profile I’ve specified:
PS1='[\W]$ '
Here, the \W refers to the current directory, and PS1 is the variable that refers to the prompt.

Other popular options are:

  • A time stamp helps you trace back your work. Add \@ in your file.
  • Adding your username and hostname make sense if you’re working on a remote server. Add \u and / or \h.
  • You could add the shell and the shell version if it’s relevant for your work. Add \s and / or \v to your file.
  • A dollar sign $ usually marks the end of the prompt.

You can also print your prompt in colors. This could help when you’re generating lots of output (or error messages…) and you want to see where the program started.

3. Make the most of your history

Of course you don’t want to type the same commands over and over again. Of course there’s the tab completion — start typing a command, then hit tab to auto-complete it. But what if want to access your past commands? There are a few options:

  • The up and down arrows let you navigate through your recent history.
  • If you want to re-execute your last command, type !!.
  • If you want to re-execute the last command starting with foo, type !foo. Assume for example that the last time I used my favorite text editor, I opened my configuration file: vim ~/.bash_profile. The next time, I’ll just type !vim.
  • If you want to access the argument of the previous command, you can use !$. Say I just opened my configuration file with vim, but now I want to use a different text editor. Then nano !$ is enough.
  • What if you remember the middle part of a long command, but not the first letters? You can search for the command using ctrl+R and then typing the letters that you know. Once you’ve found the command, hit enter as usual.
  • If you want to see the last 500-or-so commands that you’ve made, just type history. You can change the number of commands that get stored in the history to, say, one million entries by adding HISTSIZE=1000000 and HISTFILESIZE=1000000 to your shell configuration. If you want to remove your entire history, type rm ~/.bash_history.

4. Use your environment efficiently

You’ve already encountered a few environment variables — PS1, HISTSIZE and HISTFILESIZE. In general, these are variables written in CAPITAL LETTERS that define important properties of the system.

You can access the complete list of them with the set command. Another example (of many) is SHELLOPTS. It lists all the programs that are set to ‘on’ upon startup of your terminal session. A full documentation of the preset variables can be found in the GNU documentation.

5. Profit from shell options
You can customize your shell in a number of ways with shell options. To display all options, run the following commands in your terminal:
bash -O
bash -o

The option -O refers to options that are specific to the bash shell, while -o refers to all other options.

In the displayed list, you’ll also see whether an option is toggled on or off. You can change the default setting by adding a line in your configuration file. Some handy examples are:
# Correct spelling
shopt -q -s cdspell
# Cd into directory with only its name
shopt -s autocd
# Get immediate notification of background job termination
set -o notify

The first option listed here makes the shell more robust to typing mistakes. The second saves you from typing cd every time you want to change your directory. And if you have a lot of background jobs running, you might want to get notified by adding the third option to your environment file.

6. Find files by name

Say you’re searching for the file foo.txt, but you have no idea where you’ve put it. Then from your home directory, type:
find . -name foo.txt

Here, the . stands for the current working directory, and you specify the file name with the option -name. You can also use wildcards. For example, this command will return all files in txt format:
find . -name *.txt

7. Search for files by content

Say you want to search for all occurrences of bar in foo.txt. Then grep is your friend:
grep bar foo.txt

If you want search multiple files, you can add them like this:
grep bar foo.txt foo2.txt foo3.txt

And if you want to search for bar in all files of the directory dirfoo, you can use the recursive mode:
grep -r bar dirfoo

For more options, you can check out the manual page. Just hit man grep in your terminal.

8. Deal with lots of output

Sometimes you’ll want to grep for something, but the output is too lengthy to be printed on the screen. That’s where piping with the symbol | comes in handy.

Say for example that there are a thousand files in the directory dirfoo that contain bar. You might want to sift through the output and cherry-pick the files that interest you. Then you can type:
grep -r bar dirfoo | less

This will show the output in a more digestible way. You can then take note of the files you want, and close the display by typing q. This will leave your command line interface uncluttered.

You can also use grep in the inverse way. Say you want to run a program fooprog which produces a lot of output. But you’re only interested in the part that contains bar.

To make this output more understandable, you might want to add the three lines preceding each occurrence of bar, and the five lines following it. You can then type:
./fooprog | grep -B3 -A5 -i bar

Here, -B3 refers to the three lines before bar, and -A5 to the five after it. This way, you can ensure that you only get meaningful information printed in your terminal.

9. Move around in text

You thought your terminal was all about keystrokes? Well, here’s one: you can alt-click in the middle of a line in your terminal. It’s a bit clunky, but it’ll be enough to impress your friends.
What will save you tons of time, though, are keyboard shortcuts. To get started, I suggest you start adopting the following:

  • Hop to the beginning of a line with ctrl+a.
  • Hop to the end of a line with ctrl+e.
  • Delete everything from the beginning of the line until the cursor with ctrl+u.
  • Delete everything from the cursor to the end of the line with ctrl+k.
  • You can adopt other shortcuts using the full list of Apple’s keyboard shortcuts. It works pretty well on a Linux command line, too.
    Of course you can’t learn all keyboard shortcuts at once. I suggest starting with a couple of them and then moving on to the next couple once you’ve mastered the first.
    The bottom line: Get fast, become amazing.
    You’ve learnt how customize your terminal with aliases, the prompt, environment variables and shell options. You can now access its history, sift through vast amounts of files and navigate to the meaningful parts.
    Coding is an ongoing learning process. It’s never too late to get efficient!

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with