3

For example, we have:

1    this is line. please go to file example.txt for more information.
2    this is line. please go to file demo.txt for more information.
3    this is line. please go to file new.txt for more information.

Result:

1    this is line 6555. please go to file example.txt for more information.
2    this is line 6556. please go to file demo.txt for more information.
3    this is line 6557. please go to file new.txt for more information.

Please notice, the number doesn't start from 0 or 1. One needs to specify a range like (11123,11200) or (11023,11201) case by case, where the file lines are actually different.

3
  • Welcome to the Vi and Vim! I believe you are looking for {Visual}g Ctrl-A. I am posting an answer below. Hope it helps!
    – husB
    Commented Apr 7, 2022 at 7:16
  • Howdy data, does the end of the range need to be specified? It looks likes that would be determined by the number of lines in the file and the start of the range.
    – chicks
    Commented Apr 7, 2022 at 19:33
  • Yes. either way. You are right by noticing that.
    – data
    Commented Apr 7, 2022 at 20:02

2 Answers 2

4

The example in :help v_g_Ctrl-A illustrates how to use Visual mode and g Ctrl-A to have a sequence of running numbers on each line. We modify that example slightly and use a general starting number:

  • Place your cursor on the first .
  • Press Ctrl-V and move down until all lines are selected
  • Insert the starting number, for example 11123, with I<space>11123<Esc>.
  • Move down a line, and place your character on the second 11123.
  • Press Ctrl-V and move down until all the numbers are selected (except the first line)
  • Hit g Ctrl-A
4

Short answer

  • Use g<c-a> in visual-block mode to create an incrementing list from selected numbers.

This can be done in a few steps in normal mode.

  • Place your cursor where you want your number on the first line
  • Start visual-block selection with <c-v>
  • Select desired lines
  • Insert the base number, minus one, with I
  • Reselect your numbers with gve for example (gv selects the last selection again)
  • Create the incrementing sequence with g<c-a>

Help topics:

  • :h v_g_ctrl-a
  • :h v_gv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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