Vim command to increase the number of digits ONLY at the cursor position in a longer line?

I have some examples when I want to do the following

In file:

1234 

With the cursor on the 1st digit, I want to use Ctrl-A to increase the number to get

 2234 

instead

 1235 

Are there any vim built-in commands?

Otherwise, you should configure a quick script:

  • Surround digit with start and end space
  • Ctrl-A to enlarge
  • Delete Lead and End Space

So, then map the key?

+8
vim
source share
2 answers

The increment function takes a leading number, similar to most vim commands. 1000 ctrl+a will return 2234 as you wish. If all of your numbers are 4 digits, this will work. Or you can use r2 , which replaces the current character under the cursor with 2 , but that might be too specific.

If you need a script, you can write a macro.

qaa[space][esc]h[ctrl+a]lx

broken:

qa - start recording a macro q and save for registration a

a[space][esc] - add a space after the number

h - return to number

ctrl+a - add one

lx move right and remove the space.

You do not need to add a leading space, because, as you noticed, the ctrl+a function acts on the number as a whole and will always add 1.

+8
source share

You can do this as s<Cr>=<Cr>"+1<Enter> .

Then you can match this with something else, like nnoremap g<Ca> s<Cr>=<Cr>"+1<cr> (you need to use Ctrl-v Ctrl-r to insert <Cr> into this normal map )

Step by step:

s - delete the character under the cursor and start pasting

<Cr>= - start evaluating the expression.

<Cr>" - put the contents of the unnamed register in

See: help i_CTRL-r for more information on this.

+1<Enter> - add 1 to the value and run the command.

+4
source share

All Articles