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:
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
You have two options:
set number for regular line numbers
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 ...
Yes, vim can do this! Do a global search for x, and replace it with \=printf("%d", line('.'). For example:
You can also change x to another search, if you need to.
This replaces every occurence of x (or whatever you search for) with the evaluation register \=. This register evaluates vimscript code, and ...
Assuming your lines span from 15 to 10 lines above the current one, you can achieve what you requested using relative line numbers:
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:
You can use the command:
to turn on line numbering. To turn it off again you can use:
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.
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 ...
For the record, and definitely not the shortest way (see @Undo's awesome solution),
but sequence of keystrokes will do it too:
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 (...
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.
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 ...
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.
You can use hard numbers in your range:
Or relative numbers:
Or manual marks:
Or automatic marks:
Or line shortcuts:
Or any combination of the tricks above:
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".
Depending on your usecase the following might be useful:
Create the entries all with the number "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
See :help v_g_CTRL-A
Update: What if ...
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)
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.
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 ...
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:
let &statusline .= '%<%f ' " ...
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[his] Make the current window part of the diff windows. This sets
the options like for "vimdiff".
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.
There is another way to search between particular lines.
This will search from line 200 to 299 for the pattern 'good'.
%>199l - l is for line, >199 denotes the lines greater than 199
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
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:
autocmd InsertEnter * highlight LineNr ctermfg=white guifg=white
autocmd InsertLeave * highlight LineNr ctermfg=130 guifg=...
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.
Why it didn't work
From :help 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