0

I have multiple HTML files that form a small website. Several lines of code are duplicated at the top of the files (although the <title> differs), and a few lines are duplicated at the bottom.

I could use a pre-compiler like Jekyll, and use layouts so I only have to change the code once, but I feel this adds unnecessary complexity to the project. I could use regexes (which is what I'm currently doing, using DirDo.vim), but this is a bit of a hassle.

So, is there any plugin that detects the identical parts of a set of files, and allows you to edit all of them at once? Or are there any other ways of doing this that I might not have thought of?

2
  • 1
    I would take the time to invest into using a precompiler like Jekyll, I think you'll be happier in the long run. It makes making even minor changes to your common pieces of the website very simple, and it is a pretty simple framework to use.
    – heavyd
    Jul 24, 2014 at 14:16
  • One option is to use sed the stream editor. Sed is a non interactive editor, for batch editing multiple files. Also it is part of the same family as vi. Having said that I could use a pre-processor, there is a good rule in software “say it only once”, if you find your self saying/writing it more than once, then you need to modularise (function calls, includes, …). Jul 24, 2014 at 14:16

2 Answers 2

2

In Vim, this problem can be broken down into two parts:

  1. Applying an edit to multiple files
  2. Repeating the same edit

For 1., you're using the DirDo plugin; alternatives would be the built-in :argdo, :bufdo, etc.

For 2., you can use any (combination of) Vim commands. The most powerful one is :substitute, using a :[range] to select the lines, and regular expression to (find the point to) modify the text in those line(s).

You could also record a macro, and re-apply that via :normal! @a. Of, if this is a single edit at the start of a single line, a repeat via :[address]normal! . would suffice (but is limited to a single edit operation and the fact that :normal positions the cursor at the beginning of the line).

0

You can use CtrlSF.vim to search for a given pattern in all the files, and then simultaneously edit the part around the search results using vim-multiple-cursors. There are some limitations though, e.g. you cannot add or delete lines, (see README.md).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .