Terminal Productivity Tips

The shell has shortcut keys. Ctrl+A and Ctrl+E jump to the start and end of the line. Ctrl+W deletes one word, Ctrl+U deletes to the start of the line, and…

You mistype one letter at the start of a command that fills a whole line, then hold the left arrow while the cursor crawls back to it. There's a faster way. Your shell (such as bash or zsh) has shortcut keys for editing a line, finding old commands, and controlling a running program. The shell also quietly keeps every command you've typed. Shortcut keys are like the number buttons on a TV remote: to go to channel 37, you press 3 and 7, you don't press "channel up" 36 times. In the terminal, to jump to the start of a line, you press Ctrl+A, you don't hold the left arrow.

Editing a line without backspace

These keys work on the line you are typing:

Key What it does
Ctrl+A jump to the start of the line
Ctrl+E jump to the end of the line
Ctrl+W delete one word left of the cursor, back to the previous space
Ctrl+U delete from the cursor to the start of the line
Ctrl+K delete from the cursor to the end of the line
Ctrl+Y paste back the text you deleted last
Tab complete a file name or a command

Text deleted with Ctrl+W, Ctrl+U or Ctrl+K isn't lost: the shell keeps it, and Ctrl+Y pastes it back. Ctrl+U in the table is for bash. In zsh (the default shell on macOS), Ctrl+U deletes the whole line, wherever the cursor is. The table below shows what happens in the Lab. █ is the cursor, and the keys in the table's rows are pressed one after another. Before you look at the second row, guess: the cursor is at the end of git commit -m "fix typo", and you press Ctrl+W. What's left?

Key The line afterwards
(start) git commit -m "fix typo"█
Ctrl+W git commit -m "fix █
Ctrl+Y git commit -m "fix typo"█
Ctrl+A █git commit -m "fix typo"
Ctrl+K █ (empty, all of it kept)
Ctrl+Y git commit -m "fix typo"█

The second row gives the answer. Ctrl+W stops at a space, not at a quote, so only typo" is deleted. The Ctrl+U then Ctrl+Y trick helps when you've typed a long command and remember you need to run another command first: clear the line, run the other command, then paste it back.

History: no retyping old commands

The history is the list of commands you've typed, which the shell keeps. There are three ways to use it:

budi@laptop:~/online-shop$ mkdir /opt/app
mkdir: cannot create directory ‘/opt/app’: Permission denied
budi@laptop:~/online-shop$ sudo !!
sudo mkdir /opt/app
[sudo] password for budi:
budi@laptop:~/online-shop$ cd !$
cd /opt/app
budi@laptop:/opt/app$

Controlling a running program

These keys are for a program that is running:

Key What it does
Ctrl+C stop the running program (the SIGINT signal)
Ctrl+Z pause it (suspend) and return to the prompt; continue with fg
Ctrl+L clear the terminal screen, the line you're typing stays
Ctrl+D on an empty line: exit the shell

The example below pauses ping with Ctrl+Z, then continues it with fg. ^Z marks where Ctrl+Z was pressed:

budi@laptop:~$ ping belajarweb.dev
PING belajarweb.dev (203.0.113.10) 56(84) bytes of data.
64 bytes from 203.0.113.10: icmp_seq=1 ttl=54 time=12.4 ms
^Z
[1]+  Stopped                 ping belajarweb.dev
budi@laptop:~$ fg
ping belajarweb.dev
64 bytes from 203.0.113.10: icmp_seq=2 ttl=54 time=11.9 ms

A program you pause with Ctrl+Z is not closed. It waits as job number 1 ([1]+) until you type fg (continue in front) or bg (continue in the background), as in the Process Management lesson.

tmux at a glance: sessions with names

tmux is a program that runs your terminal inside a session. The session stays alive on the server even when your SSH connection drops. These are the four most basic session commands:

tmux new-session -s main      # make a session called main (short: tmux new -s main)
tmux list-sessions            # list the sessions (short: tmux ls)
tmux attach-session -t main   # go back into the main session (short: tmux attach -t main)
tmux kill-session -t main     # close the main session

The exercise's shortcut dictionary and tmux command parser

In the exercise, you write two JavaScript functions. The first one, describeShortcut, uses a dictionary: an object that pairs each key with its description. When the key isn't in the dictionary, the result is a default text, Unknown shortcut. This is what the dictionary holds:

Key Description in the exercise
Ctrl+R Reverse search history
Ctrl+C Cancel the running process
Ctrl+Z Suspend the process to the background
Ctrl+L Clear the terminal screen
Ctrl+A Move to the start of the line
Ctrl+E Move to the end of the line
!! Repeat the last command

The second function, parseTmuxCommand, reads a tmux command and returns { action, name }. This code splits the command into words:

const tokens = "tmux attach-session -t main".split(" ");
console.log(tokens);           // [ 'tmux', 'attach-session', '-t', 'main' ]
console.log(tokens[1]);        // attach-session
console.log(tokens.indexOf("-t")); // 2, so the name is in tokens[3]

⚠️ Keys that surprise you

TL;DR: The shell has shortcut keys. Ctrl+A and Ctrl+E jump to the start and end of the line. Ctrl+W deletes one word, Ctrl+U deletes to the start of the line, and Ctrl+K to the end of the line. Ctrl+Y pastes back the text you deleted last. Ctrl+R searches the history (the list of commands you've typed) backwards, !! repeats the last command (sudo !! to repeat it with sudo), and !$ is the last word of the previous command. Ctrl+C stops a program, Ctrl+Z pauses it (continue with fg), and Ctrl+L clears the screen. tmux runs your terminal inside a session that stays alive when the SSH connection drops. Its session commands: new-session -s name, list-sessions, attach-session -t name and kill-session -t name.

⚡ Jump along the line

Meaning: On the line you are typing, Ctrl+A moves the cursor to the start of the line, and Ctrl+E to the end. No need to hold the left arrow.

⚡ Delete, then paste back

Guess first: Ctrl+W deletes one word left of the cursor. The cursor is at the end of git commit -m "fix typo", and you press Ctrl+W. What's left?

git commit -m "fix, plus one space. Ctrl+W deletes back to the previous space, not to the quote, so only typo" goes.

Meaning: Text deleted with Ctrl+W, Ctrl+U (delete to the start of the line) or Ctrl+K (delete to the end of the line) is kept by the shell, and Ctrl+Y pastes it back.

⚡ Find an old command

Meaning: Ctrl+R searches the history (the list of commands you've typed) backwards. Type part of the command, and bash shows the newest match. Press Ctrl+R again for an older one, or Enter to run it.

⚡ Repeat it with sudo

Write:

mkdir /opt/app
sudo !!

Meaning: !! is replaced with the last command. So if mkdir /opt/app fails with Permission denied, sudo !! runs sudo mkdir /opt/app. Check what the last command was first, because now it runs as root (the admin).

What you will learn