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?)

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

  • 1
    Is there a way to save this (without having to use let) ? – nobe4 Apr 12 '16 at 10:01
  • 2
    No. :let it is. But you can save it into a variable (:let foo = ...) or into a register (:let @a = ...). – romainl Apr 12 '16 at 10:03
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.