6

I had the following piece of Markdown text that I wanted to edit:

 * 2-6 for a moka pot
 * 7-10 for a drip machine
 * 5-12 for a pour-over

When I had the cursor over the number 6 in the first bullet point, pressing Ctrl-A should “Add [count] to the number or alphabetic character at or after the cursor”. Instead of giving 7, Vim decreased the number from 6 to 5. When I tried to decrease the number using Ctrl-X, it incremented instead. It was like both commands were doing the opposite of what they should.

I checked to see if some key-mapping was over-writing Vim’s defaults but running both :map <C-A> and :nmap <C-A> printed

No mapping found

Ditto for C-X. What’s going on?

2 Answers 2

10

Just use set nrformats+=unsigned. Then vim will treat all numbers as positive and ignore the hyphen.

2
  • Wow! There’s an option for everything! The alpha setting could also be useful in certain circumstances. Thanks. Nov 14, 2021 at 0:50
  • a workaround, if you do not want to :set nrformats+=unsigned is to visually select only the number and then use Ctrl-A/Ctrl-X Nov 15, 2021 at 7:51
7

It turns out that if a number is preceded by a hyphen-minus character (ASCII 45, 0x2D), Vim interprets the number as being a negative integer so Vim was actually adding +1 to -6, giving -5.

The solution was to replace the hyphens with en dashes to represent the range of values:

 * 2–6 for a moka pot
 * 7–10 for a drip machine
 * 5–12 for a pour-over

Note: Jason Diamond’s UniCycle plugin makes it easy to replace ASCII characters with typographically appropriate Unicode characters.

1
  • 1
    Only after typing out and posting both question and answer, I discovered vi.stackexchange.com/q/5219/1608 I figured I'd keep this here as my problem was slighly different and I had come up with a solution that worked with Vim (but wouldn't work with dates. Nov 13, 2021 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.