110 votes
Accepted

How can I show relative line numbers?

I just replied to a similar question here: How can I add line numbers to Vim? Beginning with version 7.3, you can use the following: set relativenumber I actually use both relativenumber and number ...
user avatar
58 votes
Accepted

How can I add line numbers to Vim?

You have two options: set number for regular line numbers And also set relativenumber which will show relative line numbers. i.e. current line is always 0. This is useful for moving up/down N number ...
user avatar
56 votes
Accepted

How can I generate a list of sequential numbers, one per line?

Use :put and range(): :put =range(1,100) To avoid the blank line at the top (kudos to romainl), use :0put: :0put =range(1,100)
user avatar
  • 2,423
37 votes
Accepted

Replace a pattern with current line number

Yes, vim can do this! Do a global search for x, and replace it with \=printf("%d", line('.'). For example: :%s/x/\=printf("%d", line('.')) You can also change x to another search, if you need to. ...
user avatar
  • 16.5k
26 votes
Accepted

How do I use relative line numbers in command line mode?

Assuming your lines span from 15 to 10 lines above the current one, you can achieve what you requested using relative line numbers: :-10,-15m. Unfortunately when specifying a backwards range, Vim ...
user avatar
  • 3,372
24 votes

How can I add line numbers to Vim?

You can use the command: :set number to turn on line numbering. To turn it off again you can use: :set nonumber If you want vim to always default to showing line numbers you can add the ...
user avatar
  • 494
20 votes

How can I generate a list of sequential numbers, one per line?

In addition to Undo's pure-vim :put =range(1,100) (which actually leaves you with a blank line up top), you can, depending on your OS, use one of its commands. E.g., on a Unix/Linux box: %!seq 1 100 ...
user avatar
  • 1,815
19 votes

How can I generate a list of sequential numbers, one per line?

For the record, and definitely not the shortest way (see @Undo's awesome solution), but sequence of keystrokes will do it too: i1EscqaYpCtrl+aq98@a Let me break that down for you: i1<Esc> -- ...
user avatar
  • 2,618
17 votes
Accepted

Can relative number be changed so that current line is 1?

Don't do that. A numbering system that goes from -1 to +1 without a zero in between is just asking for trouble. As an alternative habit, I suggest d2j — "delete this line and the next two lines". ...
user avatar
  • 8,739
17 votes

How can I show relative line numbers?

You can toggle relative numbering on and off using: :set rnu " toggle relative numbering on :set rnu! " toggle relative numbering off
user avatar
17 votes

Replace a pattern with current line number

Since you mentioned vertical select and replace, you can do that too. Use CtrlV to do select the lock of text you want to replace, then c and type 0, to replace that with 0s. Then, select those 0s: ...
user avatar
  • 23.7k
15 votes
Accepted

Current line number aligned to the left with number and relativenumber

From screen.c, win_line() function (around line 3707): /* 'number' + 'relativenumber' */ num = lnum; fmt = "%-*ld "; The %- makes it left-aligned (See sprintf(3)). As you can see, this value is hard-...
user avatar
14 votes
Accepted

Visual Block Mode edit with sequential number

Depending on your usecase the following might be useful: Create the entries all with the number "1": - "1" - "1" - "1" - "1" Then go to the second "1" and press V to start line-wise visual. Then ...
user avatar
  • 8,510
13 votes

How do I use relative line numbers in command line mode?

You can use hard numbers in your range: :200,300command Or relative numbers: :-27,+46command Or manual marks: :'a,'bcommand Or automatic marks: :'[,'>command Or searches: :?foo?,/bar/...
user avatar
  • 32.5k
13 votes
Accepted

Set line numbers only while in normal mode

You can do this with autocommands. au InsertEnter * set nonumber au InsertLeave * set number Not much explanation is needed. This does exactly what you asked for. It ties "entering and exiting ...
user avatar
  • 16.5k
11 votes
Accepted

How to write each line into separate file?

Try a global command: :g/^/exe ".w! line".line('.').".txt" :g/^/ Do a command for every line (you can adjust this regular expression if you only want to save certain lines, i.e. . for non-empty ...
user avatar
  • 7,406
10 votes

How can I show relative line numbers?

If you want to use a hotkey for toggling relative line number, here is a snippet in my vimrc: " Toggle relative line number nmap <C-L><C-L> :set invrelativenumber<CR> In this case, ...
user avatar
  • 101
7 votes

How can I generate a list of sequential numbers, one per line?

Here is a different approach, that needs a newer Vim (something like 7.4.800) This assumes an empty buffer and '1' in register a. First enter 100 1 into your buffer, "a100P. Then visually select ...
user avatar
7 votes

How can I search for a string between certain line numbers?

:100,235g/foo/#<CR> then press :147<CR> to jump to the desired line. You can also use :ilist: :100,235il foo<CR> See :help range, :help :global and :help :ilist.
user avatar
  • 32.5k
7 votes
Accepted

How to get and use the number of each matched line in a global command

One way to do it: clear the register: :let @a='' append search results in it: :g/TODO/let @A = getpos('.')[1] . ' ' . getline('.') . "\n" Re: BONUS remove indentation in the global command: :...
user avatar
  • 9,250
7 votes

How can I set number and relativenumber at the same time?

Showing all the line numbers and relative numbers at the same time isn't possible. But you can get the current line number and the relative number for all the others. From :help number_relativenumber: ...
user avatar
  • 6,350
7 votes

Why this function to change relative number in lines is not working?

Why won't you use Vim's builtin feature to toggle that? Just use: set relativenumber! - this will cycle between on and off.
user avatar
  • 4,188
7 votes
Accepted

Vim never shows line number for Python files even though I have `.vimrc`

Why it didn't work From :help FileType, *FileType* FileType When the 'filetype' option has been set. The pattern is matched against the ...
user avatar
  • 20.5k
6 votes
Accepted

Different gutter colours for each code function

Here's a different solution which may be of help: put the current function name in the statusline: Vim doesn't really have a concept of a "dynamic" statusline; but we can work around that by setting ...
user avatar
6 votes
Accepted

Weird vertical bar to left of line numbers

I have a couple guesses. My first guess is that this is because you have "diff" on, most likely because you had done "diffthis" at some point, or started vim in diff mode. From :help diffthis ...
user avatar
  • 16.5k
5 votes
Accepted

Relative number on old vim versions

Relative numbers were officially added to Vim with the 7.3 release. If you can't update Vim or can't compile your own you will need to install a plugin like DrChip's RltvNmbr. Maybe there are others. ...
user avatar
  • 32.5k
5 votes

How can I show relative line numbers?

function! NumberToggle() if(&relativenumber == 1) set norelativenumber else set relativenumber endif endfunc nnoremap <leader>nt :call NumberToggle()<cr>
user avatar
5 votes

How can I search for a string between certain line numbers?

There is another way to search between particular lines. /%>{linenumber}l\%<{linenumber}{pattern} Example: /%>199l\%<300lgood This will search from line 200 to 299 for the pattern '...
user avatar
  • 3,012
5 votes

Set line numbers only while in normal mode

Here's an alternative solution which will only hide the numbers, but won't actually change the margin on the left so you text doesn't jump around so much when entering/leaving insert mode: set number ...
user avatar
5 votes
Accepted

Get rid of extra gutter spacing

Vim automatically sets the gutter spacing, but it has some constraints. Notably, numberwidth determines the minimum number of columns reserved for the gutter. The default is 4, 3 for the numbers and ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible