rdfs:comment
| - The :s command can be used to insert line numbers before each line: :%s/^/\=printf('%-4d', line('.')) The pattern ^ matches the start of every line, and the replacement \= is the result of evaluating the following expression. That expression uses printf() to format the number of the current line: %-4d is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (%4d is right-aligned, and %04d inserts leading zeroes). :'<,'>s/^/\=printf("%d. ", line(".") - line("'<") + 1) :'<,'>s/^\S/\=printf("%d. ", line(".") - line("'<") + 1) :let i = 1
|
abstract
| - The :s command can be used to insert line numbers before each line: :%s/^/\=printf('%-4d', line('.')) The pattern ^ matches the start of every line, and the replacement \= is the result of evaluating the following expression. That expression uses printf() to format the number of the current line: %-4d is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (%4d is right-aligned, and %04d inserts leading zeroes). To number a section, specify which lines should be numbered with a range. For example, press V to select the first line, then press j to extend the selection down until all required lines are selected. Then type the following command (do not type '<,'> as that is inserted by Vim): :'<,'>s/^/\=printf("%d. ", line(".") - line("'<") + 1) The above example has a format string of "%d. " which inserts a left-aligned number, followed by a period and a tab character. Each line in the selected range is numbered. Use the following if you want to only number non-blank lines (it finds the start of a line followed by a character that is not whitespace): :'<,'>s/^\S/\=printf("%d. ", line(".") - line("'<") + 1) In the above, blank lines are counted, but do not have a number inserted (the inserted numbers would be 1, 2, 3, 5, 6 if line 4 was blank). The following alternative does not number blank lines and does not skip line numbers: :'<,'>g/^\S/s/^/\=printf("%d. ", Inc()) The above requires the Inc() function from here. In addition, you need to set the value for the first line number before selecting any lines. To do that enter: :let i = 1
|