12

Suppose I'd like to create a list

- "1"
- "2"
- "3"

what Is the smartest way to create such?

My attempt would be to i- "1"ESCyypp to get

- "1"
- "1"
- "1"

and then jump to the second line's 1 r2jr3

BUT: doing this for a list of e.g. 100 (also thinking about the second digit) is not what I want to do with technique above...

I am using nvim v0.3.5 von archLinux, but i don't think, that that does matter.

0

3 Answers 3

17

Depending on your usecase the following might be useful:

Create the entries all with the number "1":

- "1"
- "1"
- "1"
- "1"

Then go to the second "1" and press V to start line-wise visual. Then move down to the last "1". So now all but the first "1" is selected.

Now hit gCtrl-a and you get

- "1"
- "2"
- "3"
- "4"

See :help v_g_CTRL-A

Update: What if a I need a new number "3" and all following should be increased by one?

First insert the new line:

- "1"
- "2"
- "3"
- "3"
- "4"

Then go to the second "3", hit V and move down to the end of the list. Now hit Ctrl-a (without leading g) and the selected numbers are increased by one.

You get:

- "1"
- "2"
- "3"
- "4"
- "5"
6
  • 2
    and additional +1 for the :help v_g_CTRL-A
    – Joel
    Commented May 20, 2019 at 15:34
  • 2
    I had no clue of v_g_CTRL-A, brilliant! you have my vote.
    – padawin
    Commented May 20, 2019 at 15:45
  • Note that v_g_CTRL-A is only available since vim 8.1. Commented May 21, 2019 at 10:51
  • 1
    @Nigu Just tested with vim 9.0.861 and 9.0.1128. It works as described. Did you forget the g before Ctrl-a ?
    – Ralf
    Commented Jan 2, 2023 at 9:23
  • 1
    Seems like I must have forgotten the g somehow before. Good guess, it works now. Thanks.
    – Nigu
    Commented Jan 3, 2023 at 23:37
3

Here's a shorter macro version.

First write a single line with the contents

- "1"

Then type the following:

qqyypCtrl-Aq98@q

  • qq—start recording
  • yyp—Duplicate the line
  • Ctrl-A—Increment the number
  • q—End the recording
  • 98@q—Replay 98 times
1

I would do it as follow:

  • Somewhere in your file (before where you want your list), add a line with the first number - 1 (most likely 0),
  • Make a mark at this place (ma)
  • Make an empty line where you want your list and make another mark there (mb)

Your file would, at this point, look like this (The lines 1 and 2 don't matter, they are just here to say where the marks are):

enter image description here

  • Record the following macro:
    qq                          // Start the macro in register q
    `a                          // go to mark a
    CTRL-a                      // Increase the number
    yiw                         // Copy the number
    `b                          // Go to mark b
    i- "                        // Go in insert mode, insert - "
    <esc>p                      // leave the insert mode and paste the number
    A"<CR><esc>                 // Closes the quote, add a new line and leave the insert mode
    mb                          // Update the mark b to be here
    q                           // Stop recording the macro

    100@q                       // Run the macro 100 times

You can delete the line where the mark a is, as it was just to store the counter.

And voila.

2
  • I like the idea of using a variable. Is there an option to save (and increase) this variable inside a register? That would make the macro much shorter.
    – Joel
    Commented May 20, 2019 at 15:17
  • 1
    I am not sure it is easily possible (without using a : command, which is fine too, but I don't think that would make the macro simpler). Maybe someone will have a suggestion to improve it?
    – padawin
    Commented May 20, 2019 at 15:19

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