How do I make Ctrl-A and Ctrl-X increment and decrement alphabetical characters in Vim?
Answer
set nrformats+=alpha
Explanation
By default, Vim's <C-a> and <C-x> commands only increment and decrement numbers (decimal, hex, binary). Adding alpha to nrformats extends this behavior to single alphabetical characters, so pressing <C-a> on a gives b, on z gives a (wrapping), and on Z gives A.
How it works
nrformatscontrols which value formats<C-a>and<C-x>recognize and modifyset nrformats+=alphaappendsalphato the existing list- Vim increments single lowercase or uppercase letters in sequence (
a→b,Z→A) - Wraps around:
z→a,Z→A - Only applies to single characters — it won't increment multi-character words
- Count prefix works:
5<C-a>onagivesf
Example
With set nrformats+=alpha, place the cursor on the character in:
step a
Press <C-a> to get:
step b
Press 5<C-a> to advance 5 letters:
step g
Tips
- Record a macro and use
<C-a>to produce auto-incrementing list labels:a),b),c), ... - Combine with a visual block
g<C-a>on a column of identical letters to create a sequence:a,b,c, ... - To check current value:
:set nrformats? - Reference:
:help nrformats