Bash keyboard shortcuts

2017-05-07|Categories: Linux|Tags: |

Moving the cursor

Keystroke Meaning
Ctrl + a Go to the beginning of the line (Home)
Ctrl + e Go to the End of the line (End)
Ctrl + p Previous command (Up arrow)
Ctrl + n Next command (Down arrow)
Alt + b Back (left) one word
Alt + f Forward (right) one word
Ctrl + f Forward one character
Ctrl + b Backward one character
Ctrl + xx Toggle between the start of line and current cursor position

Editing

Keystroke Meaning
Ctrl + l Clear the Screen, similar to the clear command
Ctrl + _ Undo
Alt + Del Delete the Word before the cursor
Alt + d Delete the Word after the cursor
Ctrl + d Delete character under the cursor
Ctrl + h Delete character before the cursor (Backspace)
Ctrl + w Cut the Word before the cursor to the clipboard
Ctrl + k Cut the Line after the cursor to the clipboard
Ctrl + u Cut/delete the Line before the cursor to the clipboard
Ctrl + y Paste the last thing to be cut (yank)
Alt + t Swap current word with previous
Ctrl + t Swap the last two characters before the cursor (typo)
Esc + t Equal to Alt + t
Alt + u UPPER capitalize every character from the cursor to the end of the current word
Alt + l Lower the case of every character from the cursor to the end of the current word
Alt + c Capitalize the character under the cursor and move to the end of the word
Alt + r Cancel the changes and put back the line as it was in the history (revert)
Tab Tab completion for file/directory names
For example, to move to a directory 'sample1':

  1. type cd sam;
  2. then press Tab and Enter.

Type just enough characters to uniquely identify the directory you wish to open.

Special keys: Tab, Backspace, Enter, Esc

Text terminals send characters (bytes), not key strokes.

Special keys such as Tab, Backspace, Enter and Esc are encoded as control characters. Control characters are not printable, they display in the terminal as ^ and are intended to have an effect on applications.

  • Ctrl + I = Tab
  • Ctrl + J = Newline
  • Ctrl + M = Enter
  • Ctrl + [ = Escape

Many terminals will also send control characters for keys in the digit row:

  • Ctrl + 2^@
  • Ctrl + 3^[ Escape
  • Ctrl + 4^\
    • ^\ sends the quit signal (SIGQUIT), which conventionally tells the application to exit as soon as possible without saving anything; many applications don't override the default behavior, which is to kill the application immediately, and traditionally generate a core dump, but many systems disable that by default nowadays. (http://unix.stackexchange.com/questions/226327/what-does-ctrl4-and-ctrl-do-in-bash)
    • One day, I used ssh -X to log in a RHEL 7.0 server, then I ran firewall-config, but the GUI can't be started and the terminal has been hung! For terminating the command, I tried exit, Ctrl-c, Ctrl-d, <Enter>~.<Enter>, but they all can't work. At last, Ctrl+\ ( = Ctrl+4 ) works.
  • Ctrl + 5^]
  • Ctrl + 6^^
  • Ctrl + 7^_ Undo
  • Ctrl + 8^? Backward-delete-char

Ctrl+v tells the terminal to not interpret the following character, so Ctrl+v Ctrl-I will display a Tab character, similarly Ctrl+v Enter will display the escape sequence for the Enter key: ^M.

History

Keystroke / Input Meaning
Ctrl + r Searches the command history as you type
Ctrl + p Previous command in history (i.e. walk back through the command history)
Ctrl + n Next command in history (i.e. walk forward through the command history)
Ctrl + s Go back to the next most recent command
Not execute it from a terminal because this will also launch its XOFF
Ctrl + o Execute the command found via Ctrl+r or Ctrl+s
Ctrl + g Escape from history searching mode
!! Repeat last command
!abc Run last command starting with abc
!abc:p Print last command starting with abc
!^ First argument of previous command
!$ Last argument of previous command
Alt + . Last argument of previous command
!* All arguments of previous command
!:n The nth argument of previous command
!:0 The 0th argument of previous command, it's the command name.
for example, previous command is echo hello world, then !:0 will get echo
!n The nth command in history
!n:m The mth argument of the nth command in history
^abc­^­def Run previous command, replacing abc with def
!:gs/abc/def/ Run previous command, replacing all abc with def

Process control

Keystroke Meaning
Ctrl + c Interrupt/Kill whatever you are running (SIGINT)
Ctrl + s Stop output to the screen (for long running verbose commands),
then use PgUp/PgDn for navigation
Ctrl + q Allow output to the screen (if previously stopped using command above)
Ctrl + d Send an EOF marker, unless disabled by an option,
this will close the current shell (EXIT)
Ctrl + z Send the signal SIGTSTP to the current task, which suspends it.
To return to it later enter fg 'process name' (foreground).

Emacs mode vs Vi mode

All the above assume that bash is running in the default Emacs setting, if you prefer this can be switched to Vi shortcuts instead.

Set Vi Mode in bash:
$ set -o vi

Set Emacs Mode in bash:
$ set -o emacs

Call Vim to edit a long command line

If you are reasonably used to working with the modeless Emacs for most of your shell work but do occasionally find yourself in a situation where being able to edit a long command line in vi would be handy, you may prefer to press Ctrl+X Ctrl+E to bring up the command line in your $EDITOR instead, affording you the complete power of Vim to edit rather than the somewhat sparse vi emulation provided by Readline. If you want to edit a command you just submitted, the fc (fix command) Bash builtin works too.

Leave A Comment