Wikia

Vim Tips Wiki

Watchlist Recent changes

Increasing or decreasing numbers

Redirected from VimTip30

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 30 Printable Monobook Previous Next

created 2001 · complexity basic · author neuron · version 7.0


You can increment or decrement a number by pressing Ctrl-A or Ctrl-X when in Normal mode. The number can be at the cursor, or after the cursor.

The number can be decimal, hexadecimal or octal. You can also increment or decrement a single letter ("a...", "b...", "c..."). This is controlled with the 'nrformats' option.

Ctrl-A is very useful in a macro. As an example, suppose you type the line:

101 This is an item.

In Normal mode, enter the following to record a macro into the a register. This macro yanks the current line, then pastes it below, then increments the number.

qa
Y
p
Ctrl-A
q

Now type 15@a to perform the macro 15 times. You will see:

101 This is an item.
102 This is an item.
103 This is an item.
104 This is an item.
and so on

On Windows, your _vimrc file may source mswin.vim. That script sets Ctrl-A to Select All. If you want to use Ctrl-A in Normal mode to increment a number, you need:

:nunmap <C-A>

Alternatively, you can keep the Select All behaviour and use a different map for incrementing. To use Ctrl-X to decrement and Ctrl-Shift-X to increment, you need:

:nnoremap <C-S-x> <C-a>

Making a listEdit

It is easy to insert a list of ascending numbers, for example, this command inserts five lines after the current line:

:put =range(11,15)

The five lines are:

11
12
13
14
15

If wanted, the lines can be inserted after a particular line number, for example :123put =range(11,15) inserts them after line number 123, while :0put =range(11,15) inserts the lines at the start of the buffer, and :$put =range(11,15) inserts them after the last line.

An equivalent command is :call append(123,range(11,15))) to insert the five lines after line number 123, for example.

The list of numbers can be formatted. For example, the following inserts 150 lines, where each line contains a number displayed in four columns with leading zeros.

:put =map(range(1,150), 'printf(''%04d'', v:val)')

The results range from 0001 to 0150. The map() function replaces each value with the result of the expression, which must be given as a string (the double '' presents a single apostrophe when inside an apostrophe-quoted string). In the expression, v:val represents each value from the list in the first argument.

Here is another example, using a loop rather than map():

:for i in range(1,10) | put ='192.168.0.'.i | endfor

Executing this command inserts the following after the current line:

192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10

ReferencesEdit

CommentsEdit

About :nnoremap <C-S-x> <C-a> – I don't think Vim handles Shift when considering Control characters. That is, Ctrl-Shift-x is the same as Ctrl-x in all versions of Vim on all systems, I think. Here is an experiment (on my Windows system, pressing either Ctrl-x or Ctrl-Shift-x will produce the same output, namely whichever was defined last):

:nnoremap <C-x> :echo 'Ctrl-x'<CR>
:nnoremap <C-S-x> :echo 'Ctrl-Shift-x'<CR>
:nnoremap <C-S-y> :echo 'Ctrl-Shift-y'<CR>
:nnoremap <C-y> :echo 'Ctrl-y'<CR>

However, at least on Windows with gvim, non-printable key chords work, so (for example) Ctrl-Shift-Left is usable by Vim, and is not the same as Ctrl-Left. Does <C-S-x> work with Vim on some systems? The idea of picking another key for increment is good, how about <A-x> (Alt-x)? JohnBeckett 06:56, August 12, 2011 (UTC)

Pages on Vim Tips Wiki

Add a Page
1,601pages on
this wiki

Latest Photos

Add a Photo
66photos on this wiki
See more >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki