1

I am using latest available version of NeoVim v0.4.3 and latest Arch build with kernel 5.6.4-arch1-1.

To paste onto the vim command line I am using

: ctrl+r "

to paste the " register

Is there an easiear way to go about this, without having to press the :, then press <c-r>, then press the associated register key?

Perhaps some ideas exist for mapping or a function in .vimrc.

  • 2
    You mean from unnamed register? <Ctrl-r>" should be enough. – Ralf May 14 at 12:38
  • I've clarified the question, and removed the redundant ctrl+o. No idea where that ctrl+o came from. – Kes May 14 at 14:57
  • 1
    So you are looking for this mapping nnoremap <f1> :<C-r>" (where you can replace <f1> by any key you prefer to use)? The ctrl+o probably came from :h c_CTRL-R_CTRL-O EDIT Or you can use Fill's solution :) – statox May 14 at 15:10
1

I like to use the :@ command to execute a register containing Vimscript.

It's quite convenient to test a snippet that will go into vimrc or into a *.vim script. It has one main limitation, which is that it doesn't support line continuations with backslash, so beware of that. You can't use it to define s: functions either.

You still end up typing four characters (:@" and the "Return" key), so I often add a mapping to run the current line or run the visual block:

xnoremap <buffer> <F5> y:@"<CR>
nnoremap <buffer> <F5> yy:@"<CR>

Note I made these <buffer> mappings, they make sense in Vimscript files, so you might want to add those to a ~/.vim/ftplugin/vim.vim file.

Also note that the normal-mode mapping will work with a count, so 12<F5> will run the commands in the 12 lines starting with the current one as Ex commands.

(It also overwrites the default register. You can probably rework them to take an optional register if you like, or save/restore registers if you want it not to leave behind any trace.)

| improve this answer | |
  • 1
    "It's quite convenient to test a snippet that will go into vimrc or into a *.vim script. It has one main limitation, which is that it doesn't support line continuations with backslash, so beware of that. You can't use it to define s: functions either." Thanks - i've got that, I think. From reading I think s: makes a function local, tho I've yet to understand what that means. Thanks for the heads up on continuation backslashes too. – Kes May 17 at 11:12
  • 1
    About s:, see :help local-function and :help <SID> to understand how that works. When you're sourcing them with @", you don't have a script file associated with them, so you can only define global functions (whose names must start with an uppercase.) – filbranden May 17 at 12:08
  • 1
    @Kes Hmmm note that @" is different from :@", the former executes the register as a normal mode command, while the latter executes it as an Ex command... So not sure what is going on there... – filbranden May 17 at 12:11
  • 1
    Thank you. No idea what I was doing there. I put the : back in front of the @ and it does work. – Kes May 17 at 12:14
  • 1
    I noticed that if I press F5 in insert mode that vim crashes. So I added inoremap <buffer> <F5> <esc>y$:@"<cr>. Is there a way to handle crashing like so that it springs an error rather than hangs? – Kes May 18 at 8:43

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.