3

Consider:

ssh -i "key.pem" root@server.com

Is there a quick way to jump to the beginning of "root" in such a Bash command in the terminal, without iterating over every word/character?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3180
  • 1,369
  • 1
  • 21
  • 38

1 Answers1

8

Yes. Bring up the last command using up arrow. These shortcuts will help:

  • Ctrl + E - go to the end of the line
  • Ctrl + A - go to the start of the line
  • Alt + left - go back one word
  • Alt + right - go right one word
  • Ctrl + W - delete the last word

To search the command line and the command line history, use:

  • Ctrl + R and then type the search string, e.g. "roo" to search backwards for "root@...". In the example you have given, however, it will be easier just to jump forward or back by words.
  • Ctrl + S search forwards (rarely useful). (If forward searching doesn't appear to work, try this other Stack Overflow answer.)

See also this page and google "Bash command line editing" for more tricks.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • I am referring more to the case where there are many words in the command, and I want something like the vim / notation where we can jump to the first instance of a search word – user3180 Jan 05 '19 at 06:57
  • 2
    CTRL-R works! But, CTRL-S does not bring up any search prompt like CTRL-R, even when starting at the beginning of the line? – user3180 Jan 05 '19 at 07:21
  • That's very interesting. I just copied it there for the sake of completeness from the docs, now I test it it doesn't work for me either. I can't immediately find a way to explain that. It's not a useful feature, because in practice, you always want to search backwards. – Alex Harvey Jan 05 '19 at 07:29
  • The keyboard shortcuts with *"Alt"* may conflict with shortcuts in the GUI (say, using the terminal window on [Ubuntu](https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29)). – Peter Mortensen Sep 19 '22 at 00:25