56

I noticed bash has a short cut for ctrl+T which swaps the last two characters before the cursor.

I'm wondering why the engineers decided to include this.

  • Was it inherited from a previous convention?
  • Or is there a practical purpose that this is commonly used for?
2
  • 17
    Not an answer, but this isn't bash, as such, but readline (which bash uses for input at the prompt). Your bash is using emacs key-bindings; you can change to vi key-bindings if your an Editor Wars partisan ;) sanctum.geek.nz/arabesque/vi-mode-in-bash There's a couple of "cheat-sheets" for readline here: readline.kablamo.org/emacs.html and readline.kablamo.org/vi.html.
    – John N
    Commented Dec 14, 2016 at 19:39
  • 13
    Historically, the Emacs-style command-line editing features were initially developed in Bash, and then turned into a separate library — but that happened before the first release in 1989. The very first entry in the readline ChangeLog hints at this. So originally the shortcut was handled in Bash itself, albeit briefly, before being pulled out into readline — but the shortcut came to Bash from Emacs (I don't know if it was invented in Emacs or came from elsewhere). Commented Dec 14, 2016 at 22:26

5 Answers 5

69

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).

6
  • 5
    Swapping words was something new & very helpful
    – AReddy
    Commented Dec 15, 2016 at 8:07
  • 1
    Providing a numeric argument to Alt+T transposes the word at (or before) the cursor with the n-th word before that word. eg: Alt+2 Alt+T will modify word1 word2 word3 to word3 word2 word1 if performed at word3.
    – crw
    Commented Dec 15, 2016 at 9:38
  • @crw that would be nice but alt+T doesn't swap words on my terminal. I just opens the menu item 'set title'. Commented Feb 2, 2017 at 23:39
  • @PhilipKirkbride that's a shame, your terminal emulator is depriving you of a very useful shortcut... Commented Feb 3, 2017 at 8:47
  • @PhilipKirkbride there should be an option of the terminal emulator to disable menu access keys
    – Joril
    Commented Jan 29, 2019 at 13:20
33

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.

21

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.

7
  • Yes, it's great for those like us who sometimes forget we're typing into a terminal rather than an Emacs; I still get caught out when using Firefox's textarea widgets (like this one now), which are missing these important bindings. :-( Commented Dec 15, 2016 at 11:57
  • Is there a way you could pair up the character and word versions of related commands in your description? It really helps the memory when you recognise that the Alt and Ctrl versions of those keystrokes are related in a consistent way. Commented Dec 15, 2016 at 11:59
  • @TobySpeight GTK+ supports an alternative Emacs-style keybinding theme so you can use some of these keybindings in Firefox. C-t however seems to not do anything. wiki.archlinux.org/index.php/GTK%2B#Emacs_keybindings Commented Dec 15, 2016 at 14:46
  • @TobySpeight have you ever tried It's All Text!? It allows you to edit the contents of textareas in the editor of your choice. Commented Dec 15, 2016 at 21:21
  • 1
    I'm finding the back and forward commands you shared extremely useful. Commented Feb 3, 2017 at 11:53
11

It is vrey useufl for corretcing smiple tpyos wehre yuo haev accidnetally trasnposed piars of lettres.

(Having severe RSI in both wrists, I end up using this a lot, personally...)

1
  • 2
    My eyes are bleeding
    – Basic
    Commented Dec 15, 2016 at 17:46
5

A very quickly fix typos. effects the immediate 2 corrector block before the courser.

If you typed ls- and you want a space between ls & hyphen then you can use Ctrl + T

This works if you have a space after hyphen & then do Ctrl + T

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .