Editing Insert line numbers 0 We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free. {{TipImported |id=28 |previous=27 |next=29 |created=2001 |complexity=intermediate |author= |version=7.0 |rating=159/57 |category1= |category2= }} The <code>:set number</code> command displays line numbers. This tip, however, shows how to insert line numbers into a file, or into just a section. Also, the option to print with line numbers is given (Vim can print lines with numbers, and does not need the numbers in the file). ==Using Vim== The [[Search and replace|:s]] command can be used to insert line numbers before each line: <pre> :%s/^/\=printf('%-4d', line('.')) </pre> The pattern <code>^</code> matches the start of every line, and the replacement <code>\=</code> is the result of evaluating the following expression. That expression uses <code>printf()</code> to format the number of the current line: <code>%-4d</code> is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (<code>%4d</code> is right-aligned, and <code>%04d</code> inserts leading zeroes). To number a section, specify which lines should be numbered with a [[Ranges|range]]. For example, press <code>V</code> to select the first line, then press <code>j</code> to extend the selection down until all required lines are selected. Then type the following command (do not type <code>'<,'></code> as that is inserted by Vim): <pre> :'<,'>s/^/\=printf("%d.\t", line(".") - line("'<") + 1) </pre> The above example has a format string of <code>"%d.\t"</code> 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): <pre> :'<,'>s/^\S/\=printf("%d.\t", line(".") - line("'<") + 1) </pre> 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: <pre> :'<,'>g/^\S/s/^/\=printf("%d.\t", Inc()) </pre> The above requires the <code>Inc()</code> function from [[Making a list of numbers|here]]. In addition, you need to set the value for the first line number before selecting any lines. To do that enter: <pre> :let i = 1 </pre> ==Using <code>nl</code>== On Unix based systems, the ''number lines'' utility can be used to insert numbers by filtering the whole buffer: <pre> :%!nl -ba </pre> The <code>nl</code> utility has many options to control which lines are numbered and how the numbers are formatted. To number a section, specify which lines should be numbered with a [[Ranges|range]]. For example, press <code>V</code> to select the first line, then press <code>j</code> to extend the selection down until all required lines are selected. Then type <code>:!nl -ba</code>. You will see (the <code>'<,'></code> is inserted by Vim): <pre> :'<,'>!nl -ba </pre>When using the <code>nl</code> utility up to 6 leading spaces will be added by default, and the number will be separated from the line content by a single tab character. To reduce the leading spaces to 2 (for numbering up to 99) and to replace the separator with '. ' you can use the following flags: :'<,'>!nl -s '. ' -w 2 ==Printing with line numbers== There is no need to insert line numbers if all that is wanted is to have lines numbered in a printout. Instead, use the following command to tell Vim to insert line numbers on printing (change <code>y</code> to <code>n</code> to turn this off): <pre> :set printoptions=number:y </pre> ==Comments== [[VimTip569|Insert line numbers with a Perl filter]] is another method. Loading editor Below are some commonly used wiki markup codes. Simply click on what you want to use and it will appear in the edit box above. Insert: – — … ° ≈ ≠ ≤ ≥ ± − × ÷ ← → · § Sign your username: ~~~~ Wiki markup: {{}} | [] [[]] [[Category:]] #REDIRECT [[]] <s></s> <sup></sup> <sub></sub> <code></code> <blockquote></blockquote> <ref></ref> {{Reflist}} <references/> <includeonly></includeonly> <noinclude></noinclude> {{DEFAULTSORT:}} <nowiki></nowiki> <!-- --> <span class="plainlinks"></span> Symbols: ~ | ¡ ¿ † ‡ ↔ ↑ ↓ • ¶ # ¹ ² ³ ½ ⅓ ⅔ ¼ ¾ ⅛ ⅜ ⅝ ⅞ ∞ ‘ “ ’ ” «» ¤ ₳ ฿ ₵ ¢ ₡ ₢ $ ₫ ₯ € ₠ ₣ ƒ ₴ ₭ ₤ ℳ ₥ ₦ № ₧ ₰ £ ៛ ₨ ₪ ৳ ₮ ₩ ¥ ♠ ♣ ♥ ♦ View this template This field is a spam trap. DO NOT fill it in! Edit summary Preview Mobile Desktop Show changes