Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 8Name in the book: Advanced Keyboard Tricks

Work faster with the keyboard

People who work in the shell all day type little. bash uses a library called Readline that gives you keys to move, delete, cut and paste, complete names and reuse old commands. This chapter shows the keys that save the most time.

  • 9 Lessons
  • 10 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Edit, complete and reuse commands fast with the keyboard

8.1

Move the cursor

You do not need the arrow keys. Ctrl and Alt keys jump to the start, the end, or one word.

KeyAction
Ctrl-aGo to the start of the line
Ctrl-eGo to the end of the line
Ctrl-f / Ctrl-bOne character forward / back
Alt-f / Alt-bOne word forward / back
Ctrl-lClear 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.

Commands in this lesson

8.2

Change text

A few keys delete or change text without the Backspace key.

KeyAction
Ctrl-dDelete the character under the cursor
Ctrl-tSwap the character under the cursor with the one before
Alt-tSwap the word under the cursor with the one before
Alt-lMake the rest of the word lower case
Alt-uMake the rest of the word upper case

8.3

Cut and paste (kill and yank)

Readline calls cutting killing and pasting yanking. Killed text goes into a buffer, the kill ring.

KeyAction
Ctrl-kKill from the cursor to the end of the line
Ctrl-uKill from the cursor to the start of the line
Alt-dKill from the cursor to the end of the word
Alt-BackspaceKill from the cursor to the start of the word
Ctrl-yYank (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.

8.4

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 l and press Tab: it becomes ls ls-output.txt (only one name starts with l).
  • ls D + Tab does nothing (two names match). Tab again shows both. Type one more letter.
  • Tab also completes $VAR names, ~user and host names from /etc/hosts.
KeyAction
TabComplete
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.

Commands in this lesson

8.5

Use the history

bash saves your commands in ~/.bash_history. history shows them with numbers. Combine it with grep to find an old command.

history | less
history | grep /usr/bin
   88  ls -l /usr/bin > ls-output.txt
!88
  • !88 runs line 88 of the history again.
  • The variable HISTSIZE sets how many commands are kept (often 1,000).

8.7

Use history expansion

Commands that start with ! take text from the history.

ExpansionMeans
!!The last command
!numberLine number of the history
!stringThe last command that starts with string
!?stringThe last command that contains string
dnf install htop
Error: This command has to be run with superuser privileges
sudo !!
sudo dnf install htop

Watch out

!string runs at once without a check. Use Ctrl-r when you are not sure what will come back.

8.8

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.

script ~/session.txt
Script started, output log file is '/home/me/session.txt'.
ls /etc | head -3
exit
Script done.
less ~/session.txt

8.9

Summary of the chapter

Ctrl-a, Ctrl-e, Ctrl-u, Ctrl-k, Ctrl-y, Tab and Ctrl-r save hours every week.

1Which key finds the last command that contained "nginx"?

Ctrl-r and type nginx.

2A command failed because it needed root. How do you run it again with sudo without typing it?
3Which keys cut the whole line before the cursor and paste it later?

Ctrl-u to cut, Ctrl-y to paste.

Practice

All commands in this chapter