Fastest way(s) to move the cursor on a terminal command line?

What is the best way to move around on a given very long command line in the terminal?

Say I used the arrow key or Ctrl-R to get this long command line:

./cmd --option1 --option2 --option3 --option4 --option5 --option6 --option7 --option8 --option9 --option10 --option11 --option12 --option13 --option14 --option15 --option16 --option17 --option18 --option19 --option20 --option21 --option22 --option23 --option24 --option25 --option26 --option27 --option28 --option29 --option30 --option31 --option32 --option33 --option34 --option35 --option36 --option37 --option38 --option39 --option40 --option41 --option42 --option43 --option44 --option45 --option46 --option47 --option48 --option49 --option50 

Now I need to move (starting from the beginning or the end of the line) the cursor to --option25 to modify something there.

What is the fastest way to get there? What I usually do is Ctrl-A to get to the beginning and then repeatedly Alt-F to move forward, word by word (or Ctrl-E to go the end and Alt-B to then go backward). But on a long line that takes too much time. There must be a way to search and jump directly to the part I need to modify, eg option25 ?


Since this hasn't been closed yet, here are a few more options.

  • Use Ctrl+x followed by Ctrl+e to open the current line in the editor specified by $FCEDIT or $EDITOR or emacs (tried in that order).
  • If you ran the command earlier, hit Ctrl+r for a reverse history search and type option25 (in this case). The line will be displayed. Hit Tab to start editing at this point.
  • Use history expansion with the s/// modifier. Eg !-2:s/--option25/--newoption/ would rerun the second-to-last command, but replace option25. To modify the last ./cmd command, use the !string syntax: !./cmd:s/--option25/--newoption/
    Any delimiter may be used in place of / in the substitution.
  • If editing the previous line, you can use quick substitution: ^--option25^--newoption
  • Character search. This was mentioned by Pax, and can be done in regular emacs-mode with Ctrl+] for forward search, and Ctrl+Alt+] for backward search.
  • I recommend the second option. Ctrl+r is really handy and fast, no mucking about with editors, and you see the results before the command is run (unlike the history expansions).


    To be clear, you don't want a "fast way to move the cursor on a terminal command line". What you actually want is a fast way to navigate over command line in you shell program.

    Bash is very common shell, for example. It uses Readline library to implement command line input. And so to say, it is very convenient to know Readline bindings since it is used not only in bash. For example, gdb also uses Readline to process input.

    In Readline documentation you can find all navigation related bindings (and more): http://www.gnu.org/software/bash/manual/bash.html#Readline-Interaction

    Short copy-paste if the link above goes down:

    Bare Essentials

  • Cb Move back one character.
  • Cf Move forward one character.
  • [DEL] or [Backspace] Delete the character to the left of the cursor.
  • Cd Delete the character underneath the cursor.
  • C-_ or Cx Cu Undo the last editing command. You can undo all the way back to an empty line.
  • Movement

  • Ca Move to the start of the line.
  • Ce Move to the end of the line.
  • Mf Move forward a word, where a word is composed of letters and digits.
  • Mb Move backward a word.
  • Cl Clear the screen, reprinting the current line at the top.
  • Kill and yank

  • Ck Kill the text from the current cursor position to the end of the line.
  • Md Kill from the cursor to the end of the current word, or, if between words, to the end of the next word. Word boundaries are the same as those used by Mf.
  • M-[DEL] Kill from the cursor the start of the current word, or, if between words, to the start of the previous word. Word boundaries are the same as those used by Mb.
  • Cw Kill from the cursor to the previous whitespace. This is different than M- because the word boundaries differ.
  • Cy Yank the most recently killed text back into the buffer at the cursor.
  • My Rotate the kill-ring, and yank the new top. You can only do this if the prior command is Cy or My.
  • M is Meta key. For Max OS X Terminal you can enable "Use option as meta key" in Settings/Keyboard for that. For Linux its more complicated.

    Update

    Also note, that Readline can operate in two modes:

  • emacs mode (which is the default)
  • vi mode
  • To switch Bash to use vi mode:

    $ set -o vi
    

    Personaly I prefer vi mode since I use vim for text editing.

    Bonus

    In macOS Terminal app (and in iTerm too) you can Option-Click to move the cursor (cursor will move to clicked position). This even works inside vim .


    按住Option键并点击你想要移动光标的位置,然后终端将光标移动到精确的位置。

    链接地址: http://www.djcxy.com/p/49360.html

    上一篇: 如何在Mac OS X中清除终端中的以前输出?

    下一篇: 在终端命令行上移动光标的最快方法?