1

Being truthful, I find the shortcut !:0 not being short enough, considering it also requires the shift key.

I was wondering if I can make an alias or function for it, I'm aware I'm missing something. I did the obvious:

pr() {
    !:0
}

And:

alias pr="!:0"

Alternatively, does something like Alt + . exist, but for the command only section ($0)? I was not able to find anything.

1
  • pr is an existing Unix command, you should pick some other name for your alias or function to avoid breakage in your code and/or other tools you use now or in future that might be calling pr.
    – Ed Morton
    Mar 18 at 11:41

2 Answers 2

5

You can do this using fc:

alias pr='fc -e "sed -i s/\ .*//"'

This asks fc to start an editor with the last command entered, wait for the editor to exit, and run (potentially modified) command. Instead of a regular editor, the alias uses sed to only keep the first word of the command.

1
  • Is there a way to do this without instantly running? Would like to edit a new argument first
    – TuxForLife
    Mar 18 at 0:27
2

First, note that !:0 is not the command in the last line of history. It's the first word. So if you run A=B some-command arg1, !:0 will be A=B.

That said, since you mention Alt., which is:

yank-last-arg (M-. or M-_)

Insert last argument to the previous command (the last word of the previous history entry). With a numeric argument, behave exactly like yank-nth-arg.[...]

There's the aforementioned yank-nth-arg:

Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0).

So you could do Alt0 AltCtrly, or, since yank-last-arg behaves "exactly like yank-nth-arg" with an argument, Alt0 Alt..

You can make a binding for this, but it's a bit roundabout - apparently readline doesn't have a mechanism to bind to a command while specifying an argument. You have to bind to a key sequence which first imitates sequence for an argument and then then the sequence for an existing binding, so you could do:

bind '"\e,": "\e0\e."'

And press Alt, to get the first word.

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.