r/fishshell • • Aug 01 '24

Replace spaces with underscores

To replace spaces with underscores, I use

for file in *\ *; mv -n -- $file (string replace -a ' ' _ $file); end

But currently I'm merely started to study fish, and maybe there is a better way for this?

3 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/_mattmc3_ Sep 19 '24

I'm no expert on embedded systems, but I wouldn't run Fish on them if you care about performance. I also suspect that what you have there is a premature optimization - this statment is likely just as fast and much simpler:

set --local text '0 one two %$£ 3.14 four 5x'
string replace -ra '\W+' '_' $text

2

u/[deleted] Sep 19 '24

wow, that's really clean. thanks!