3 Terminal Commands You Should Use

Edwin Jose Palathinkal
Xendit Engineering
Published in
3 min readJun 28, 2021

The command-line interface (CLI) is considered intimidating, but it can save time by letting us automate mundane tasks. One of the main reasons GUI is still preferred to CLI is because of the misconception that there is a lot of repetitive typing to do. Typing the same command over and over again can be tedious indeed. Most of us are aware of using the arrow key to browse through older commands. We will be exploring a few other ways to run older commands with fewer keystrokes.

Another reason why CLI is disliked is because as the commands get long, they wrap around the screen into the next line, and it is hard to edit them like you would edit the text in a text editor i.e. using arrow keys.

sudo !!

TL;DR: Run the last command as root. Useful when you forget to use sudo for a command.

If you don’t know what sudo is, sudo is a command to run another command with the security privileges of another user. It is often used to assume the privileges of the system administrator. You may have already used it to install programs on your machine.

A common mistake CLI users make is that we forget to type sudo before a command and then later regret having to type the entire command again with sudo at the start. There are many ways to avoid typing the entire command. However, the quickest way to run the last command as system administrator is to use sudo !! as shown in the video below:

Ctrl-x Ctrl-e

TL;DR: Instantly turn on your favorite text editor to edit a long command.

Another common annoyance when using the CLI is when typing a long command e.g. a curl command to make HTTP requests, you wish you had a text editor to edit the command. Often people edit their command in a text editor and copy-paste it to the terminal. There is a better way to do it if you have already set the $EDITOR environment variable to your favorite text editor (e.g. Visual Studio Code). You can simply press Ctrl-x followed by Ctrl-e as shown below, and a text editor will appear with your current partially complete command already in it, you can then edit your command in the text editor, save and exit the editor, and the command you saved will be automatically copied and paste into the terminal. See below:

Note: In the latest macOS, the default Terminal runs zsh instead of bash. So to enjoy the above shortcut you need to add the following into your .zshrc

# Enable Ctrl-x-e to edit command line
autoload -U edit-command-line
# Emacs style
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line

Ctrl-r

TL;DR: Search your command history using keywords.

If you are a veteran CLI user, you might be familiar with using an arrow key to find older commands. However, there is a quicker way to find older commands using the Ctrl-r. It offers you a autocomplete type search interface as shown below. As you type parts of the old command and press Ctrl-R over and over again, it lets you browse through your command line history only showing commands similar to what you already typed. See the video below for a better view:

Note: In the latest macOS, the default Terminal runs zsh instead of bash. So to enjoy the above shortcut you need to add the following into your .zshrc

bindkey -v
bindkey '^R' history-incremental-search-backward

--

--