97

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 in my vimrc which will use relative numbers for all lines except the current line. set number " Show current line number set ...


56

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 of lines using 5j for example. What is cool is that you can combine them. I have the following in my .vimrc set number " Show current line ...


53

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)


26

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. Explanation: This replaces every occurence of x (or whatever you search for) with the evaluation register \=. This register evaluates vimscript code, and ...


24

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 command to your vimrc file. nu and nonu can be used as aliases for number and nonumber respectively.


23

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 asks you to confirm if that is what you really wanted. To avoid the confirmation step, you can type silent before your command, or just specify a forwards range: ...


18

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 The above works by piping the entire (empty) buffer to seq, which ignores its input and just outputs the numbers 1 to 100. Vim then replaces the entire buffer ...


17

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> -- insert the number 1, then get back out to command mode qa -- start recording a macro in register "a" Y -- copy the current line p -- paste the current line (...


16

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". That works both ways: d2k deletes upward. A long-winded ex command version of that would be :+2d.


13

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/command Or line shortcuts: :.,$command Or any combination of the tricks above: :?foo?,+46command :'a,$command ...


13

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 insert mode" to "turning line numbers on and off".


12

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-coded. If you really want this, you can modify it to: fmt = "%*ld "; and recompile. The result should be something like: Of course, this might make a good ...


12

You can toggle relative numbering on and off using: :set rnu " toggle relative numbering on :set rnu! " toggle relative numbering off


12

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: Then press g<c-a> (g+Ctrla): If you had replaced a single column, then you can use gv to quickly reselect the same area.


11

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 move down to the last "1". So now all but the first "1" is selected. Now hit gCtrl-a and you get - "1" - "2" - "3" - "4" See :help v_g_CTRL-A Update: What if ...


9

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 lines) exe "" execute the following command .w! save the current line and overwrite if already exists. (Remove ! if you don't want to auto-overwrite everything) "...


7

: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.


7

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, I use ctrl-L twice to toggle it.


7

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: :g/TODO/let @A = getpos('.')[1] . ' ' . substitute(getline('.'), '^\s*', '', '') . "\n"


7

Why won't you use Vim's builtin feature to toggle that? Just use: set relativenumber! - this will cycle between on and off.


6

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: The 'relativenumber' option changes the displayed number to be relative to the cursor. Together with 'number' there are these four combinations (cursor in ...


6

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 a new statusline on certain autocmd events and/or key mappings. First, let us define a statusline: set statusline= let &statusline .= '%<%f ' " ...


6

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 *:difft* *:diffthis* :difft[his] Make the current window part of the diff windows. This sets the options like for "vimdiff". You ...


5

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


5

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 'good'. %>199l - l is for line, >199 denotes the lines greater than 199


5

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 lines 2 till 100 :2EnterVG. Now press gCtrl+A. Read the help at :h v_g_CTRL-A


5

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. Note that it requires the +signs feature which you may or may not have.


5

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 augroup number autocmd! autocmd InsertEnter * highlight LineNr ctermfg=white guifg=white autocmd InsertLeave * highlight LineNr ctermfg=130 guifg=...


5

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 1 for the space separating the numbers from the text. That's one too many in this case, so set numberwidth=3 in your vimrc, and you should be good to go. ...


5

Why it didn't work From :help FileType, *FileType* FileType When the 'filetype' option has been set. The pattern is matched against the filetype. <afile> can be used for the name of the file where this option was set, and <amatch> for ...


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