8 vifiles - Edit multiple files as one
12 If a LF char at the end of any files is missing, it'll be added after edit.
16 vidir(1) from moreutils
35 dir_depth
=$
[dir_depth
+ 1]
40 if [ $dir_depth -le 1 -o $Recursive = yes ]
45 cat "$file" | filename
=$file perl
-pe 's/^/$ENV{filename}:/; s/\n?$/\n/'
51 tmpfile
=`mktemp -t vifiles.tmp-XXXXXX`
63 echo "vifiles - edit multiple files at once
65 Usage: vifiles [-R] [FILES]
67 -R, -r, --recursive open all files within the directories specified in parameters
68 -f, --force don't ask confirmation to save changes (provided that the editor exited without error code)
70 vifiles opens up your text editor (search list: \$EDITOR, \$VISUAL, vi) to edit a file with the content of all of the files specified in parameters, each line prefixed with the file name from which it's coming from. Save changes back to the original files."
74 -*) echo "Unknown option: $1" >&2
83 prepcat
"$@" >"$tmpfile"
85 echo "Opening $nfiles files to edit ..." >&2
87 command ${EDITOR:-${VISUAL:-vi}} "$tmpfile"
90 read -e -p "Apply changes? [y/n] " -i n
91 if [ "${REPLY^^}" != Y
]
96 # create directories if any new added in the file paths
97 cat "$tmpfile" | cut
-s -f1 -d: |
sed -e 's@\(.\+\)/[^/]\+@\1@' |
sort -u |
xargs --verbose -d "\n" mkdir
-v -p
99 echo "Saving edited files ..." >&2
101 cat "$tmpfile" | perl
-ne '
102 if(($path, $line) = /([^:]+):(.*)/s)
104 if(not exists $fhs{$path})
106 if(not open $fhs{$path}, ">", $path)
108 warn "vifiles: $path: $!\n";
112 if(defined $fhs{$path})
114 print {$fhs{$path}} $line or warn sprintf "vifiles: %s while writing %d bytes to %s\n", $!, length $line, $path;