Making Vim use capital letters when incrementing hexadecimal numbers with Ctrl-A

One tricky way:

:nnoremap <C-A> m'<C-A>vUgUTx``

Explanation:

m'         # Create a mark on digit to increment.
<C-A>      # Control-A
v          # Visual select current letter.
U          # Set visual selection (current letter) to uppercase.
gUTx       # Set to uppercase (gU) next movement: (Tx) from current position to previous 'x' letter.
``         # Go to position of previous mark.

So this way creates a small different behaviour from original <C-A>, for example, in this case:

A hex number 0x0ba in lowercase.
      ^--- Cursor position

Will set 0x0ba in 0x0BB but cursor will come back to letter n from number, insteat setting its position in the number incremented. You can play with marks to change this behaviour. I hope this can help.


One can prefix hexadecimal constants with 0X instead of 0x in order to make Vim use capital letters when adding to or subtracting from them via the Ctrl+A and Ctrl+X commands.

Not a perfect solution, but oftentimes it is easier to change the prefix of the affected number to Vim’s liking, increment or decrement it, and then change the prefix back, than to recall how to do that in a general way instead.

Tags:

Vim