Note, this question is about vi, not vim or any other clone. I work on AIX (IBMs UNIX) and the (to my knowledge standard-conforming) vi editor.

Quite often I need to enumerate a list of values, like this:

typeset bar[1]="foo-1"
typeset bar[2]="foo-2"
typeset bar[3]="foo-3"

Up to now the only way to accomplish this that I know of is to manually change the number-parts in every line after copying the original line n times.

Is there a way to automate this process?

Note: I have searched and looked at How to insert repetitive text with minor difference in vim? but the suggested solution only works in vim and also involves writing the list of what in my case are enumerations down first manually. This is exactly what I would like to avoid. Lists of arbitrary texts (like in the linked case) cannot be guessed from one another, but (ranges of) numbers can: on "1" follows "2", "3" and so on.

PS: my OS version and the version information of my vi is:

# oslevel -s

# lslpp -l bos.rte.edit
  Fileset                      Level  State      Description
  Path: /usr/lib/objrepos
     bos.rte.edit     COMMITTED  Editors
  Path: /etc/objrepos
     bos.rte.edit     COMMITTED  Editors

# what /usr/bin/vi
        98 src/bos/usr/ccs/lib/libc/POWER/longjmp.s, libcsys, bos720 6/27/13 13:24:34
        61      1.16  src/bos/usr/ccs/lib/libc/__threads_init.c, libcthrd, bos720 8/2/07 13:09:21
        88  1.24  src/bos/usr/bin/ex/ex_put.c, cmdedit, bos720 4/10/06 07:47:54
        50  1.13  src/bos/usr/bin/ex/ex_data.c, cmdedit, bos720 1/21/06 03:54:52
        09  1.11  src/bos/usr/bin/ex/printf.c, cmdedit, bos720 8/7/00 10:14:15
        69      1.17  src/bos/usr/bin/ex/ex_get.c, cmdedit, bos720 3/21/05 23:25:35
        76  src/bos/usr/bin/ex/ex_v.c, cmdedit, bos720 3/11/08 01:28:00
        38  src/bos/usr/bin/ex/ex_tty.c, cmdedit, bos720 3/31/04 07:01:03
        40  1.28  src/bos/usr/bin/ex/ex_cmdsub.c, cmdedit, bos720 1/10/07 04:44:31
        27  src/bos/usr/bin/ex/ex_temp.c, cmdedit, bos72Q, q2018_46A8 10/25/18 02:17:34
        97  1.25  src/bos/usr/bin/ex/ex_vget.c, cmdedit, bos720 3/24/06 05:30:01
        01      1.24  src/bos/usr/bin/ex/ex_vops.c, cmdedit, bos720 8/29/08 00:51:01
        03      1.16  src/bos/usr/bin/ex/ex_vops3.c, cmdedit, bos720 3/22/05 00:02:42
        00  src/bos/usr/bin/ex/ex_voper.c, cmdedit, bos720 3/22/05 01:39:20
        99      1.15  src/bos/usr/bin/ex/ex_addr.c, cmdedit, bos720 10/21/05 06:32:51
        98  1.26  src/bos/usr/bin/ex/ex_re.c, cmdedit, bos720 6/12/06 02:37:59
         Version 3.10
        18  src/bos/usr/bin/ex/ex_cmds.c, cmdedit, bos72V, v2019_46A1 10/25/19 05:52:42
        80  src/bos/usr/bin/ex/ex_io.c, cmdedit, bos720 9/13/13 01:48:13
        68  1.15  src/bos/usr/bin/ex/ex_unix.c, cmdedit, bos720 6/12/06 02:43:44
        09  1.13  src/bos/usr/bin/ex/ex_set.c, cmdedit, bos720 1/21/06 03:54:54
        99  src/bos/usr/bin/ex/ex_vmain.c, cmdedit, bos720 7/3/08 03:31:08
        02 1.28  src/bos/usr/bin/ex/ex_vops2.c, cmdedit, bos720 8/7/08 00:01:05
        05  1.21  src/bos/usr/bin/ex/ex_vput.c, cmdedit, bos720 3/21/05 23:17:43
        84  1.13  src/bos/usr/bin/ex/ex_vadj.c, cmdedit, bos720 3/21/05 23:36:28
        06  1.13  src/bos/usr/bin/ex/ex_vwind.c, cmdedit, bos720 3/21/95 17:33:51
        29  src/bos/usr/bin/ex/ex_cmds2.c, cmdedit, bos720 8/2/07 07:28:08
        19  1.33  src/bos/usr/bin/ex/ex_subr.c, cmdedit, bos720 3/21/05 21:50:05
        67  src/bos/usr/bin/ex/ex.c, cmdedit, bos720 9/3/09 04:27:29
  • Just to be sure, what is the output of $ vi --version?
    – romainl
    Nov 30, 2023 at 12:43
  • The output os "unknown option" - which is understandable because introducing options by "--" is a GNU specialty which doesn't work in ordinary UNIX systems. I have added my OS version and the version information for vi in the question
    – bakunin
    Nov 30, 2023 at 12:48

1 Answer 1


Vi didn't have Vim's <C-a> or g<C-a> or \= or expressions or any kind of scripting, etc. so all the slick Vim methods are basically irrelevant. The "Improved" in "Vi Improved" is not a joke, it really is.

But vi had :read… and you have a shell.

The following may or may not work out of the box with your particular shell but it is, IMO, the smartest way to do it in vi:

:r !for n in {1..10}; do echo "typeset bar["$n"]=\"foo-"$n"\""; done

or, with $ man seq:

:r !for n in $(seq 10); do echo "typeset bar["$n"]=\"foo-"$n"\""; done

FWIW, looping is not necessary if the incremented number is only present once in each desired line, which makes the syntax a lot more concise:

:r !echo "foo-"{1..10}"-bar"

But, with the number present more than once, keeping up with the syntax above:

:r !echo "foo-"{1..10}"-bar-"{1..10}"-baz"

would result in 10×10 lines, which is not really the desired outcome. Hence the loop.

  • Using the "r" subcommand with a shell construct is clever and in hindsight I wonder why I didn't come to this conclusion myself. It was already a gain to join this community, thank you!
    – bakunin
    Nov 30, 2023 at 13:35
  • Just as an aside: vim IMHO is in some ways an improvement, but in other ways rather overdesigned. If I'd need an emacs I could use the one there is already, but I like vi because of its simplicity - and the fact that I don't have to load a GB of LISP code at the startup just to have an OS without a bootloader, not an editor.
    – bakunin
    Nov 30, 2023 at 13:38
  • Well, Vim sits somewhere in the middle. You can definitely use it the way you have always used vi, with the confidence that there is quite a bit of power under the bonnet for when you are ready. FWIW, most people use Vim or a Vim fork, these days, so vi knowledge is increasingly hard to find. Make sure you mention your requirements clearly in your next questions or you will get lots of unusable advices.
    – romainl
    Nov 30, 2023 at 13:46
  • I've come to always refer to this command as :read! (or read-exclamation-mark) because that's how it's indexed in Vim's :help. Just living up to my reputation as being nitpicky.
    – Friedrich
    Nov 30, 2023 at 14:00
  • @Friedrich as long we don’t confuse :write! with :write !
    – D. Ben Knoble
    Nov 30, 2023 at 15:46

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.