vimtricks.wiki Concise Vim tricks, one at a time.

How do I decrement a visual block of numbers as a sequence?

Answer

g<C-x>

Explanation

Most people know <C-x> decrements one number under the cursor, but g<C-x> in Visual mode performs a sequential decrement across the selection. Instead of subtracting the same amount everywhere, Vim walks each matched number and continues the series. This is useful when you need descending IDs, reverse-ordered test data, or to renumber blocks without external scripts.

How it works

  • Enter a Visual selection that spans multiple numeric fields
  • g<C-x> applies decrement-as-sequence semantics
  • Each next number is decremented relative to the previous result, creating a descending run

This is the counterpart to g<C-a> (increment sequence), but often overlooked.

Example

Given:

item_10
item_10
item_10

Select the three lines in Visual mode and press:

g<C-x>

Result:

item_10
item_9
item_8

You get a clean descending sequence in one action.

Tips

  • Use Visual Block mode when numbers are aligned in a specific column
  • Adjust 'nrformats' if you need hex or other number formats handled consistently
  • Pair with macros for large structured edits where numbering and text changes happen together

Next

How do I change the key that opens Vim's command-line window?