Re: Help using vim script capabilities

22 views
Skip to first unread message

Ben Fritz

unread,
Oct 25, 2012, 3:17:22 PM10/25/12
to vim...@googlegroups.com
On Thursday, October 25, 2012 11:37:33 AM UTC-5, Michael Young wrote:
> Hello!
> I am attempting to use vim in a shell script to read in a file, edit it, and write it out to a new file. When I manually execute the individual vim commands it seems to work properly but when I put the commands in a script file and try to use that, the commands don't seem to work. My shell script generates a recursive file directory listing. I then want to use vim to edit the listing to replace any spaces in each file path with \space. I am including the shell below. HELP!
> Thanks!
> Michael
>
> #!/bin/sh
>
> # Generate a recursive list of full pathnames for all filenames that do not
> # begin with a "."
> find "$1" -type f -and \! -name ".*" > filelist1
>
> # Generate the input script for the vim editor
> # Go through the "find" listing and replace every " " with "\ "
> cat << EOF > vimscript
> :%s/ /\\ /g
> :wq! md5shell
> EOF
>
> # Execute vim with the generated input script
> vim -s vimscript filelist1

Your cat command does not create the file you expect. Garbage in, garbage out. I did the cat command by hand and got a file with contents:

:%s/ /\ /g
:wq! md5shell

Note the lack of a second \ character.

Marc Weber

unread,
Oct 25, 2012, 3:25:24 PM10/25/12
to vim_use
Use the right tool for this job which means:

:n **/*.ext
:bufdo %s/ /\\space/g
:wa

or use command line utils only:

find -p0 ... | xargs -0 sed -i -e 's/ /\\space/'
or such.

If you have more time continue with your attempt. Some ideas /notes

command! -nargs=* E exec 'e '.fnameescape(join([<f-args>], ' '))

Within vim you can do

filter(split(glob('**.txt'),"\n"),"v:val =~ '.txt$')

to get a file list, fnameescape may be your friend.

Marc Weber

Michael Young

unread,
Oct 25, 2012, 6:09:53 PM10/25/12
to vim...@googlegroups.com
On Thursday, October 25, 2012 9:37:33 AM UTC-7, Michael Young wrote:
> Hello!
> I am attempting to use vim in a shell script to read in a file, edit it, and write it out to a new file. When I manually execute the individual vim commands it seems to work properly but when I put the commands in a script file and try to use that, the commands don't seem to work. My shell script generates a recursive file directory listing. I then want to use vim to edit the listing to replace any spaces in each file path with \space. I am including the shell below. HELP!
> Thanks!
> Michael
>
> #!/bin/sh
>
> # Generate a recursive list of full pathnames for all filenames that do not
> # begin with a "."
> find "$1" -type f -and \! -name ".*" > filelist1
>
> # Generate the input script for the vim editor
> # Go through the "find" listing and replace every " " with "\ "
> cat << EOF > vimscript
> :%s/ /\\ /g
> :wq! md5shell
> EOF
>
> # Execute vim with the generated input script
> vim -s vimscript filelist1

Ahhh ... I see where I went wrong. Thank you for your response.

Michael Young

unread,
Oct 25, 2012, 9:15:31 PM10/25/12
to vim...@googlegroups.com
Thanks again for your help. It turned out that all I had to do was add another backslash to make it work correctly (i.e. 3 backslashes in a row). Once I did that it worked great.
Reply all
Reply to author
Forward
0 new messages