Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For example I enter

cat /very/long/path/textfile

and right after that I decide to give the next command

rm /very/long/path/textfile

So, my question is: What is the easiest/fastest way to replace the first word of a previous command? (without arrow keys or copy-paste)

It should work for any command (cat, rm and textfile are just for this example, it doesn't have to be those)

share|improve this question

3 Answers 3

Assuming that you’re running a shell with a standard history mechanism (e.g., bash, C shell, …), type the new command (e.g., rm) followed by !*.  Type man bash, man csh, or whatever is appropriate for you, to see more details (and there are lots of them).

share|improve this answer
    
thank you, that is what I was looking for. –  tr3quart1sta Jan 3 '13 at 10:06

If you are using bash or sh you can type the first command and then press "Alt" with "." (Dot) to get the last argument of the previous command.

cat /very/long/path/textfile

to use the last argument of earlier command type

rm "Alt+." and you get

rm /very/long/path/textfile
share|improve this answer

If you're using bash, you can type <Esc>_ to get the last argument of the previous command. You can also use the variable $_.

Another approach would be to recall the previous command with Ctrl-P, then use Ctrl-A to move to the start of the command, then Alt-D to delete the first word, type the new first word (the command name) and hit Enter.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.