Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recursively print all files under the current working directory from Vim?

Tags:

vim

I’m currently using MacVim and I’d like to print out all the files in my working tree. Is there a way to simply do this, perhaps using the hardcopy command?

like image 593
Phil Aquilina Avatar asked Oct 21 '11 02:10

Phil Aquilina


People also ask

How do I list all files in a directory recursively?

Try any one of the following commands to see recursive directory listing: ls -R : Use the ls command to get recursive directory listing on Linux. find /dir/ -print : Run the find command to see recursive directory listing in Linux. du -a . : Execute the du command to view recursive directory listing on Unix.

How do I list files in a directory and subdirectories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


2 Answers

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 :argdone, 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
like image 190
ib. Avatar answered Oct 13 '22 23:10

ib.


Edit Sorry, I misunderstood before.

To print all, say php and C# files in your working directory:

:args ./*.{cs,php} **/*.{cs,php}
:argdo ha
like image 33
sehe Avatar answered Oct 13 '22 22:10

sehe