Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

Is there a practical purpose that this might be commonly used for?

share|improve this question
7  
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 13 hours ago
5  
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). – Stephen Kitt 11 hours ago
up vote 27 down vote accepted

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

share|improve this answer
    
Swapping words was something new & very helpful – Mongrel 1 hour ago

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.

share|improve this answer

This key combination, a binding inherited from the emacs 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 be 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,
  • Ctrl+F, for forward, moves the cursor right one position,
  • Alt+B, for backward, moves the cursor left one word,
  • Alt+F, for forward, 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, for delete, 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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.