Editing and completing commands
On this page
In the last lesson, you typed every command letter by letter. That works for ls. It gets slow with a line like ls -l Documents/notes.txt, and every letter is a chance for a typo. One wrong letter in a name, and you get an error and type the whole line again.
You do not have to. Bash can finish names for you, give back commands you already ran, and let you fix a line before it runs. By the end of this lesson, you type less and correct more.
Before you start
Section titled “Before you start”- Command anatomy (B04.1): command names, options, and arguments.
- The
~/labpractice directory from that lesson (setup). If you opened another terminal, runcd ~/labfirst.
Let Bash finish the name
Section titled “Let Bash finish the name”Type ls -l Doc, but do not press Enter. Press the Tab key instead:
| You type, then press Tab | The line becomes |
|---|---|
ls -l Doc |
ls -l Documents/ |
Bash looked in the current directory, found exactly one name that starts with Doc, and typed the rest for you. This is called completion.
Look at the end of the line. Bash added /, because Documents is a directory: you can go on typing a name inside it. After a file name, Bash adds a space instead, ready for the next word.
Nothing has run yet. Tab only types; Enter runs.
Completion works for command names too. Guess first: you type hea and press Tab. What happens?
Run it, then compare with your guess
| You type, then press Tab | The line becomes |
|---|---|
hea |
head |
-n 1 to |
head -n 1 todo.txt |
On the lab system, head is the only command that starts with hea, so Bash completes it. A second Tab, after to, completes the file name todo.txt. Press Enter, and the completed command runs:
Install UbuntuWhen Tab does nothing
Section titled “When Tab does nothing”Documents holds two files. Type cat Documents/ and press Tab. (cat prints the contents of a file.)
| You type, then press Tab | The line becomes |
|---|---|
cat Documents/ |
cat Documents/ (no change) |
Nothing happened, and you may hear a beep. Tab is not broken. Two names match, notes.txt and report.txt, and Bash does not guess which one you mean.
Press Tab a second time, and Bash lists the matching names below the line, then shows your line again so you can keep typing. Type enough letters to make the name unique, and Tab finishes it:
| You type, then press Tab | The line becomes |
|---|---|
r |
cat Documents/report.txt |
Quarterly lab reportCompletion only offers names that exist, and it matches capital letters exactly. So when Tab adds nothing even on the second press, it is telling you something: check the spelling and the case of what you typed so far.
Bring back a command you already ran
Section titled “Bring back a command you already ran”Bash keeps a list of the commands you ran, called the history. You move through it with the arrow keys:
- Up replaces the current line with the previous command. Each further Up goes one command further back.
- Down moves forward again. Past the newest command, Down gives you an empty line.
After running ls Documents and then cat todo.txt, the keys give:
| You press | The line becomes |
|---|---|
| Up | cat todo.txt |
| Up | ls Documents |
| Down | cat todo.txt |
| Down | (empty line) |
A recalled command is an ordinary line. Press Enter to run it unchanged, or change it first.
Put it together
Section titled “Put it together”The replay below combines both tricks. It lists Documents in long form, then recalls that command and narrows it to one file, without retyping anything.
learner@rootvolt:~/lab$ ls -l Documents/total 8
-rw-r--r-- 1 learner learner 40 Oct 1 09:00 notes.txt
-rw-r--r-- 1 learner learner 21 Oct 1 09:00 report.txtlearner@rootvolt:~/lab$ ls -l Documents/notes.txt -rw-r--r-- 1 learner learner 40 Oct 1 09:00 Documents/notes.txtlearner@rootvolt:~/lab$ Files ~/lab
- ~
- lab/cwd
- Documents/
- notes.txt-rw-r--r--
- report.txt-rw-r--r--
- projects/
- todo.txt-rw-r--r--
History what Up recalls
ls -l Documents/ls -l Documents/notes.txtsaved
Steps (10)
- Type
ls -l Doc— Type the start of the folder name - Press Tab: the line becomes
ls -l Documents/— Only Documents matches, so Bash completes it - Press Enter
- Output:
total 8 -rw-r--r-- 1 learner learner 40 Oct 1 09:00 notes.txt -rw-r--r-- 1 learner learner 21 Oct 1 09:00 report.txt - Press Up: the line becomes
ls -l Documents/— Bring back the previous command - Type
no— Extend the argument with the start of a file name - Press Tab: the line becomes
ls -l Documents/notes.txt— Bash completes notes.txt and adds a space - Press Enter
- Output:
-rw-r--r-- 1 learner learner 40 Oct 1 09:00 Documents/notes.txt - Tab finishes names, Up recalls a command, and nothing runs until you press Enter.
In words: typing ls -l Doc and pressing Tab gives ls -l Documents/. Enter lists the two files. Up brings back ls -l Documents/. Typing no and pressing Tab extends the argument to Documents/notes.txt, and Enter shows the details of that one file.
Count the keys for the second command: Up, n, o, Tab, and Enter. Five key presses, instead of typing the 25 characters of ls -l Documents/notes.txt and then Enter.
Fix the line before it runs
Section titled “Fix the line before it runs”You can change any part of the line before you press Enter. The part of Bash that reads what you type, called Readline, gives you these keys:
| Key | What it does |
|---|---|
| Left, Right | Move the cursor one character |
| Ctrl+A | Move the cursor to the start of the line |
| Ctrl+E | Move the cursor to the end of the line |
| Backspace | Delete the character before the cursor |
| Enter | Run the whole line, wherever the cursor is |
| Ctrl+C | Discard the line without running it |
Ctrl+A means: hold Ctrl and press A.
Here is a situation they solve. You typed todo.txt and then noticed that you forgot the command name. Do not delete the line. Text you type goes in at the cursor, so move the cursor to the start and add the missing word:
| You press or type | The line becomes |
|---|---|
| Ctrl+A | todo.txt, with the cursor before t |
cat |
cat todo.txt |
| Ctrl+E | cat todo.txt, with the cursor at the end |
| Enter | runs cat todo.txt |
Install UbuntuVerify the ISO checksumTake a snapshotEnter runs the whole line no matter where the cursor is, so the Ctrl+E step is optional. It is there to show the key.
To abandon a line completely, press Ctrl+C. Bash prints ^C, the command does not run, and a new prompt appears:
learner@rootvolt:~/lab$ head -n 5 todo.txt^CIn lesson B03.4, Shutdown, restart, and session recovery, you used the same key to stop a running command and to abandon an unfinished line at the > prompt.
Mistakes to recognize
Section titled “Mistakes to recognize”Using Ctrl+C to copy. In a terminal, Ctrl+C discards the line or stops the running command; it does not copy. Use the copy and paste keys from lesson B03.4, Shift+Ctrl+C and Shift+Ctrl+V.
Running a recalled line without reading it. Up shows you the old command, and Enter runs it again exactly as written. Read the line before you press Enter. This matters more once later modules introduce commands that change or delete files.
Expecting another terminal’s history. Each terminal window runs its own Bash, as you saw in lesson B03.2, and each one keeps its own history while it runs. Bash writes the history to the file ~/.bash_history when the shell exits, and reads that file when a new shell starts. So commands from a terminal that is still open do not appear when you press Up in another one.
Security Angle
Section titled “Security Angle”The history file is plain text in your home directory. If you type a password on the command line, for example as an option value, it is saved there. It also comes back when anyone at your keyboard presses Up. When a program offers to ask for the password instead, let it ask, so the password never becomes part of a command line.
Ubuntu’s default ~/.bashrc sets HISTCONTROL=ignoreboth, which also means that a line starting with a space is not saved. Other systems may not set this, so treat it as a convenience, not a protection.
Try it yourself
Section titled “Try it yourself”Goal: Change a value in the middle of a recalled command without retyping it.
- Run
head -n 1 todo.txt. - Bring the command back, change it so that it prints three lines, and run it.
Expected: The three lines of todo.txt.
Hint
The value 1 is in the middle of the line, so editing at the end does not help. Move the cursor until it is right after 1, either with Left from the end or with Ctrl+A and then Right from the start. Backspace deletes the character before the cursor.
Solution
- Press Up. The line is
head -n 1 todo.txt. - Press Left nine times. The cursor is now right after
1. - Press Backspace. The line is
head -n todo.txt. - Type
3, then press Enter.
Install UbuntuVerify the ISO checksumTake a snapshot- Tab completes a command or file name when only one name matches. When several match, a second Tab lists them.
- Up and Down move through the commands you ran before. A recalled command can be changed before you run it.
- Left and Right move the cursor one character, Ctrl+A jumps to the start of the line, and Ctrl+E to the end. Backspace deletes the character before the cursor.
- Nothing runs until you press Enter. Ctrl+C discards the line instead.
Next, in Finding trustworthy help, you find where each program documents its options, so you can look them up instead of guessing.
Further reading
Section titled “Further reading”- GNU Bash Reference Manual, Readline chapter: Commands For Moving, Commands For Manipulating The History, and Letting Readline Type For You (completion).
- GNU Bash Reference Manual: Bash History Facilities, where the history is kept and when it is saved.
man bash, sections READLINE and HISTORY, or the Ubuntu 24.04 bash(1) page.- GNOME Terminal Help: Copy and paste text.