Set Keep options in an index

You can't use InDesign's Keep with next n lines option indexes. First, in an index you typically want certain topics to keep together. These topics are in separate paragraphs, and because Keep with next n lines targets the number of lines in the next paragraph only, it's of no use.

But even if you could keep a number of paragraphs together, it's only in certain circumstances (such as after a section heading) that paragraphs need to be kept together. So the keep options should be conditional: keep with the next n lines if followed (or preceded) by a certain paragraph type.

Similarly, you want to keep the last topic in a block (a block of level-1 topics, or a block of level-2 topics) with the previous topic of the same level in order to avoid widowed topics: the last paragraph of a block at the top of a column on its own.

Take this schematic indix:

topic 1
topic 2
topic 3
topic 4
  subtopic 1
  subtopic 2
  subtopic 3
  subtopic 4
  subtopic 5
topic 5

We want to keep the following items together:

  1. The section heading, T, should be kept with the next two topics.
  2. A topic followed by some subtopics should be kept with two subtopics. So topic 4 should be kept with subtopic 1 and subtopic 2.
  3. The last (sub)topic in a block should be kept with the previous topic of the same level. So subtopic 5 should be kept with subtopic 4.

(Note: I've using line and topic as if all topics are single-line paragraphs. It doesn't really matter though, it has very little influence on the end result.)


You can set two options in the script: the number of topics to be kept with section headings and the number of subtopics to be kept with their parent topic. To set these options, open the script in a text editor and look for these two lines at the beginning of the script:

var keepLinesAfterSectionHeading = 2;
var keepSubtopicsWithParentTopic = 2;

Their meaning will be clear: keepLinesAfterSectionHeading determines how many topics are kept with the section heading, keepSubtopicsWithParentTopic specifies how many subtopics should be kept with their parent topic. If you want to use different values, replace what you see in the script with those new values and save the script as a plain text file.

To run the script, select one of the text frames that the index sits in or click anywhere in the index and run it from InDesign's Scripts panel. The script has no interface.


To check how the script applies the keeps it's easiest to make the keeps visible. You can use the show-keeps.jsx script for that (use the link, below). This script applies conditions to paragraphs that have keep with next or keep with previous applied.

You'll see that the index-keeps script uses only keep with previous. This makes the script much easier, and captures the fact that keeping topics with section headings and keeping subtopics with parent topics in fact amount to the same thing; they differ only in the level at which they operate. Note that this script is fairly slow.

To remove the highlights, simply delete the two condions whose name starts with keep.

Download the index-keeps script

Download the show-keeps script

Back to index fixes

Back to script index

Installing and running scripts

Questions, comments? Get in touch