Last Updated: February 25, 2016
·
1.709K
· smathy

Reuse parts of previous command

So, you're in bash and you've just run a command on some file, like you've ls -led it.

ls -l some/long/filename.here

...and now you want to open that file in, say, vim. You could arrow up, delete the ls -l and replace it with vim - or...

vim !!:$

Tada, the !!:<position> allows you to extract the zero-based bash arguments from the previous command in your history. The $ represents the last argument, but you can use !!:0 or any other number to get at the args. You can even do: !!:1-$ to get from index 1 to the end.

2 Responses
Add your response

In fact, vim !$ is enough in your example.

over 1 year ago ·

Awesome, I didn't know about that. I also recently discovered that there's even a var that gets set: $_ is set to the last arg of the previous command.

over 1 year ago ·