3

Based on the accepted answer of the following post How to add permanent line numbers to a file?

I would like to have the capacity to select a certain number of lines and add numbers to those particular lines only. Also I require to delete the numbers as well in case I need to renumber those lines.

It will be great to have some keybindings for the commands.

Any help is highly appreciated. Thanks

2

Easy-peasy. Or should I say "it can be done in a simple and straightforward manner" instead? By default Ex commands in the visual mode work over a selected range just the way we want.

" F12 to add line numbers to the selected lines
vnoremap <F12> :s/^/\=line(".").". "<CR>
" The same as above but numbers from 1 to n
"vnoremap <F12> :s/^/\=line(".")+1-line("'<").". "<CR>

" Shift-F12 to remove any numbers from the beginning of selected lines
vnoremap <S-F12> :s/^\d\+\. /<CR>
  • Thanks a lot. :) – cosmicraga Jul 21 at 19:05
2

Command g CTRL-A in visual mode can accomplish that for you. It creates an incrementing sequence, incrementing a number on each line by a subsequent number.

As an example of using this to build a numbered list, somewhere in the middle of your file:

  1. Start visual block selection mode with CTRL-V, at the first column of a line (or a different column if you want the numbers to be added later on, or including an indent.) Move to the last line of the block you want to number.
  2. Type I0. <esc>, where the last key is the ESC key. You're entering insert mode with I (more specifically, visual block insert), then inserting "zero", "dot", "space" (which are inserted to each line), and leaving insert mode with an ESC. When you leave insertion mode, Vim will add the text to each line in the block.
  3. Type gv to recall your selection. This will select all "zeroes" you inserted.
  4. (Optional) Grow your insertion to include the whole numbers. It turns out right now all "zeroes" are covered, but if you're doing a further operation and have lines with two, three or four digit numbers, you'll want to make sure they're fully in the visual block. It's ok if there are further characters in the block. You can use V here to switch to a linewise visual select, or you can keep in a visual block and use $ to select all the way to the end of the line.
  5. Finally, use the g CTRL-A command to increment the zeroes. You type a g (lowercase "g", which will be pending), followed by a Control-A.

After this sequence, you'll end up with a numbered list, with a number on each line, starting at 1. Pretty handy stuff!

Side note: Beware if you use screen, since Control-A is the default hot-key for it and might go to screen and not Vim. The typical way to pass a Control-A to the application is to type Control-A, then "a". The default keybindings for tmux are different, but using Control-A for tmux is a popular choice, in which case Control-A then "a" is typically the way around that too.

You asked about deleting numbers from some lines, which should be easy to do using the usual deletion commands, such as dW, also using visual block mode you can use x, or r + "space" to replace the numbers with spaces to keep the lines where they are.

If you further need to adjust the numbering, typically by decrementing the numbers on the lines that follow (so you're not skipping from, say, 8 to 12), you can then use the CTRL-X command, with a count, to subtract from the numbers in the lines that follow.

Simply select the lines that follow (visual line mode with V is fine), then type, for example, 3 (if that is the number of lines you deleted numbers from), followed by CTRL-X, which will then subtract 3 from the numbers in the lines of your visual selection.

Using g CTRL-A in visual mode is also useful if you want to number paragraphs, since it will skip lines with no numbers in them.

If you have text that looks like this:

0.    This is a paragraph. It uses multiple
      lines to make a point. There are 123
      numbers 456 in the middle of the
      789 text.

0.    This is another paragraph.

0.    And a final one here.

You can then use visual block mode to select the first 6 columns (or even fewer) with the zeroes and use g CTRL-A, which will renumber your paragraphs to 1., 2. and 3..

If you have more than 9 paragraphs, some will get misaligned (as you get to double digit numberings.) In that case, use visual block mode to select the text of the paragraphs themselves and use < to re-align them.

  • 1
    Thanks for the elaborate answer. :) I am a beginner user of vim. Surely I will try to learn more from your answer. – cosmicraga Jul 21 at 19:06
  • 1
    @cosmicraga We all are, there's always more of Vim to learn! 😁 – filbranden Jul 21 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.