5

Sometimes we just need to type a slightly different name when using mv/cp/convert. For example,

convert IMG-long-number.jpg  IMG-long-number.png

How can I repeat IMG-long-number.jpg before typing the IMG-long-number.png, so I only need to make small adjustment?

This is similar to How to repeat currently typed in parameter on bash console? but for zsh/zle.

1
  • I just tested in zsh, and the solution from the link you provided also worked in zsh. Oct 26 '16 at 3:07
7

!#$<Tab> works for me. Given:

$ echo a

Typing !#$ then pressing Tab expands !#$ to a. Tab completion also lists other options if you try an operation with ::

$ echo a !#$:
&  -- repeat substitution
A  -- absolute path resolving symbolic links
Q  -- strip quotes
a  -- absolute path
c  -- PATH search for command
e  -- leave only extension
g  -- globally apply s or &
h  -- head - strip trailing path element
l  -- lower case all words
q  -- quote to escape further substitutions
r  -- root - strip suffix
s  -- substitute string
t  -- tail - strip directories
u  -- upper case all words
2
4

An alternative to bear in mind is that: you don't.

After using completion to get

convert IMG-long-number.jpg
simply edit that into

convert IMG-long-number.{jpg,png}

3

I'd use Ctrl+Alt+_ that copies the previous word (copy-prev-word widget). Once copied, you can use Backspace to edit the extension of the copy.

Or use Ctrl+W to delete it as a whole. By default, Ctrl+W deletes whole words (defined as alnums+$WORDCHARS), but you can change the behaviour on demand with the select-word-style widget.

In ~/.zshrc:

autoload select-word-style
zle -N select-word-style
bindkey '\eW' select-word-style

Then, you can select a different word style with Alt+Shift+W. You can use the bash word style (words only alnums) here (the reference is to how bash word widgets other than Ctrl+W like Alt+B, Alt+D... consider words).

1

If you use the vi mode in zsh then you can just write the first number, then ESC and Byt. to copy until dot. Finally A and SPC to go to the end and insert a space and ESC Pi.png to paste and type the png extension.

0

Similar to what @muru suggested - !#$<TAB>. But I also made a bindings like this in my .zshrc :

bindkey -s "^[," "!#\$^I"

That way, pressing Esc then comma will do it for you, similar to when Esc then dot will get you the last argument from the previous command.

2
  • 1
    But zsh already has a widget (copy-prev-word) and binding for that (Ctrl+Alt+_). If you'd rather have it on (Alt+,), bindkey '\e,' copy-prev-word Jul 28 '17 at 15:51
  • Well, technically, it is not the same. Type something like command "parameter with spaces" and then try yours and my solutions. Both my and yours approach could be used in different situations. That being said, zsh has another widget copy-prev-shell-word that works exactly like my previous suggestion.
    – Alex J.
    Dec 11 '17 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.