Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: removing spaces from a file name

"Mikael Magnusson" wrote:
> > You could define a function in your .zshrc (or make a script in your
> > ~/bin or something) that does it for you
> > function renamespaces() { for a ($@) {mv $a ${a// } } }
> If you're the kind of person who likes keybinds, I guess this works
> too, if you have copy-earlier-word bound somewhere too.

That's quite useful... here's another more general widget that operates on
the current word, prompting for source and replacement strings (as with
Mikael's, if you use it with mv it's more work).  This essentially just
replaces the history substitution in the original posting with something
a bit more intuitive.

# replace-in-current-word
# Prompt for source and replacement text for current argument.
# Currently only handles strings, not patterns.
# Operates on the unquoted argument, but takes care to requote
# it (with backslashes).  Hence, for example, a command line argument
#   foo\*\ bar
# when given " " to replace with "_" produces
#   foo\*_bar
# (with no backslash before the "_").
# Based on replace-string: can use previous source and replacement
# strings using the mechanism documented for that (via numeric argument
# or style).

emulate -L zsh
setopt extendedglob

autoload -U read-from-minibuffer modify-current-argument

local p1="Replace: " p2="   with: "

if (( ${+NUMERIC} )); then
  (( $NUMERIC > 0 )) && previous=1
  zstyle -t ":zle:$WIDGET" edit-previous && previous=1

read-from-minibuffer $p1 ${previous:+$_replace_word_src} || return 1

read-from-minibuffer "$p1$_replace_word_src$p2" \
  ${previous:+$_replace_word_rep} || return 1

modify-current-argument '${(q)${(Q)ARG//$_replace_word_src/$_replace_word_rep}}'

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

Messages sorted by: Reverse Date, Date, Thread, Author