Vim question

Bart Whiteley bart.plug at whiteley.org
Tue Jan 13 21:17:04 MST 2009


On Tue, Jan 13, 2009 at 8:41 PM, Alec Shaw <shaw.alec at gmail.com> wrote:
> Every so often I have files with spaces in their names and I want to rename
> them with underscores for the spaces.
> The way I have normally done the the renaming is by putting the output of an
> ls into a file and editing the file in vim
> to make it into a script.
> I usually do ":g/.*/s//mv & &/ " and then hand editing the paces to
> underscores in the third column.
> There has to be a better way.
> Is it possible to have vim do the substitution on the third column without
> affecting the first column?
> Actually as I think about it maybe a bash or ksh (after years of use ksh is
> still my preferred shell) script
> would be better.  Maybe a "for f in *; do mv $f $g; done" but how do I get
> $g from $f with spaces substituted?

$ touch "one two" "three four" "five six"
$ ls -1
five six
one two
three four
$ sh ../rename.sh
$ ls -1
five_six
one_two
three_four
$

rename.sh:

#!/bin/bash

for i in `ls | tr ' ' '^A'`; do
    src=`echo $i | tr '^A' ' '`
    dest=`echo $i | tr '^A' '_'`
    mv "$src" $dest
done


The magic ^A character is created in vim with Ctrl+V,Ctrl+A



More information about the PLUG mailing list