9 B<vidir> [--verbose] [directory|file|-] ...
13 vidir allows editing of the contents of a directory in a text editor. If no
14 directory is specified, the current directory is edited.
16 When editing a directory, each item in the directory will appear on its own
17 numbered line. These numbers are how vidir keeps track of what items are
18 changed. Delete lines to remove files from the directory, or
19 edit filenames to rename files. You can also switch pairs of numbers to
22 Note that if "-" is specified as the directory to edit, it reads a list of
23 filenames from stdin and displays those for editing. Alternatively, a list
24 of files can be specified on the command line.
32 Verbosely display the actions taken by the program.
36 =head1 ENVIRONMENT VARIABLES
46 Also supported to determine what editor to use.
52 Does not support deletion of directories. Does not support recursive
53 editing of contents of a directory.
57 Copyright 2006 by Joey Hess <joey@kitenet.net>
59 Licensed under the GNU GPL.
69 if (! GetOptions
("verbose|v" => \
$verbose)) {
70 die "Usage: $0 [--verbose] [directory|file|-]\n";
77 foreach my $item (@ARGV) {
79 push @dir, map { chomp; $_ } <STDIN
>;
81 open(STDIN
, "/dev/tty") || die "reopen: $!\n";
84 opendir(DIR
, $item) || die "$0: cannot read $item: $!\n";
85 push @dir, map { "$item/$_" } sort readdir(DIR
);
93 my $tmp=File
::Temp
->new(template
=> "dirXXXXX");
94 open (OUT
, ">".$tmp->filename) || die "$0: cannot write ".$tmp->filename.": $!\n";
99 next if /(.*\/)?\
.$/ || /(.*\
/)?\.\.$/;
101 print OUT
"$c\t$_\n";
107 if (-x
"/usr/bin/editor") {
108 @editor="/usr/bin/editor";
110 if (exists $ENV{EDITOR
}) {
111 @editor=split(' ', $ENV{EDITOR
});
113 if (exists $ENV{VISUAL
}) {
114 @editor=split(' ', $ENV{VISUAL
});
116 $ret=system(@editor, $tmp);
118 die "@editor exited nonzero, aborting\n";
121 open (IN
, $tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
124 if (/^(\d+)\t{0,1}(.*)/) {
127 if (! exists $item{$num}) {
128 print STDERR
"$0: unknown item number $num\n";
131 elsif ($name ne $item{$num}) {
132 next unless length $name;
136 if (-e
$name || -l
$name) {
139 while (-e
$tmp || -l
$tmp) {
143 if (! rename($name, $tmp)) {
144 print STDERR
"$0: failed to rename $name to $tmp: $!\n";
148 print "'$name' -> '$tmp'\n";
150 foreach my $item (keys %item) {
151 if ($item{$item} eq $name) {
157 if (! rename($src, $name)) {
158 print STDERR
"$0: failed to rename $src to $name: $!\n";
162 print "'$src' -> '$name'\n";
171 die "$0: unable to parse line \"$_\", aborting\n";
175 unlink($tmp.'~') if -e
$tmp.'~';
177 foreach my $item (sort values %item) {
178 if (! unlink($item)) {
179 print STDERR
"$0: failed to remove $item: $!\n";
183 print "removed '$item'\n";