Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increment numbers in visual vertical block selection in emacs evil

I have been using https://github.com/vim-scripts/increment.vim--Avadhanula for vim for years.

How could i leverage functionality similarly in emacs?

The idea be something like this:

given a list such as the following:

the_array[0] 
the_array[0] 
the_array[0] 

I want to be able to select all the zeros, issue a command, and have the text replaced with this:

the_array[0] 
the_array[1] 
the_array[2] 

I am using emacs evil-mode if it matters, hoping to do the equivelent of a block select for the region of numbers i want to increment.

Thanks,

like image 799
joefromct Avatar asked Mar 17 '23 13:03

joefromct


1 Answers

Say you start with this text in your buffer:

the_array[0]
the_array[0]
the_array[0]

Move the cursor to the first 0 and use C-v 2 j d to delete all the zeros. C-v } F 0 d will work for an arbitrary number of lines as long as the last the_array[0] line is at the end of a paragraph, but note that it requires (setq evil-cross-lines t) in your config.

Regardless of how you delete the 0's, you should now have this in your buffer:

the_array[]
the_array[]
the_array[]

Select all the ending ]'s in the same way you selected the 0's. Now press C-u C-x r N 0 <Enter> <Backspace> <Enter>. C-x r N runs rectangle-number-lines, which prompts for a starting number and format string when invoked with a prefix argument (C-u). We specify that it should start and 0 and insert only the numbers (<Backspace> removes a trailing space in this case).

Your buffer should now contain this:

the_array[1]
the_array[2]
the_array[3]
like image 145
Gordon Gustafson Avatar answered Apr 27 '23 19:04

Gordon Gustafson