How to generate a number sequence in file using vi or Vim?

前端 未结 10 2035
野趣味
野趣味 2020-12-07 09:47

Is there a way to generate a number sequence in vi or Vim?

For example, for an arbitrary range of lines i  through j (where i < j

相关标签:
10条回答
  • 2020-12-07 10:34

    Select several lines with V(Shift-v), then type command bellow:

    :let i=1 | '<,'>g/^/ s//\=i . " "/ | let i+=2
    

    Type :help sub-replace-expression to read more.

    0 讨论(0)
  • 2020-12-07 10:36

    The Nexus plugin provides the Series type and an object, s1, of that type used like this:

    :4,8s/^/\=s1.next().' '/
    

    Nexus also comes with an s0 Series object that yields 0 as its first .next() result. Both s0 and s1 use a 1-step increment. All Series objects have a .reset() method which sets them back to their initiated value. New Series objects can be created like the following call:

    let s2 = Series(0, 2)
    

    which creates a 2-step object meeting your second request (yielding: 2, 4, 6, 8, etc.).

    0 讨论(0)
  • 2020-12-07 10:42

    Starting with Vim 8.0 one can use g Ctrl-a, see :help v_g_CTRL-A

    Go to line #4, use Ctrl-v to blockwise select the first character, go down 4 lines, press Shift i, enter 0 (this is 0, followed by Space) and Esc to exit insert mode.

    Now use gv to re-select the previously selected area. Press g Ctrl-a to create a sequence.

    I start with a 0 here, so I can re-select by gv. If you start with a 1, you need to re-select by hand while omitting the first 1.

    Use 2g Ctrl-a to use a step count of 2.


    0 讨论(0)
  • 2020-12-07 10:42

    Here's a dirty trick but then life is composed of these. :)

    ESC :r! for i in $(seq 1 10); do echo "This is line \#${i}"; done
    

    Not cross platform.

    0 讨论(0)
提交回复
热议问题