Creating a File Set Processing Script with Vim

In Vim, the r! ex command will insert the contents of standard output of a command. Combining this with Vim’s powerful subsitute command, it is possible to write a batch script over a set of files. In this example, Vim is used to quickly change the extension of a number of files in a directory.

For this example, a script to change the extension of some files from “tmp” to “dat“. In Vim, use the following command in Windows:

r! dir /B *.tmp

If you are in Linux, use ls instead:

r! ls *.tmp

The listed files will be from the current working directory. If the files were in another directly, you could provide the location of the directory to the dir or ls command. Alternatively, you change the current working directory by using the cd ex command.

After executing the listing command, the files will be listed in the editor. The listing can be turned into a batch rename script by using the substition ex command. The following substitution command will turn the list of rename commands for Windows:

%s/\(\(.*\)\.tmp\)/move \1 \2.dat/

In Linux, you would have to use mv in place of move. In case you do not understand the above command, the following is the excerpt taken from Vim documentation:

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
            For each line in [range] replace a match of {pattern}
            with {string}.
            For the {pattern} see |pattern|.
            {string} can be a literal string, or something
            special; see |sub-replace-special|.
            When [range] and [count] are omitted, replace in the
            current line only.
            When [count] is given, replace in [count] lines,
            starting with the last line in [range].  When [range]
            is omitted start in the current line.
            Also see |cmdline-ranges|.
            See |:s_flags| for [flags].

For the pattern, the two sets of brackets remembers, both, the entire file name (or the source file) and the file name without the extension (so that the same file name can be used with a different extension). If you are not familar with Vim’s regular expressions, you can find more information about in its pattern documentation.

The string “move \1 \2.dat“, in the example, tells the substitute command to replace the matched content with the word “move“, followed by the arguments to the move command. The numbers 1 and 2 corresponds to the contents of the escaped outer brackets (the file name, including the extension) and inner brackets (the file name, without the extension) respectively. By modifying this parameter, you should be able to produce a batch file processing script.

If you make a mistake in this part of the command, the substitute command might not produce the desired results. If you find yourself in this situation, go back to normal or command mode (press ESC to get to this mode from visual, select, insert or ex modes, there is more information in the <a href=""documentation) and press u to undo the results of the substitute command. When you return to ex mode, press the up arrow key to go through the history of executed commands.


One Response to Creating a File Set Processing Script with Vim

  1. Pingback: Using Sed to Process a Set of Files « Kah – The Developer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: