Since | is used to separate commands, I thought I could just do this:

:function! SomeFunc() | return 0 | endfunction

It works fine when I type it on separate lines (entering the first line causes it to prompt for the remaining lines):

:function! SomeFunc()
  return 0

I now see this caveat at :help :bar:

These commands see the '|' as their argument, and can therefore not be followed by another Vim command:


Is there any way around that?

I see where it says...

You can also use to separate commands in the same way as with '|'. To insert a use CTRL-V CTRL-J. "^@" will be shown.

But this doesn't work either:

:function! SomeFunc() <NL> return 0 <NL> endfunction

It gives this error:

E488: Trailing characters

This works if I manually type in the CTRL-V CTRL-J sequence:

:function! SomeFunc() ^@ return 0 ^@ endfunction

But that still isn't a acceptable solution because I want to be able to simply copy and paste the function! command and press Enter...

  • I guess I can just copy and paste the multi-line version of the function definition... But I'm still curious if there is a way to define it with one copy-and-pastable line...
    – Tyler Rick
    Dec 1 '14 at 23:04
  • 2
    Your ultimate goal is not clear to me but you can yank the function and "source" it with :@".
    – romainl
    Dec 1 '14 at 23:43
  • The original problem that prompted this Q was that I found a function in a plugin that I wanted to use in my own .vim scripts/config, but it was only callable as <SNR>104_mixedcase(). I wrote my own wrapper function to try to "export" it and make it easier to use from anywhere. But it didn't work to define my new wrapper function in a script file, apparently because the <SNR> is then relative to that (wrong) file. It did seem to work, however, if I simply pasted the function in the vim command line. So I thought it would be nice if I could paste that as a single line/command...
    – Tyler Rick
    Dec 2 '14 at 23:59
  • Great idea, thanks! Didn't know about :@ for sourcing the contents of registers. That (vim registers) would work well — better than copying and pasting all the way out to the system clipboard and then back into vim.
    – Tyler Rick
    Dec 3 '14 at 0:00
  • <SNR>104_ means "script number 104". You can locate the script with :scriptnames and copy the function to your vimrc.
    – romainl
    Dec 3 '14 at 6:06

One option would be to use exe:

exe ":function! SomeFunc() \n return 0 \n endfunction"

The \n characters are interpreted as newlines by the double-quoted strings. This does mean you should be careful to escape any special sequences.

That said,

I want to be able to simply copy and paste the function! command and press Enter...

As romainl mentioned, your ultimate goal is not clear. If this is something you do often for some reason, maybe there's a better way to get what you want. It's a good idea to describe your problem in terms of why you need this functionality.

  • Good answer. That makes sense, and seems to work. I've added my motivation as a comment above, but it basically has turned into just a theoretical/learning question. I have learned several new tricks as a result, thanks!
    – Tyler Rick
    Dec 3 '14 at 0:01
  • 1
    I see, you found a useful function in a plugin, but it's script-local. Happens every once in a while. In a case like this, I'd copy the function to my local files and make it global (or autoloaded), so it's easily callable. It's copy-pasta, but if it's just a utility function, I'd say it's not a big deal. Anyway, glad to hear you've learned new tricks, that's the value in experiments like this :). Dec 3 '14 at 9:42

I was looking into this because I wanted to test out functions I copied online in a vim session before adding to my .vimrc. I tried source but that requires a file name.

One of the comments in the answer section says to use :@". That runs whatever is in the unnamed register.

If you want to run something you copied from online, you can do :@+. But it won't work if you don't have vim installed with system clipboard integration.

Another method that works without clipboard integration is :normal :<PASTE YOUR COMMAND HERE WITH YOUR SYSTEM HOTKEY>. That runs everything in normal mode as if you were typing it yourself, each newline would continue onto the next line fo the function.

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.