You are not logged in.

#1 2023-03-23 20:34:06

matthew02
Member
Registered: 2012-08-01
Posts: 42

[SOLVED] Shortcut to reference a previous arg from the current cmd?

Is there a way to quickly reference a previous argument from the command I'm currently typing? Let's say, for example, that I want to rename a file to simply add another extension to it?

mv dir/subdir/filename.ext dir/subdir/filename.ext.old

I'm thinking something like the history expansion that lets you reference an argument from the previous command.

ls dir/subdir/filename.ext
mv !$ !$.old

Except, I would like to reference the command I'm currently typing. In this example, #$% should be some designator referencing the first argument of the command in progress.

mv dir/subdir/filename.ext #$%.old

I'm aware of find dir/subdir/filename.ext -exec mv {} {}.old \;, but that's tedious for individual files. I also know this is scriptable, but I prefer to look for "built-in" solutions first.

The usefulness of this is not limited to mv, so I'm really seeking a generic solution that is not tied to mv or any other specific command.

Last edited by matthew02 (2023-03-23 20:59:55)

Offline

#2 2023-03-23 20:44:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,744
Website

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

This would be shell specific.  But for bash, and likely for other big modern shells (eg., zsh), there is brace-expansion:

mv dir/subdir/filename.ext{,.old}

Or you could just use a variable:

f=dir/subdir/filename.ext mv $f $f.old

Last edited by Trilby (2023-03-23 20:47:55)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2023-03-23 20:55:08

matthew02
Member
Registered: 2012-08-01
Posts: 42

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

That's exactly what I was looking for. I was pretty confident there was a way to do it, but I was having trouble finding the solution. I definitely appreciate shell expansions, especially the more I learn (there seems to be quite a bit). Thank you for the help!

Edit: The variable method is also a nice trick. I can see myself putting that to good use, too.

Last edited by matthew02 (2023-03-23 20:58:09)

Offline

#4 2023-03-23 21:28:34

stanczew
Member
Registered: 2021-03-02
Posts: 64

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

For renaming/moving/copying, you can use the renameutils package.
Instead of usual mv/cp, you call imv/icp with the source path, and then interactively modify it (e.g. append ".old" suffix) to produce destination path.
It's not a completely generic solution, but for file operations it's very quick and convenient.

Offline

#5 2023-03-23 22:55:21

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 661

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

I use vi-editing mode in bash so traditionally I would yank and put that last argument again. Nowadays, e.g. for your specific example, I use edir. Just `edir dir/subdir` and then append the extra extension to that file name (and however many others) via your favorite editor. Get it from the AUR.

Disclaimer: I am the author of edir and now use it a few times per day. Don't know how the rest of you are surviving without it!

Offline

#6 2023-03-24 09:16:47

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

If you're already looking at history expansion, it doesn't seem that easy to miss in the man pages (bash/zsh at least):

mv dir/subdir/filename.ext !#$.old

Offline

#7 2023-03-24 19:13:20

matthew02
Member
Registered: 2012-08-01
Posts: 42

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

Raynman wrote:

If you're already looking at history expansion, it doesn't seem that easy to miss in the man pages (bash/zsh at least):

mv dir/subdir/filename.ext !#$.old

Yep. Looking back at it, I also don't see how I missed it. That's even more exactly what I was looking for. It's nice because I can grab any earlier argument. Thanks!

Last edited by matthew02 (2023-03-24 22:02:34)

Offline

#8 2023-03-24 19:14:23

matthew02
Member
Registered: 2012-08-01
Posts: 42

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

stanczew wrote:

For renaming/moving/copying, you can use the renameutils package.
Instead of usual mv/cp, you call imv/icp with the source path, and then interactively modify it (e.g. append ".old" suffix) to produce destination path.
It's not a completely generic solution, but for file operations it's very quick and convenient.

Okay, I'm going to have to check that out. Those seem like some really handy tools. Thanks!

Offline

#9 2023-03-24 19:24:17

matthew02
Member
Registered: 2012-08-01
Posts: 42

Re: [SOLVED] Shortcut to reference a previous arg from the current cmd?

bulletmark wrote:

I use vi-editing mode in bash so traditionally I would yank and put that last argument again. Nowadays, e.g. for your specific example, I use edir. Just `edir dir/subdir` and then append the extra extension to that file name (and however many others) via your favorite editor. Get it from the AUR.

Disclaimer: I am the author of edir and now use it a few times per day. Don't know how the rest of you are surviving without it!


I'm still getting the hang of vi-mode in ZSH. It sometimes seems to not work how I expect, but I'm probably just not trying hard enough. I tend to work around annoyances for a while before I get tired and actually address them.

I'll check out `edir`. Thanks for the tip!

Offline

Board footer

Powered by FluxBB