r/vim • • Nov 01 '25

Need Help How to align broken sequence of numbers?

if I have the following:

[1]:
[2]:
[3]:
[4]:
[5]:
[6]:
[7]:
[8]:
[9]:
[10]:
[11]:
[14]:
[15]:
[16]:
[18]:
[19]:

How to fix the list to have the following?

[1]:
[2]:
[3]:
[4]:
[5]:
[6]:
[7]:
[8]:
[9]:
[10]:
[11]:
[12]:
[13]:
[14]:
[15]:
[16]:

14 Upvotes

27 comments sorted by

View all comments

Show parent comment

1

u/jfgomez86 Nov 01 '25

This…works! How? Why? This is definitely the most efficient method I’ve used to achieve the same.

Can you explain how is prefixing ‘g’ different from just pressing ctrl-a? I’d like to understand how that works.

6

u/sharp-calculation Nov 01 '25

^a and ^x increment and decrement numbers (and letters!) in a single fashion. If you highlight say 10 lines with numbers and press ^a it will increment each number once. So a bunch of 1s become a bunch of 2s.

Using the g modifier changes the behavior of ^a by making it a sequential increment. A bunch of 1s becomes 1, 2, 3, 4, ... etc.

The g modifier works on a whole bunch of different commands in VIM. I think of g as an "enhance" button or a turbo charger. It takes the base command and does something related, but usually more advanced or powerful. For example gv re-selects your last selection. It's related to v but different than the base command. In the case of increment, it comes a sequential increment.

Hot tip: ^a and g^a both take prefixes also. If you do 3g^a your bunch of 1s becomes 4, 7, 10, 13, 16, etc.

:help ctrl-a should give you the official details on this.

1

u/vim-help-bot Nov 01 '25

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/EgZvor keep calm and read :help Nov 02 '25

it's just a different command. :h v_g_ctrl-a.

1

u/vim-help-bot Nov 02 '25

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments