How to write Line Numbers into Text Files
In Office programs such as Microsoft Word, Libre Office or OpenOffice, it is easy to add line numbers to your DOC, DOCX or ODT documents. Depending on the Office suite you are using, all it takes is a few clicks in the column or page settings of the document and the program automatically takes care of the rest. But what about simple plain text files that do not contain any meta level for formatting?
Also for these files there is a simple solution: Just use the "Insert Line Numbers" function from the TextConverter, which you can find on the right side of the main window under "Actions > Lines > Line Numbers".
Configure Start Value and Interval
After you have activated this action via the checkbox in front of it, you can control your line numbers via two options:
- Start: With this field you can define a free starting value at which the counting of your line numbers begins. In other words, this is the number that the first line will have. Typically this is "1", but you can also make the numbers start at "0" or any other value - for example if you want to number multiple text files consecutively.
- Interval: Line numbers typically do not appear before each line but, for example, only before every fifth or tenth line of a text. You can control this property with this value. Set this field to "1" if you want to insert numbers before each line, "2" if you only want every second line to have a number, and so on.
Example of Numbering every fifth Line
Let's look at an example. Suppose we have a text file with the following original content:
abcdef
bcdefg
cdefgh
defghi
efghij
fghijk
ghijkl
hijklm
ijklmn
jklmno
Now we want to add line numbers to this text file. To do this, we activate the action "Actions > Lines > Line Numbers" in the TextConverter and select a start value of "1" and an interval of "5". As a result we get the following text:
abcdef
bcdefg
cdefgh
defghi
5 efghij
fghijk
ghijkl
hijklm
ijklmn
10 jklmno
As you can see, all lines are automatically indented to such an extent that each line starts at the same character position after the numbers, which means that when using a font with a fixed character width (such as Courier or Consolas), each line starts left-aligned after the "column" with the line numbers.
In our example, this is character position 4. The longest line number is "10" with two digits, which, including a space, results in this start at position 4. If our line numbers for longer texts were of three or four digits, the start of the actual line would shift accordingly to character position 5 or 6 for all characters.
Insert automatic Line Breaks
In contrast to Office documents, text files have no internal formatting to limit the page width, which would result in long lines automatically breaking in the same place, regardless of the real line breaks. Instead, the wrapping of long lines typically depends on the window size of the text editor that is currently displaying the file. Depending on whether you enlarge or reduce the size of your editor, the line breaks occur at a different place in the text.
Of course, this behavior is not beneficial for our inserted line numbers. Finally, the line counting and numbering only takes place based on the real line breaks within our file, which means that long automatically wrapped lines can appear unnumbered depending on the editor window size.
However, the TextConverter also offers us a simple solution for this problem: by simply combining the action "Actions > Lines > Line Numbers" with the action "Actions > Lines > Line Breaks" by activating this second action in addition to the first. In this action we are only interested in the following option (the other options should be disabled):
- Insert Line Break after this Number of Characters: Here you can define any minimum number of characters, after which longer lines will then automatically be broken by inserting a real line break. If necessary, several times.
This allows us to artificially create a fixed "page width" for our text, which of course should, if possible, not be wider than the editor window or the other display location used later. Of course, the breaks take into account spaces and words and are not just inserted in the middle of a word or such things after simply counting the required number of characters.
Let’s also look at a practical example for this. Suppose we have the following original text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
If we add line numbers to this text (for demonstration purposes, inserted here in each individual line), we would get the following unpleasant result (assuming the editor width is 50 displayable characters):
1 Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
2 Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
3 Duis autem vel eum iriure dolor in hendrerit in
vulputate velit esse molestie consequat, vel
illum dolore eu feugiat nulla facilisis at vero
eros et accumsan et iusto odio dignissim qui
blandit praesent luptatum zzril delenit augue
duis dolore te feugait nulla facilisi.
4 Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh euismod
tincidunt ut laoreet dolore magna aliquam erat
volutpat.
This can be remedied by activating the option "Actions > Lines > Line Breaks > Insert Line Break after this Number of Characters", which we have set here to 45 characters (with an insertion interval for the line numbers of 5 lines):
Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud
5 exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat
10 cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.
Duis autem vel eum iriure dolor in hendrerit
in vulputate velit esse molestie consequat,
vel illum dolore eu feugiat nulla facilisis
15 at vero eros et accumsan et iusto odio
dignissim qui blandit praesent luptatum
zzril delenit augue duis dolore te feugait
nulla facilisi.
Lorem ipsum dolor sit amet, consectetuer
20 adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat.
As you can see, this additional action resulted in an automatic line break in such a way that now no line is longer than the specified maximum number of characters and as a result, our inserted line numbers are displayed sensibly even with a smaller editor window or other available display width.