0

I often mix up characters when entering commands in the command line, like gitp ush or c d-. I'd like to be able to tap up to recall the line and press a couple of keys to swap the offending characters. I can delete and retype one of the characters, of course, but it would be nice to make these mistakes even less annoying to fix if I can.

Is there some way to transpose two characters when typing commands into the terminal?

I'm usually on Ubuntu using the GUI terminal application, but I'm also wondering if there's an answer that's agnostic to how the shell is being run.

  • In vi-mode you can use xp. – jasonwryan Sep 21 '17 at 21:10
  • 2
    Just drink less coffee – xhienne Sep 21 '17 at 21:21
4

If you use Vi editing mode in the shell, then swapping two characters is easily done by just typing xp in normal mode ("command mode", i.e. after pressing Esc). This literally means "erase the character under the cursor" followed by "paste the previously yanked text (the deleted character) after the cursor".

This will work in all sh shells. Vi mode is descibed in the POSIX standard.

In Emacs editing mode, the same operation is often bound to Ctrl+T (T for "transpose").

There is no completely shell agnostic way to do this. The above works in bash, ksh93, and zsh, but the Emacs command line editing mode is not standard (as in POSIX standard).

This is from the rationale section of the above linked page:

In early proposals, the KornShell-derived emacs mode of command line editing was included, even though the emacs editor itself was not. The community of emacs proponents was adamant that the full emacs editor not be standardized because they were concerned that an attempt to standardize this very powerful environment would encourage vendors to ship strictly conforming versions lacking the extensibility required by the community. The author of the original emacs program also expressed his desire to omit the program. Furthermore, there were a number of historical systems that did not include emacs, or included it without supporting it, but there were very few that did not include and support vi. The shell emacs command line editing mode was finally omitted because it became apparent that the KornShell version and the editor being distributed with the GNU system had diverged in some respects. The author of emacs requested that the POSIX emacs mode either be deleted or have a significant number of unspecified conditions. Although the KornShell author agreed to consider changes to bring the shell into alignment, the standard developers decided to defer specification at that time. At the time, it was assumed that convergence on an acceptable definition would occur for a subsequent draft, but that has not happened, and there appears to be no impetus to do so. In any case, implementations are free to offer additional command line editing modes based on the exact models of editors their users are most comfortable with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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