2

In insert-mode, I would like to extract the current line number I'm editing in a "variable"/memory/buffer, and to be able to paste it through a remapping.

I know that :echo line(".") gives me the line number. I have found how to insert the line number at the beginning of each line, using :s/^/\=line("."). But I would like to have a remapping like :

:inoremap \l <Esc>:echo line(".") > buffer<CR> p

that would extract the line number and paste it at my current cursor position.

(Sources of previous searches : How to add permanent line numbers to a file?)

Feffe
  • 1,761
  • 2
  • 14
  • 21

2 Answers2

4

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(".")<CR>"lp
nobe4
  • 16,033
  • 4
  • 48
  • 81
4

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

romainl
  • 40,486
  • 5
  • 85
  • 117