vkontori
vkontori

Reputation: 1245

index current line in bash

I was curious if there is a way to index the current line in bash as it might save quite a bit of typing.

e.g.

$ command longarg1 longarg2 longarg3 | command2 <something_to_expand_longarg2>

I tried !#2 (!# will give expand the contents of the line so far) However it did not work as it will expand the whole line and then put 2.

-Thanks

Upvotes: 3

Views: 112

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798616

From the bash(1) man page, HISTORY EXPANSION section, Word Designators subsection:

A : separates the event specification from the word designator.

$ command longarg1 longarg2 longarg3 | command2 !#:2

Upvotes: 4

sampson-chen
sampson-chen

Reputation: 47267

Not indexing as you have asked, and not ideal exactly, but how about:

  • Using the up / down arrow keys to cycle through history
  • Selecting the long command with mouse cursor, Ctrl+Shift+C to copy and then Ctrl+Shift+V to paste?

(environment dependent)

Upvotes: 0

Related Questions