120 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 ...
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 ...
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)
  • 2,511
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. ...
  • 17.1k
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 ...
  • 3,552
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 ...
  • 504
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 ...
  • 1,905
19 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
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> -- ...
  • 2,748
18 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". ...
  • 9,129
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: ...
  • 24.4k
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-...
15 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 ...
  • 8,937
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/...
  • 36.2k
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 ...
  • 17.1k
12 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, ...
  • 121
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 ...
  • 7,626
8 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: ...
  • 6,520
8 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 ...
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.
  • 36.2k
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: :...
  • 9,510
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.
  • 4,398
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 ...
  • 23.4k
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 ...
6 votes

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

It is not really possible to have both number and relativenumber displayed at the same time, besides what is mentioned at :h number_relativenumber (see different answer). However, you can misuse the ...
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 ...
  • 17.1k
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. ...
  • 36.2k
5 votes
Accepted

How do I setnumber and relativenumber for :Explore in .vimrc

You can set it in g:netrw_bufsettings. The default is "noma nomod nonu nobl nowrap ro nornu", so this should do: let g:netrw_bufsettings = "noma nomod nu nobl nowrap ro rnu"
  • 3,678
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>
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 '...
  • 3,182

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