Saturday, March 5, 2011

Increment and decrement a number in vim

This tip will increment and decrement a number in vim editor.

(a) To increment, hit Ctrl+A
(b) To decrement, hit Ctlr+X

Suppose a number 5 is present in vim editor's buffer. Now, pressing Ctrl+A will increments the number by 1 i.e 6, again if you press Ctrl+A, 6 becomes 7 etc.. same way if you press Ctrl+X, the number will be decremented by 1.

Example:

Let's say, vim buffer contains a number 1

Ctrl+A increments to 2
10Ctrl+A increments to 11
5Ctrl+X decrements to 6
Ctrl+X decrements to 5

Number system conversion in vim editor

Here is the vim editor's way to convert between hexadecimal number system to decimal number system and vice versa.

In the vim editor's command mode, type

(a) Hexadecimal to decimal

:echo 0x111
273
(or)
:echo printf ('%d',0x111)

(b) Decimal to hexadecimal

:echo printf ('%x', 273)
111

Corollary:
1. If you use, %X instead of %x then the numbers will be displayed in uppercase.
2. you can even perform arithmetic like the way below.
:echo printf ('%x',273-173)
64
:echo 0x111-0x10
257