$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Edit, complete and reuse commands fast with the keyboard
Move the cursor
You do not need the arrow keys. Ctrl and Alt keys jump to the start, the end, or one word.
| Key | Action |
|---|---|
Ctrl-a | Go to the start of the line |
Ctrl-e | Go to the end of the line |
Ctrl-f / Ctrl-b | One character forward / back |
Alt-f / Alt-b | One word forward / back |
Ctrl-l | Clear the screen (like clear) |
Note
"Alt" is the Meta key in the Readline documents. If Alt does not work, press Esc and then the letter.
Change text
A few keys delete or change text without the Backspace key.
| Key | Action |
|---|---|
Ctrl-d | Delete the character under the cursor |
Ctrl-t | Swap the character under the cursor with the one before |
Alt-t | Swap the word under the cursor with the one before |
Alt-l | Make the rest of the word lower case |
Alt-u | Make the rest of the word upper case |
Cut and paste (kill and yank)
Readline calls cutting killing and pasting yanking. Killed text goes into a buffer, the kill ring.
| Key | Action |
|---|---|
Ctrl-k | Kill from the cursor to the end of the line |
Ctrl-u | Kill from the cursor to the start of the line |
Alt-d | Kill from the cursor to the end of the word |
Alt-Backspace | Kill from the cursor to the start of the word |
Ctrl-y | Yank (paste) the last killed text |
At work
You typed a long command and need to check something first? Ctrl-u kills the line, run the other command, then Ctrl-y brings the long line back.
Complete names with Tab
Press Tab and the shell completes a file name, command, variable, user or host name. Press Tab twice to see all choices.
ls
Desktop Documents ls-output.txt Music Pictures Videos
ls l
ls ls-output.txt
ls D
Desktop/ Documents/- Type
ls land press Tab: it becomesls ls-output.txt(only one name starts withl). ls D+ Tab does nothing (two names match). Tab again shows both. Type one more letter.- Tab also completes
$VARnames,~userand host names from/etc/hosts.
| Key | Action |
|---|---|
| Tab | Complete |
Tab Tab or Alt-? | Show all possible completions |
Alt-* | Insert all possible completions |
Note
Programmable completion (package bash-completion) also completes options, Git branches, service names and more. complete | less shows the rules.
Use the history
bash saves your commands in ~/.bash_history. history shows them with numbers. Combine it with grep to find an old command.
Search the history with Ctrl-r
Ctrl-r starts a live search back in the history. Type a few letters; the newest match appears.
| Key | Action |
|---|---|
Ctrl-r | Search back (press again for an older match) |
| Enter | Run the found command |
Ctrl-j | Copy the found command to the line, to edit it first |
Ctrl-g or Ctrl-c | Stop the search |
Ctrl-p / Ctrl-n | Previous / next command (like Up / Down) |
Alt-< / Alt-> | First / last line of the history |
Ctrl-o | Run the current line and show the next one |
(reverse-i-search)`/usr/bin': ls -l /usr/bin > ls-output.txtUse history expansion
Commands that start with ! take text from the history.
| Expansion | Means |
|---|---|
!! | The last command |
!number | Line number of the history |
!string | The last command that starts with string |
!?string | The last command that contains string |
dnf install htop
Error: This command has to be run with superuser privileges
sudo !!
sudo dnf install htopWatch out
!string runs at once without a check. Use Ctrl-r when you are not sure what will come back.
Record a session with script
script saves everything you type and see in a file. Good for notes and for showing others what you did.
Summary of the chapter
Ctrl-a, Ctrl-e, Ctrl-u, Ctrl-k, Ctrl-y, Tab and Ctrl-r save hours every week.