Skip to main content
40 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. ...
DJMcMayhem's user avatar
  • 17.7k
18 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: ...
muru's user avatar
  • 25k
16 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 ...
Ralf's user avatar
  • 9,287
14 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, ...
Sean Lee's user avatar
  • 141
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 ...
DJMcMayhem's user avatar
  • 17.7k
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 ...
Tumbler41's user avatar
  • 7,756
8 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 ...
D. Ben Knoble's user avatar
  • 26.6k
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.
grodzik's user avatar
  • 4,658
7 votes
Accepted

Nvim can not detect focus change inside tmux sessions

I opened an issue on Neovim repo and get the right answer. We need to turn on the focus-events for tmux. Edit the tmux config file ~/.tmux.conf and add the following setting: set -g focus-events on ...
jdhao's user avatar
  • 1,143
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 ...
DJMcMayhem's user avatar
  • 17.7k
6 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 ...
Eggrenade's user avatar
  • 101
6 votes
Accepted

What are the benefits and use cases of relativenumber?

TL;DR relative numbers are just another tool in the toolbox they can be effective in terms of locality when a text-object doesn't cut it (and for the lazy-minded among us) they pair nicely with ...
D. Ben Knoble's user avatar
  • 26.6k
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 ...
Martin Tournoij's 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 '...
SibiCoder's user avatar
  • 3,432
5 votes
Accepted

How to toggle relative number for all opened windows?

Yes, using :h :windo: nnoremap <C-a> :windo set relativenumber!<CR>
Jake Grossman's user avatar
5 votes
Accepted

Why are relative line numbers useful in Vim?

One reason for relative line number is that some Vim command use relative line number or relative range. A trivial example is 4j that moves 4 line down relative to the current line number. But there ...
Vivian De Smedt's user avatar
4 votes
Accepted

Extract line number as a variable to re-insert it

This is a perfect use case for the expression register: inoremap <key> <C-r>=line(".")<CR> or: inoremap <expr> <key> line(".") See :help \=, :help i_ctrl-r, and :help ...
romainl's user avatar
  • 41.2k
4 votes

Extract line number as a variable to re-insert it

You can save it in a register with let : inoremap \l <Esc>:let @l = line(".")<CR> If you want to immediately paste it, you can do so with: inoremap \p <Esc>:let @l = line(".")<...
nobe4's user avatar
  • 16.1k
4 votes

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

I know this question is really old, but there's another way you can do it also. Try this: 99o<esc>:%s/^/\=line('.')<cr> 99o<esc> will just open up 100 blank lines. Then, we run a ...
DJMcMayhem's user avatar
  • 17.7k
4 votes

Relative Numbers sidepanel with wrapped lines

You state that your current workflow is "inaccurate" and "less usable". You can solve the former (but arguably not the latter) by replacing your current j mapping with: nnoremap <expr> j v:...
Rich's user avatar
  • 32.2k
4 votes
Accepted

How to display line numbers in netrw v125 (vim7.0)?

Is there any reason that :set number doesn't work? Edit: I see what you mean @user3813057. Netrw has a lot of control over its buffer, so we have to be a bit tricky when doing things that it doesn't ...
Tumbler41's user avatar
  • 7,756
4 votes
Accepted

Vim ignore terminal color schemes when setting colors

By using color names like cyan or black or numbers below 16 you are actively telling Vim to use your terminal color scheme. If you don't want Vim to use your terminal emulator color scheme you will ...
romainl's user avatar
  • 41.2k
4 votes
Accepted

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

The intention of the function as described in the blog post is not to toggle relativenumber but to switch between the number and relativenumber settings. In Vim 7.3, the number and relativenumber ...
Rich's user avatar
  • 32.2k
4 votes
Accepted

Send file and line number to tmux

Here's an example of inserting line number in a shell command: :exe "!echo " . line(".") So your mapping would be something like this: nnoremap <silent> <leader>ef :exe "!tmux send -t 0....
B Layer's user avatar
  • 20k
4 votes
Accepted

Line number order is broken

In SpaceVim, we use g:spacevim_relativenumber to config relativenumber. if by default it is 1, if you want to disable this feature. just add let g:spacevim_relativenumber = 0 to your custom config. ...
Eric Wong's user avatar
  • 530
4 votes
Accepted

How could I turn off the terminal line number while keep the editor line number in vimrc

Thanks the helpful comment provided by Christian Brabandt. The answer is to add a line in the .vimrc: autocmd TerminalOpen * set nonu
coin cheung's user avatar
4 votes
Accepted

How to copy lines with line numbers?

You have several alternatives. Use the TOhtml command to let Vim generate an HTML file, which includes the line numbers, and then copy and paste from that file. Replace each line by its line number ...
4 votes
Accepted

How to open multiple files with cursor for all files being at the last line?

You can use vim *.log +'argdo norm! G' See :h [+cmd] :h :argdo :h :norm The + [cmd] allows you to run an ex command when you start vim and the command argdo norm! G will run norm! G on all the ...
statox's user avatar
  • 50.1k
4 votes
Accepted

line numbers in vim -H reversed

As it turns out, this was an obscure bug, that was caused by patch 8.1.1073. I have just submitted a patch to fix this issue and it has just been included as of Patch v8.2.3012
Christian Brabandt's user avatar
4 votes

How supply range to normal mode key-binding?

Assuming the cursor is on line #80 and <leader> defaults to backslash, type directly 2\t. The point is that typing N: in Normal mode, where N is arbitrary number, is converted by Vim to :.,.+N-1....
Matt's user avatar
  • 20.9k

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