TERMINAL

Working With Processes

  • Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal. This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.
  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.
  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

Controlling the Screen

  • Ctrl+L: Clear the screen. This is similar to running the “clear” command.
  • Ctrl+S: Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
  • Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.

Moving the Cursor

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
  • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
  • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.

Deleting Text

  • Ctrl+D or Delete: Delete the character under the cursor.
  • Alt+D: Delete all characters after the cursor on the current line.
  • Ctrl+H or Backspace: Delete the character before the cursor. (Backspace is 0x08, ^H simply means 0x08 because H is the 8th letter of the alphabet)

Fixing Typos

  • Alt+T: Swap the current word with the previous word.
  • Ctrl+T: Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
  • Ctrl+_: Undo your last key press. You can repeat this to undo multiple times.

Cutting and Pasting

  • Ctrl+W: Cut the word before the cursor, adding it to the clipboard.
  • Ctrl+K: Cut the part of the line after the cursor, adding it to the clipboard.
  • Ctrl+U: Cut the part of the line before the cursor, adding it to the clipboard.
  • Ctrl+Y: Paste the last thing you cut from the clipboard. The y here stands for “yank”.

Capitalizing Characters

  • Alt+U: Capitalize every character from the cursor to the end of the current word, converting the characters to upper case.
  • Alt+L: Uncapitalize every character from the cursor to the end of the current word, converting the characters to lower case.
  • Alt+C: Capitalize the character under the cursor. Your cursor will move to the end of the current word.

Tab Completion

  • Tab: Automatically complete the file, directory, or command you’re typing.

Working With Your Command History

  • Ctrl+P or Up Arrow: Go to the previous command in the command history. Press the shortcut multiple times to walk back through the history.
  • Ctrl+N or Down Arrow: Go to the next command in the command history. Press the shortcut multiple times to walk forward through the history.
  • Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it.

Bash also has a special “recall” mode you can use to search for commands you’ve previously run:

  • Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.
  • Ctrl+O: Run a command you found with Ctrl+R.
  • Ctrl+G: Leave history searching mode without running a command.

emacs vs. vi Keyboard Shortcuts

The above instructions assume you’re using the default keyboard shortcut configuration in bash. By default, bash uses emacs-style keys. If you’re more used to the vi text editor, you can switch to vi-style keyboard shortcuts.

The following command will put bash into vi mode:

set -o vi

The following command will put bash back into the default emacs mode:

set -o emacs


Vi is a powerful text editor included on most Linux systems. Many people swear by vi and find it faster than any other editor once they’ve learned its key bindings. You can even use vi key bindings in Bash.

Mode Switching

As a short recap, vi is a modal editor – there’s an insert mode and a standard command mode. In insert mode, vi functions similar to a normal text editor. In command mode, you take advantage of these key bindings.

  • i – Enter insert mode.
  • Escape – Leave insert mode. If you’re already in command mode, Escape does nothing, so you can press Escape to ensure you’re in command mode.

Moving the Cursor

Vi uses the hjkl keys to move the cursor in command mode. Early computer systems didn’t always have arrow keys, so these keys were used instead. One advantage of these keyboard shortcuts is that you don’t have to move your fingers from the home row to use them.

  • h – Move cursor left.
  • j – Move cursor down.
  • k – Move cursor up.
  • l – Move cursor right.

You can also use search commands to quickly move the cursor.

  • / – Type a / followed by some text you want to find and press Enter to quickly move your cursor to the location of the text in the file. For example, if you have the word iguana in your file, type /iguana and press Enter to quickly move the cursor there.
  • ? – Like /, but searches backwards.
  • f – Type an f followed by any character to quickly move the cursor to the next occurrence of the character on the current line. For example, if you have the line “Hello world” on a line and your cursor is at the beginning of the line, type fo to move to the o in Hello. Type fo again to move to the o in world.
  • F – Like f, but searches backwards.
  • % – Jump between the nearest (), [], or {} characters on the line.

Use these commands to quickly move to locations in the file:

  • H – Move cursor to highest (top) line in file.
  • M – Move cursor to middle line in file.
  • L – Move cursor to lowest (bottom) line in file.
  • #G – Type a number and then type G to go to that line in the file. For example, type 4G and press Enter to move to the fourth line in the file.

Moving between words:

  • w – Move forward a word.
  • #w – Move forward a number of words. For example, 2w moves forward two words.
  • b – Move back a word.
  • #b – Move back a number of words. For example, 3b moves back three words.
  • e – Move to end of the current word.

Copying & Pasting

Vi refers to the act of copying as “yanking.”

  • v – Press v and move the cursor to select a section of text.
  • y – Copy (yank) the selected text.
  • p – Paste at cursor.
  • x – Cuts the selected text. Cuts the character under the cursor if no text is selected
  • r – Type r and then type another character to replace the character under the cursor.

Combining Commands

Some commands – including the y and v commands above and the d (delete) command accept cursor motion commands.

For example, when you press d to delete some text, nothing will happen until you enter a cursor motion command. For example:

  • dw – Deletes the next word.
  • db – Deletes the previous word
  • de – Deletes to the end of the current word.
  • dL – Deletes all text below the cursor in the file.
  • d/unicorn – After pressing Enter, deletes all text between the cursor and the word “unicorn” in the current file.
  • dd – Deletes an entire line.

As you can see, the combination of combining a command with a cursor movement command is very powerful.

Repeat & Undo

Vi’s repeat command is very powerful, as it can repeat complex, combined commands.

  • u – Undo.
  • . – The . repeats the last full command. The insert command also functions as a command here. For example, type iunicorn and press Escape. You can then use the . key to insert the word unicorn at the cursor.

Bonus: Using Vi Key Bindings in Bash

Once you’ve mastered the vi key bindings, you may want to use them elsewhere on your system. No problem – you can set the Bash shell to use vi-style key bindings.

Try this out in the current session by running the following command in a Bash terminal:

set -o vi

Bash will start in insert mode – press Escape to enter command mode and use these key bindings.

If you like this, you can add the command to your ~/.bashrc file and it will be automatically run each time you log in. Use the vi .bashrc command to open and edit the file in vi.