Learn › Linux Foundations › Bash Shell Environment
keyboard - a hands-on Linux lab on a real virtual machine.
Edit the command line at speed with readline keyboard shortcuts (default emacs mode). Ctrl-A and Ctrl-E jump to the ends, Alt-B and Alt-F move by word, Ctrl-U, Ctrl-K, and Ctrl-W delete in chunks, and Ctrl-Y pastes back what you cut. Ctrl-L clears the screen keeping your line, Ctrl-C cancels, and Ctrl-D on an empty line logs you out. These are keystrokes, so the drills teach the commands around them: echo, clear, and set -o emacs.
You type a long command. Halfway through you notice a typo in the very first word. What do you do? If your instinct is to hold the Backspace key and watch the whole line vanish one character at a time, this lesson is about to save you a great deal of that.
The command line is not a scroll you can only add to and erase from the end. It is a text field, and it has a whole set of shortcuts for moving around and editing it fast. Jump to the front. Jump to the end. Wipe a word. Wipe the line. All without ever reaching for an arrow key. These shortcuts turn the prompt from a place you retype into a place you edit, and once they are in your fingers you will use them every single day.
The black boxes in this lesson are a practice terminal: a safe sandbox that checks the one command each step teaches. One honest catch, unique to this lesson: these shortcuts are keystrokes, not something you type, so you cannot enter Ctrl-A into an answer box and no drill grades on a keystroke itself. You will practice the real commands that go AROUND the keys: building a line to edit, clearing the screen, and confirming your editing mode. The keystrokes themselves you learn from the table and press for real on the machine at the end of the module.
These shortcuts do not belong to bash exactly. They belong to a library called readline, the piece of software that draws the line you are typing and handles every edit to it. Bash hands your input line to readline, and readline is what listens for Ctrl-A, Ctrl-E, and the rest.
That detail is not trivia, it is leverage. Because the SAME readline library is used by many other tools, these exact shortcuts work far beyond bash. Open the Python interactive prompt, the psql database shell, and countless other command-line programs, and Ctrl-A still jumps to the start of the line. Learn them once here, use them in a dozen tools for the rest of your career.
Readline was written by Brian Fox, the same person who wrote bash itself, back in the late 1980s. Its default keys copy the old Emacs text editor, which is why this style is called emacs mode. There is a second style, vi mode, that uses different keys, but almost everyone leaves bash in the default emacs mode. This whole lesson assumes that default, which is what you already have.
The first four shortcuts are about MOVING the cursor without holding an arrow key across the whole line. Two of them are the ones you will reach for most often in your life at the terminal.
Ctrl-A jumps the cursor to the very start of the line. Ctrl-E jumps it to the very end. Think of the A as the start of the alphabet and E as the End. Between those two, Alt-B moves back one whole word at a time, and Alt-F moves forward one whole word. (Alt-B and Alt-F: B for back, F for forward.)
Here is the situation these solve. You have typed a long line and the cursor is sitting at the end, but the mistake is at the front:
student@linuxcamp:~$ echoo the quick brown fox jumps█
The █ marks where your cursor is: parked at the far right, a whole line away from that doubled o in echoo. Press Ctrl-A and the cursor leaps to the front in one keystroke, ready to fix the typo:
student@linuxcamp:~$ █echoo the quick brown fox jumps
The █ block is just standing in for your cursor so you can see it move on the page; your real terminal shows a thin blinking bar or a solid box, and its exact look is an example. What matters is the jump: one keystroke carried the cursor from the end of the line to the start, no arrow-key marathon. Press Ctrl-E and it flies back to the end just as fast.
The next shortcuts DELETE, and they are the real time-savers. Instead of holding Backspace, you erase in big, deliberate chunks.
Ctrl-U deletes everything from the cursor back to the START of the line. Ctrl-K deletes everything from the cursor to the END of the line. The pair splits the line at the cursor: Ctrl-U takes the left half, Ctrl-K takes the right. Ctrl-W is the surgical one: it deletes just the single word right before the cursor.
The most common use of all is starting over. You have typed a long line, changed your mind, and want a blank prompt without pressing Enter on a broken command. Put the cursor at the end, press Ctrl-U, and the entire line is gone in one stroke:
student@linuxcamp:~$ git commit -m "a message I no longer want█
After Ctrl-U:
student@linuxcamp:~$ █
Here is the shortcut that turns delete into cut-and-paste. When you delete with Ctrl-U, Ctrl-K, or Ctrl-W, bash does not throw the text away. It saves it. Ctrl-Y pastes back the last thing you deleted. In readline terms, deleting is called killing and pasting it back is called yanking, which is where the Y comes from.
That makes a real move possible. Say you are typing a command and realise you need to run something else FIRST, then come back to this line. Press Ctrl-U to cut the whole line away (it is saved, not lost), run your other command, then press Ctrl-Y to paste the original line right back and carry on. You never retyped it.
Ctrl-W then Ctrl-Y is a quick way to MOVE a word. Ctrl-W cuts the word before the cursor, you move the cursor elsewhere, and Ctrl-Y pastes it there. Small trick, but it shows the idea: every delete you make is really a cut, and Ctrl-Y is the paste.
This is the lesson. Every row is a keystroke, what it does, and the moment you reach for it. Read the WHY column, because that is what puts the key in your fingers:
Two of those keys surprise people the first time. Ctrl-L clears the screen just like the clear command, but with one gift: it keeps whatever you have already typed on the current line. So you can tidy a messy screen without losing your half-written command. And Ctrl-D on an empty prompt closes the shell and logs you out. That catches nearly everyone once: they mean to delete something, the line is already empty, and the terminal vanishes. On an empty line, Ctrl-D means end of input, and end of input ends the session.
Time to make something real to practise on. The keystrokes need a line under them, so type one out with echo. echo simply prints back the words you give it, and it makes a perfect long line to jump around inside. Type this exactly:
echo the quick brown fox
prompt: student@linuxcamp:~$ answer: echo the quick brown fox output: the quick brown fox hint: Type the word echo, a space, then the four words: echo the quick brown fox
On the real machine, the interesting part happens BEFORE you press Enter. With that line sitting on the prompt, this is your moment to press Ctrl-A to jump to the front, Alt-F to hop word by word, or Ctrl-U to wipe it and start over. Here in the sandbox we grade the typed command; the keystrokes are yours to press on the live terminal at the end of the module.
One shortcut in the table has an exact command version you CAN type and grade here, which makes it a clean way to feel the pair. Ctrl-L clears the screen, and so does the clear command. The difference is only that Ctrl-L keeps your half-typed line while clear runs as its own command, but the screen result is the same: a fresh, empty terminal.
Run the command form now:
clear
prompt: student@linuxcamp:~$ answer: clear output: hint: One word, five letters, no options: clear
clear prints nothing you can see: it wipes the screen and drops your prompt back at the top, which is why the box above shows no output. That is the same thing Ctrl-L does, and now you know both the keystroke and its command twin. On a cluttered screen mid-command, reach for Ctrl-L so you keep your line; when the line is empty, either one works.
Scaffolding off. No command is printed this time. You have every piece you need.
This whole lesson assumed you are in the default emacs mode, the editing style where Ctrl-A and Ctrl-E and all the rest work as taught. Bash lets you set that mode explicitly with set -o followed by the mode name. Suppose you want to be certain your shortcuts will behave: type the command that turns emacs editing mode ON, so the keys in the table are guaranteed to work.
prompt: student@linuxcamp:~$ answer: set -o emacs output: hint: The shell builtin is set, then the option flag -o, then the mode name from this lesson: emacs.
The command is set -o emacs. set is a shell builtin that turns options on and off, -o names an option by word, and emacs is the editing mode that gives you every shortcut in this lesson. It prints nothing, because setting an option that is already on just quietly confirms it. If you ever sit at a shell where Ctrl-A does something strange instead of jumping to the start, you are probably in vi mode, and set -o emacs puts you back. You recalled it from what you needed it to do, which is the recall the real machine will ask of you.
You earned this cheat sheet. The keystrokes are the stars; the commands are how you set the stage and prove the mode:
And the one idea to keep: the command line is a text field, not a one-way scroll. You can jump to either end, cut in chunks, and paste back what you cut, all from the keyboard. Emacs mode is the default, and set -o emacs guarantees it. Ctrl-D on an empty line is the one to respect: it ends the session.
Because these are readline keys, they follow you out of bash. The next time you are in the Python prompt or a database shell like psql and you need to jump to the start of a long line, try Ctrl-A. It works. Muscle memory you build here pays off across nearly every command-line tool you will ever touch.
The practice terminal has shown you the shape of line editing. You built a line with echo, you cleared the screen with clear (the command twin of Ctrl-L), and you pinned the editing mode with set -o emacs. The keystrokes you press; the commands around them you typed yourself.
The Bash Environment module ends with one real Linux machine, the Bash Environment capstone mission, and that is where you press these keys for real, on lines you type and edit at speed. A VM boots just for you. It hands you objectives that use exactly what you practised across this module: variables, aliases, the PATH, and now editing the command line without the arrow keys. One difference from here: the mission shows no commands. You read the objective, you recall the move, you make it. That recall is what makes it stick.
Finish the other Bash Environment lessons, then go edit a real command line with nothing but your fingers on the home row.
Practice Bash Keyboard Shortcuts in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.