Add Line Numbers

With a search-and-replace action, you can just as easily insert new information into files as you can replace information. The difference is that rather than specifying a search term to be replaced, you use a regular expression that matches a position in the file. Anchor and lookaround tokens are two ways of matching a position rather than actual text with a regular expression. Another way is to simply use the backreference \0 to reinsert the search match into the replacement text.

This example uses an anchor to match the start of the line, and the %MATCHFILEN% placeholder to insert the line numbers.

  1. Select the files you want to add line numbers to in the File Selector.
  2. Select a file format configuration such as “none” or “writable proprietary formats” that does not enable and read-only converters.
  3. Start with a fresh action.
  4. Set the action type to “search and replace”. Set the search type to “regular expression”.
  5. Enter ^ as the search term. This regular expression matches the start of any line in the file, including blank lines.
  6. Enter %MATCHFILEN%.  as the replacement text. %MATCHFILEN% is a placeholder for the sequence number of the match being replaced in the current file. Since the regular expression we’re using matches each line, %MATCHFILEN% is essentially the line number.
  7. Set the target and backup file options as you like them.
  8. Click the Preview button to run a test.
  9. If all looks well, click the Replace button to actually add the line numbers.

Add Line Numbers to Non-Blank Lines

If you only want to add numbers to lines that aren’t blank, you can follow the same steps as above, using the regular expression ^(?=[ \t]*+\S). This regular expression uses positive lookaround to check if the line the caret matched at isn’t blank. It does this by skipping leading whitespace with a possessive character class, followed by \S to check if there are any further characters on the line.

Since blank lines are not matched at all, they are not included in the %MATCHFILEN% count. The non-blank lines will be numbered without gaps in the numbering. The numbering will restart at 1 for each file. To give all lines a unique number throughout the file, use %MATCHN% instead of %MATCHFILEN%.

Add Numbers to Lists in a File

Making the numbering restart at one for each block of non-blank lines is also possible, if slightly more complicated. This can be useful if you have files containing several lists with one item on each line, and the lists are separated by one or more blank lines. This action effectively turns all of the lists into independently numbered lists. The first 4 steps are the same as in the first example.

  1. Enter %MATCHSECTIONN%.  as the replacement text. %MATCHSECTIONN% is a placeholder for the sequence number of the match being replaced in the current section. In this case, a section is one list of items, as we’ll define in the following steps.
  2. Select “split along delimiters” in the “file sectioning” list. We will split up the file so we can number each of the lists in the file independently.
  3. In the “section search” box, enter the regular expression \r\n(?:[ \t]*+\r\n)++. This regular expression matches a line break, followed by one or more line breaks. Each of the following line breaks can optionally be preceded by some whitespace. Effectively, this regular expression matches one or more blank lines, including the leading and trailing line breaks. Since we’re using the “split along delmiters” sectioning type, the files will be split up along blank lines. The main action will only search through the non-blank lines.
  4. Set the target and backup file options as you like them.
  5. Click the Preview button to run a test.
  6. If all looks well, click the Replace button to actually create the numbered lists.