Intended use of ctrl+T in bash?

It's very useful to quickly fix typos:

sl

becomes

ls

with a single CtrlT.

You can use AltT to swap words too (e.g. when switching between service and systemctl...).

Historically speaking, the CtrlT feature came to Bash from Emacs in all likelihood. It probably was copied to Emacs from some other editor; it was present in Stanford's E editor (see Essential E page 13) by 1980, and E had a strong impact on Richard Stallman (as described in Free as in Freedom). It was implemented in very early versions of Bash, before its first release in 1989, when it was pulled out into the readline library where it lives today (the very first entry in the readline ChangeLog hints at this).


This is inherited (by readline) from GNU Emacs, which uses control-T for transposing characters:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Transpose.html

Note that bash's line editor defaults to Emacs mode, but you can also switch it to vi mode, if you prefer.


This key combination, a binding inherited from the emacs text editor, causes the last 2 characters typed to be swapped at the end of the line, used in the middle of a line, it swaps the character at the left of the cursor and the one under the cursor.

It may seem vain to have a binding for such a seldom used feature that is easily achieved with a few more key strokes. Old timers such as I use it quite often and it used to save some transmission time back in the days of 300 baud modems, in the late '70s, especially in the middle of long lines.

A similar and more useful command, bound to Alt+T, transposes the words at the left and at the right of the cursor.

T was chosen because it is the initial letter of transpose. Other bindings with similar origin include:

  • Ctrl+B, for backward, moves the cursor left one position,
    • Alt+B moves the cursor left one word,
  • Ctrl+F, for forward, moves the cursor right one position,
    • Alt+F moves the cursor right one word,
  • Ctrl+A, for Anfang or ante, moves the cursor to the beginning of the line,
  • Ctrl+E, for end, moves the cursor to the end of the line,
  • Ctrl+N, for next, retrieves the next line,
  • Ctrl+P, for previous, retrieves the next line,
  • Ctrl+D, for delete, deletes the character under the cursor,
    • Alt+D deletes the word under the cursor,
  • Ctrl+K, for kill, cuts the end of the line,
  • Ctrl+Y, for yank, pastes the contents of the clipboard,

These bindings, implemented in the GNU readline package, are therefore available in all programs that use it for user input, such as bash, but also gdb, bc, ...

Some of the are also available in other environments: The Firefox URL input line, text input fields in the OS/X graphical interface, and many X-based window managers.

vim users can select the corresponding bindings via an environment variable.