There are situations when you need to perform some sequence of actions simultaneously in several files. Of course, for this you can use different tools (offhand: bash + sed / awk, python / perl, ...) - some will be less adapted for the task, some - more. Now we will consider an example of solving one such simple task with the help of the Vim editor.
Consider a simple example: inserting a string at the beginning of each file.
In the course of performing this simple task, some nuances of using Vim may arise, the knowledge of which may be necessary and useful in working with this wonderful editor. At the same time, this example will give beginners the opportunity to consolidate the use of registers and macros Vim in their practice.
So let's get started:
')
1. Open in the editor all the files in which you need to make changes:
$ vim -o file1.txt file2.txt file3.txtThe -o option allows you to open each file in a separate window. The launch with this option in the example was made not only for clarity (all changes will be visible immediately), but also on the basis of one more moment which will be discussed below.
2. Write the sequence of actions that need to be done on the files in the macro:
qm1GOFirst line <ESC> quqm - the beginning of the macro record in the register m
1G - go to the first line of the file
O (capital Latin letter O) - inserts a line before the current line with the transition to edit mode
The first line is the content of the first line.
<ESC> - exit edit mode
q - the end of the macro recording
u - cancel changes in the current window in order to avoid duplication of the changes made
3. Run the macro m in all windows:
: windo normal @mwindo - vim command launch command in all windows
normal - the command to start the command of the normal mode vim from the command line, that is, two commands to start the macro are equivalent:
@m: normal @mIf we just did this:
: windo @mthen we would get the error message “This is not an editor command”
in fact, we would do the following in each window:
: @mwhich is incorrect since we run the normal mode command on the command line. As a result, we can get either an error message or unpredictable actions of the editor.
There are commands similar to
: windo :
: argdo: tabdo: bufdoThe first command is
: argdo . A powerful command that allows you to execute a command in a list of files given as a named argument. Here we will not consider it.
The second command is
: tabdo . It is completely similar to our windo, but does all the work on all open tabs. There is one point worth mentioning: there may be several windows in each tab. So, the changes will be only in the current window of each tab!
Well, finally, the third team -
: bufdo . Executes a command on all buffers. But when using it there is one nuance. The bufdo command itself will be executed only in the current buffer and will fail with an error if the “useful” command (the one that bufdo runs and that should be executed in each buffer) changes the text.
For example:
: bufdo normal Gwill execute normally in all buffers
but like this:
: bufdo normal GoEnd filealready complete with an error. To make everything work, you need to do this:
: bufdo! normal GoEnd fileActually this is all for today. For my first post on Habré, I ask you not to kick. Corrections, additions, comments - are welcome.