If you have a lot of similar lines, here is a mapping that could help you:
nnoremap <silent> ,x :let [a, b] = map(split(getline('.'), ','), 'eval(v:val)') <bar> execute "s/^.*$/'" . join(range(a,b), "','") . "'"<CR>
Hit ,x
on a line following the form 'a','b'
(with a < b), and it should be converted into:
'a','a+1','a+2',...,'b'
.
The mapping executes 2 Ex commands separated by a pipe symbol (<bar>
).
The 1st command stores the 2 numbers on the line in the variables a
and b
.
:let [a, b] = map(split(getline('.'), ','), 'eval(v:val)')
It can be broken down like this:
getline('.') returns the current line
split(g, ',') splits the string g where a comma is found
example: split('1,2,3', ',') returns [1, 2, 3]
here g stands for: getline('.')
map(s, 'eval(v:val)')
s being a list, map() takes each of its item and transforms it with
the operation: eval(v:val)
where v:val is an item of s
example: map(['1', '2', '3'], 'eval(v:val)') returns [1, 2, 3]
eval() evaluates a string; useful to get a number inside quotes
example: eval('123') returns 123
here s stands for: split(getline('.'), ',')
let [a, b] = map(s, 'eval(v:val)')
stores in the variables a and b the 2 items inside the list returned
by the map() function
The 2nd command substitutes the current line with a new one, containing the missing numbers between a
and b
.
execute "s/^.*$/'" . join(range(a,b), "','") . "'"
It can be broken down like this:
range(a,b) returns the list of numbers between a and b
example: range(1,3) returns [1, 2, 3]
join(r, "','") returns a string which concatenates all the items from the list r
separated by the 3 characters ','
example: join([1, 2, 3], "','") returns "1','2','3"
here r stands for: range(a,b)
execute "s/^.*$/'" . j . "'"
executes the Ex command resulting from the concatenation of 3 strings:
"s/^.*$/'"
j
"'"
The resulting Ex command is a substitution command (:s/^.*$/'j')
which substitutes the whole line (^.*$) with the string "'j'"
here j stands for: join(range(a,b), "','")