How do I define a vim function on a single line?












2














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
endfunction


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:





:function




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










share|improve this question






















  • 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
















2














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
endfunction


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:





:function




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










share|improve this question






















  • 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














2












2








2


1





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
endfunction


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:





:function




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










share|improve this question













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
endfunction


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:





:function




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







vim






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 1 '14 at 23:01









Tyler Rick

20525




20525












  • 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


















  • 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
















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




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




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




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




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




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




<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










2 Answers
2






active

oldest

votes


















5














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.






share|improve this answer





















  • 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 :).
    – Andrew Radev
    Dec 3 '14 at 9:42



















1














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.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f847340%2fhow-do-i-define-a-vim-function-on-a-single-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    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.






    share|improve this answer





















    • 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 :).
      – Andrew Radev
      Dec 3 '14 at 9:42
















    5














    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.






    share|improve this answer





















    • 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 :).
      – Andrew Radev
      Dec 3 '14 at 9:42














    5












    5








    5






    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 2 '14 at 10:31









    Andrew Radev

    30114




    30114












    • 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 :).
      – Andrew Radev
      Dec 3 '14 at 9:42


















    • 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 :).
      – Andrew Radev
      Dec 3 '14 at 9:42
















    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






    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




    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 :).
    – Andrew Radev
    Dec 3 '14 at 9:42




    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 :).
    – Andrew Radev
    Dec 3 '14 at 9:42













    1














    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.






    share|improve this answer


























      1














      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.






      share|improve this answer
























        1












        1








        1






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 at 0:06









        dosentmatter

        1334




        1334






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f847340%2fhow-do-i-define-a-vim-function-on-a-single-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”