views:

1443

answers:

4

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, e.g. option25?

+5  A: 

I tend to prefer vi editing mode (since those keystrokes are embedded into my spinal cord now (the brain's not used at all), along with the CTRL-K, CTRL-X from WordStar 3.3 :-). You can use the command line set -o vi to activate it (and set -o emacs to revert).

In Vi, it would be (ESC-K to get the line up first of course) "f5;;B" (without the double quotes).

Of course, you have to understand what's on the line to get away with this. Basically, it's

f5 to find the first occurrence of "5" (in --option5).
;  to find the next one (in --option15).
;  to find the next one (in --option25).
B  to back up to the start of the word.

Let's see if the emacs aficionados can come up with a better solution, less than 5 keystrokes (although I don't want to start a religious war).

Have you thought about whether you'd maybe like to put this horrendously long command into a script? :-)

Actually, I can go one better than that: "3f5B" to find the third occurrence of "5" then back up to the start of the word.

paxdiablo
No need for a script - just hit "v" in vi mode and open the command line in an editor. You can format the commandline, err, "script" more nicely with newlines, and there's no tempfile needed. :)So, "v/25" gets you there in 4 chars.
dannysauer
+2  A: 

After running the command once, run fc

It will launch $EDITOR with the previous command, then you can use your regular editor to modify the command. When you save and exit, the file will be executed.

..but, as Pax said - the command line isn't particularly good for editing absurdly long lines - why not make the command into a script?

dbr
A: 

One option is to use M-x shell in emacs. That provides all editing facilities and keystrokes that emacs has, so C-s can be used to search the text option25, for example.

(But I'd still prefer to be in the real terminal shell instead if someone can point me to good search and edit facilities.)

+3  A: 

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

  • Use C-xC-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 C-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. E.g. !-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 C-] for forward search, and C-M-] for backward search.

I recommend the second option. C-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).

Pianosaurus
Thanks! C-x C-e is great!