You are hereUsing previous command arguments in a new line

Using previous command arguments in a new line


By thomas - Posted on 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

Tags

good work

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, </bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <latex>, </latex>, <sql>, </sql>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.