Page Body

Navigate the GNU/Linux Command Line With Keyboard Shortcuts

Keyboard shortcuts can help you better navigate software interfaces. The following sections define shortcuts for the GNU/Linux command line interface (CLI). Mastering these shortcuts will make you a more adept GNU/Linux user and help you efficiently accomplish your tasks.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.

Moving Around

Control+a or Home
Move to the beginning of the line.
Control+e or End
Move to the end of the line.
Alt+b
Move one word back.
Alt+f
Move one word forward.
Control+b
Move one character back.
Control+f
Move one character forward.
Control+xx
Move between the cursor position and the beginning of the line. You can use this shortcut to repeatedly move between the cursor position and the beginning of the line.

Command Editing

Control+u
Cut the text from before the cursor position to the beginning of the line.
Control+k
Cut the text from after the cursor position to the end of the line.
Control+w
Cut the word before the cursor.
Control+y
Paste the buffer contents at the cursor position.
Alt+u
Capitalize every character from the cursor position to the end of the current word.
Alt+l
Lowercase every character from the cursor position to the end of the current word.
Alt+c
Capitalize the character under the cursor position. The cursor will then move to the end of the current word.
Alt+t
Swap the current word with the previous word.
Control+t
Swap the character at the cursor with the character preceding it.
Control+Shift+_
Undo last key press.
Control+h or Backspace
Delete the character before the cursor.
Control+d or Delete
Delete the character under the cursor.
Alt+d
Delete the current word.

Screen Control

Control+l
Clear the screen.
Control+s
Stop output to the screen.
Control+q
Resume output to the screen.

Job Control

Control+z
Suspend the foreground process. From here, you can unsuspend the process by entering fg, or place it in the background by entering bg.
Control+c
Kill the foreground process by sending it a SIGINT signal.
Control+d
Exit the current shell by sending an End-of-file (EOF) marker to bash. This is similar to running the exit command.

Command History

Control+p or Up Arrow
Go to the previous command in the command history.
Control+n or Down Arrow
Go to the next command in the command history.
Alt+r
Revert the changes to a command that you have pulled from the command history.
Control+r
Activate recursive search. Recalls the last command in the command history that matches the characters that you provide. Can be pressed multiple times to move backward in the command history.
Enter Control+Shift+r to move forward in the command history. Enter Escape to exit recursive search with the command that you have found placed onto the command line (but not executed).
Control+o
Run a command found with recursive search and place the next command in the command history on the command line (but not execute it) afterwards.
Control+g
Exit command recursive search mode without running any commands or placing any commands on the command line.

Tab Completion

Tab
Auto-complete files, directories, and binaries.
When you press the Tab key, the shell completes an incomplete input if the continuation can be uniquely identified. For the first word of a command, bash considers all executable programs. Within the rest of the command line, bash considers all files in the current or specified directory.
If several commands or files exist whose names start out equal, bash completes the name as far as possible and then acoustically signals that the command or filename may still be incomplete. Another Tab lists the remaining possibilities.

Emacs and vi Mode

By default, most GNU/Linux distributions CLIs are set to use the emacs-style keys that were previously discussed. You can confirm what settings your system is using by displaying the contents of the SHELLOPTS variable:

$ echo "${SHELLOPTS}"
braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor

If you prefer to use vi-style keys, you can do so with the set command:

$ set -o vi && echo "${SHELLOPTS}"
braceexpand:hashall:histexpand:history:interactive-comments:monitor:vi

Documentation

To learn more about the different style keys that the Bash shell supports, refer to its man page, either on the command line (man bash) or online.

Avatar

Enjoyed this post?

Subscribe to the feed for the latest updates.