0

Frequently I’ll have to do some digging around to figure out what I’m doing on linux, involving quite a bit of ‘’, ‘ls -la’, ‘cd’, ‘cat’, and ‘vim’

Is there any way to quickly reuse the file/target of the previous command?

e.g. I have to look around for a file, say with ls, and when I’ve found it I’ll need to use it with a program like cat or vim. So let’s say I’ve got ‘ls -la /some/path/SomeName’ and hit , say I’ve drilled down to where this is the file I was looking for. What I’d like to do is some kind of ‘!!’ Like when you forget to use sudo; Say I really want to use ‘cat’ but had been searching around with ‘ls’ and ‘’ - or I’ve been searching around with ‘cat’, up-arrow and continuing to refine my ‘cat /file/path/‘ and then when I’ve found what I’m looking for I’ll want to edit that file

I’d like to be able to do something like ‘cat !!’ or ‘vim !!’

EE1337
  • 1

2 Answers2

0

If you are using bash with emacs key bindings then you need to know about ESC period (aka dot aka full stop). You can also use $_

icarus
  • 17,920
0

It sounds like you're looking for the "history event" syntax for arguments. You mention !!, which repeats the whole previous command. But, you can also refer to specific arguments from that command, or all of them. !^ will get you the first argument; !$ will get you the last one (think vi commands or regular expression syntax) and !* will get you all of the arguments (but not the command itself).

So, after finding your file with ls -l /etc/passwd, you could do cat !$ to display its contents.

mattdm
  • 40,245