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 index:
T 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:
- The section heading,
T, should be kept with the next two topics. - A topic followed by some subtopics should be kept with two subtopics. So
topic 4should be kept withsubtopic 1andsubtopic 2. - The last (sub)topic in a block should be kept with the previous topic of the same level. So
subtopic 5should be kept withsubtopic 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.)
Use
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.
Troubleshooting
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
Installing and running scripts
Questions, comments? Get in touch
Peter Kahrel Script Repository
Hosted as a community service, and with deep appreciation, by CreativePro.com
Scripts by Topic | Script Index
Basic Script Installation and Use
How to Modify or Configure These Scripts
Resources for Scripting and Publishing Automation
About This Repository
Contact Peter Kahrel
Note to users: You run scripts at your own risk: CreativePro and the author accept no liability for anything that may be caused by any of these scripts. Always take the necessary precautions. We encourage you to save backups and test scripts on copies of your documents.
This article was last modified on September 5, 2026
This article was first published on February 5, 2026
Commenting is easier and faster when you're logged in!
Recommended for you
TypeTalk: How to Work with a Lettering Artist
Best practices to follow when the best typographic solution for a design project...
The Right Font Could Save Your Life
Good design can be important for many reasons. It can be critical to the success...
Introducing Between: A New Typographic Triumvirate from Monotype
Between might seem like an odd name for a typeface – until you learn what is beh...
