How to recursively print all files under the current working directory from Vim?
A convenient way to execute a command for a group of files is to
(1) collect the list of their names, define it as the new argument list
(see :help arglist
), and then (2) iterate the command over that list.
1. To perform the first step, use the :args
command with
a wildcard matching the desired files. For example,
:args ./**/*
sets the argument list to the names of all files in the current directory and its subdirectories; similarly,
:args /tmp/**/*.{c,h}
selects all .c
and .h
files in /tmp
and its subdirectories.
For details about wildcard syntax, see :help wildcard
.
If the path to the root of a subtree containing files to print is unknown beforehand and is built by a script, use the command
:exe 'args' join(map(split(glob(p . '/**/*'), '\n'), 'fnameescape(v:val)'))
where the variable p
is supposed to contain the path to that
root directory.
2. For sending files in the argument list to the printer, execute
the :hardcopy
command for those files using the :argdo
command:
:argdo hardcopy!
The !
specifier suppresses the modal dialog for selecting printing
parameters.
A more complicated command can be used to print each file to a separate PostScript document located at the same directory as that file:
:argdo hardcopy! >%:p.ps
Here the name of a printed file is concatenated with the .ps
suffix
to get the name of a corresponding PostScript file (see
:help cmdline-special
).
For speeding up the :argdo
argument command, Vim ignores the
Syntax
autocommand event by adding it to the eventignore
list.
This implies that if Syntax
autocommands had not been run for
a file in the argument list before the :hardcopy
command is
:argdo
ne, the corresponding printed document would not be
syntax highlighted (despite syntax:y
being set in printoptions
).
To execute Syntax
autocommands for all files in the argument
list, use the following command first:
:argdo set ei-=Syntax | do Syntax
To do this in the same run as printing, concatenate the two commands:
:argdo set ei-=Syntax | do Syntax | hardcopy! >%:p.ps
Edit Sorry, I misunderstood before.
To print all, say php and C# files in your working directory:
:args ./*.{cs,php} **/*.{cs,php}
:argdo ha