Logo

dev-resources.site

for different kinds of informations.

Quick vim tips to generate and increment numbers

Published at
9/25/2019
Categories
vim
productivity
tips
numbers
Author
iggredible
Categories
4 categories in total
vim
open
productivity
open
tips
open
numbers
open
Author
10 person written this
iggredible
open
Quick vim tips to generate and increment numbers

There are times when I need to either increment or generate a column of numbers quickly in vim. Vim 8/ neovim comes with useful number tricks.

magician

I will share two of them here.

Quickly generate numbers with put and range

You can quickly generate ascending numbers by

:put=range(1,5)
Enter fullscreen mode Exit fullscreen mode

This will give you:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

We can also control the increments. If we want to quickly generate descending number, we do:

:put=range(10,0,-1)
Enter fullscreen mode Exit fullscreen mode

Some other variations:

:put=range(0,10,2) // increments by 2 from 0 to 10 
:put=range(5)      // start at 0, go up 5 times
Enter fullscreen mode Exit fullscreen mode

This trick might be helpful to generate a list when taking notes. In vim, display current line, we can use line('.'). This can be combined with put/range. Let's say you are currently on line # 40. To generate numbers to line 50, you do:

:put=range(line(','),50)
Enter fullscreen mode Exit fullscreen mode

And you'll get:

40 // prints at line 41.
41
42
43
44
45
46
47
48
49
50
Enter fullscreen mode Exit fullscreen mode

To adjust line number above, you change it to be :put=range(line('.')+1,50) to show the correct line number.

Quickly increment column of numbers

Suppose we have a column of numbers, like the 0's in HTML below:

<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
Enter fullscreen mode Exit fullscreen mode

If we want to increment all the zeroes (1, 2, 3, ...), we can quickly do that. Here is how:

First, move cursor to top 0 (I use [] to signify cursor location).

<div class="test">[0]</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
<div class="test">0</div>
Enter fullscreen mode Exit fullscreen mode

Using VISUAL BLOCK mode (<C-v>), go down 8 times (<C-v>8j) to visually select all 0's.

<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
<div class="test">[0]</div>
Enter fullscreen mode Exit fullscreen mode

Now type g <C-a>. Voila!

<div class="test">1</div>
<div class="test">2</div>
<div class="test">3</div>
<div class="test">4</div>
<div class="test">5</div>
<div class="test">6</div>
<div class="test">7</div>
<div class="test">8</div>
<div class="test">9</div>
Enter fullscreen mode Exit fullscreen mode

shocked

Wait a minute... what just happened?

Vim 8 and neovim has a feature that automatically increment numbers with <C-a> (and decrement with <C-x>). You can check it out by going to :help CTRL-A.

We can also change the increments by inserting a number ahead. If we want to have 10,20,30,... instead of 1,2,3,..., do 10g<C-a> instead.

Btw, one super-cool-tips with <C-a> and <C-x> - you can increment not only numbers, but octal, hex, bin, and alpha! For me, I don't really use the first three, but I sure use alpha a lot. Alpha is fancy word for *alpha*betical characters. If we do set nformats=alpha, we can increments alphabets like we do numbers.

Isn't that cool or what? Please feel free to share any other number tricks with Vim in comment below. Thanks for reading! Happy vimming!

numbers Article's
29 articles in total
Favicon
Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Master Number Conversions with Ease: From Binary to Hexadecimal & More!
Favicon
Leetcode โ€” 2769. Find the Maximum Achievable Number
Favicon
Leetcode โ€” 3190. Find Minimum Operations to Make All Elements Divisible by Three
Favicon
Floating Point Representation (IEEE 754 ISSUE)
Favicon
Error in Javascript
Favicon
Million day archivio
Favicon
Exploring the Mystical Language of Numbers.
Favicon
How to Check if a Value is Within a Range of Numbers in JavaScript
Favicon
BINARY NUMBERS
Favicon
Prime Numbers โ€“ Definition with Examples
Favicon
Generator Random Numbers
Favicon
Binary representation of numbers
Favicon
Get all separate digits of a number (TypeScript)
Favicon
The fascinating diagonals of Pascal's Triangle
Favicon
Convert long numbers to string
Favicon
Patterns in the multiples of 2, 5, 10 and 3
Favicon
Your startup & the conspiracy of numbers
Favicon
Teaching the computers to write number names using JavaScript
Favicon
Learn Bash by making a game
Favicon
Convert Decimal to Binary for IOT Product With Vue.js
Favicon
Hunt some strange numbers with Python.
Favicon
Generate unique (non-repeating) random numbers
Favicon
Developer 2021 - Projections based from Stack Overflow's 2020 Developer Survey
Favicon
Learn Python: Numbers
Favicon
Find Valid Phone Numbers, Anywhere!
Favicon
Quick vim tips to generate and increment numbers
Favicon
What is the type of NaN?
Favicon
Recreating Excelโ€™s ISNUMBER in a Numbers Spreadsheet

Featured ones: