Using previous command arguments in a new line

By thomas, 12 January, 2012

A friend of mine was talking about his design for a shell that would allow you to programatically reuse arguments. I started looking and found that bash can do this but not programatically (at least as far as I know). The syntax was slighty confusing but here is the summary, it blew my mind when I started using it (but I'll probably forget it again soon enough, like all the awesome things you can do in vi if you can remember...)

Here goes, lets copy a file from a deeply nested path to our location by first tab-completing the filename with ls


[thomas@host ~]$ ls /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd
/usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd

Now type cp on the new line and then Meta-Control-y

(That is, hold down Meta, Control and then type y ((hopefully you know where your Meta key is, on my keyboard it's the left-alt)))


[thomas@host ~]$ cp /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd .

Cool, now what about a line with many arguments:


[thomas@host ~]$ ls /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd /usr/share/texmf/tex/plain/plgraph/picture.tex /usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty
/usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd /usr/share/texmf/tex/plain/plgraph/picture.tex

To get the last argument, type cp then Meta-.


[thomas@host ~]$ cp /usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty .

Now the one that was somwhat tricky to get would be how to change the argument number


[thomas@host ~]$ ls /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd /usr/share/texmf/tex/plain/plgraph/picture.tex /usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty
/usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd /usr/share/texmf/tex/plain/plgraph/picture.tex

To get the second argument: Meta-2 then (without letting go of Meta) Meta-Control-y


[thomas@host ~]$ ls /usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd /usr/share/texmf/tex/plain/plgraph/picture.tex /usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty
/usr/share/texmf/tex/latex/gnuplottex/gnuplottex.sty
/usr/share/texmf/tex/latex/mathdesign/mdput/ts1mdput.fd
/usr/share/texmf/tex/plain/plgraph/picture.tex
(arg: 2) ls
[thomas@host ~]$ ls /usr/share/texmf/tex/plain/plgraph/picture.tex
/usr/share/texmf/tex/plain/plgraph/picture.tex