sponsor Vim development Vim logo Vim Book Ad

DirDo.vim : Performs Vim commands over files recursively under multiple directories.

 script karma  Rating 68/25, Downloaded by 3374  Comments, bugs, improvements  Vim wiki

created by
William Lee
 
script type
utility
 
description
This plugin is like :argdo but it works recursively under a directory (or
multiple directories).  The limitation for :argdo is that it does not glob
the files inside the subdirectories.  DirDo works in a similar manner but
you're allowed to specify multiple directories (good for refactoring code
and modifying files in a deep hierarchy).  DirDo also globs the files in
the subdirectories so you can make sure an operation is performed
consistantly.
 
install details
REQUIREMENTS:
  - The :argdo command in Vim 6.0
Put this file in your ~/.vim/plugin directory.

The syntax of the commands:

First we need to set what directory we would like to perform this
command on

    :DirDoDir [/my/directory1] [/my/directory2] [/my/directory3]

    or

    :DDD [/my/directory1] [/my/directory2] [/my/directory3]

If no argument is given, then it'll display the directories that you're
going to work on and let you edit them by separating them with commas
(',')

You can also use the following command to add a directory to the DirDoDir
variable:

    :DirDoAdd [/my/dir]

    or

    :DDA [/my/dir]

If you do not give an argument to DDA, it'll add the current working
directory to the DirDoDir variable.

Then we set the file glob pattern

    :DirDoPattern [file glob pattern1] [file glob pattern2] ...

    or

    :DDP [file glob pattern1] [file glob pattern2] ...

If no argument is given, then it'll display the patterns and let you
edit them (separate each pattern with spaces).

Then, you can do:

    :DirDo [commands]

    or

    :DDO [commands]

to executes the commands on each file that matches the glob pattern
recursively under the directories that you have specified.  The format of
[commands] is just like what you do on :argdo. See :help argdo for
details.

If no argument is given, then it'll reuse the last command.

Examples:

Replace all the instaces of "Foo" to "Bar" in all the Java or C files under
the directory /my/directory and its subdirectories, confirming each match:

    :DDD /my/directory (or just :DDD<CR> and type it in)
    :DDP *.java *.c (or just :DDP<CR> and type it in)
    :DDO %s/Foo/Bar/gce | update

(See :h argdo for the commands after DDO.)

Same scenario but replacing "Bar" with "Baz" without confirmation for each
match (note the directory and patterns are saved):

    :DDO %s/Bar/Baz/ge | update

(Since 1.2) There is a command called DirDoFast where you can set the
directory, pattern, and command at once.  Note that your arguments need to
be escaped if there is an space to it, otherwise Vim will break up the
arguments incorrectly.  The syntax is:

    :DirDoFast [directories] [patterns] command

    or

    :DDF [directories] [patterns] command

Multiple directories need to be separated by comma.  Patterns need to be
separated by spaces.  Since space is used by Vim to separate the command
arguments, ALL spaces in the directory, pattern, or command need to be
escaped.  For example, the last command using DDF would look like:

:DDF /my/directory *.java\ *.c %s/Foo/Bar/gce\ |\ update

If you only give 2 arguments to :DDF, you assume the directory is set to
".".

If you only give 1 argument to :DDF, you assume the directory is set to
"." and the pattern is set to "*".

(Since 1.3) There is a command called DirReplace that simplifies the
multi-directory string replace procedure.  Essentially, it is the same
as executing DirDoFast with a directory, pattern, and command (as
s/[regex_pattern]/[replacement/gce | update).

    :DirReplace

    or

    :DR

You will be asked for the directories, glob patterns, regex string, and
the replacement.

There is an option to run DirDo with less verbosity, to toggle the
setting, run:

    :DirDoVerbose

    or

    :DDV

You can also set the following variables in your .vimrc to set the default
directory and pattern.  This is good for pointing to the directories for
code refactoring:

let g:DirDoPattern = "..."

let g:DirDoDir = "..."

For example, if you want by default have the command apply on all your C,
C++, and Java source, you can set the DirDoPattern to:

let g:DirDoPattern = "*.c *.cpp *.java"

If you want to apply your changes to /dir1, /dir2, and /dir3, you can do:

let g:DirDoDir = "/dir1,/dir2,/dir3"
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
DirDo.vim 1.3 2005-09-14 6.0 William Lee Added the DirReplace command.  Fixed a bug on the directory listing that prevents DirDo from functioning properly.
DirDo.vim 1.2 2003-06-12 6.0 William Lee Added the DirDoFast command so you can set directory, pattern, and command at once.  Fixed bug where the unix hidden file will not show up in the file list.  Fixed bug where ~ in the path does not work.
DirDo.vim 1.1 2002-08-20 6.0 William Lee Added the option :DirDoAdd (:DDA) to add the current directory (getcwd()) to the list of directories that you want to apply DirDo on.  This is useful if you want to construct your list of DirDo directories quickly.
DirDo.vim 1.0 2002-08-07 6.0 William Lee Initial upload
ip used for rating: 3.135.183.187

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github