Escaping Shell Command Arguments in Vim












0















I'm trying to fully understand the following command in Vim:



:exe "grep -R " . shellescape(expand("<cWORD>")) . " ."<cr>


I got the use of expand function (force the expansion of into the actual string
before it gets passed to shellescape) and shellescape command itself ( from Vim help page: Escape {string} for use as a shell command argument).



What I do not understand, from help itself either, is that use of dots, one before and one after shellescape command.



Again, both of the dots are preceeded and followed by an empty space.
And if I use :



:exe "grep -R "shellescape(expand("<cWORD>"))" ."<cr>


which is the same command without those dots, I (apparently) get the same result.



Can anybody give a detailed explanation?



Thank you










share|improve this question























  • . is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

    – Jim Stewart
    Nov 25 '18 at 16:43






  • 1





    expr6 . expr6 .. String concatenation

    – phd
    Nov 25 '18 at 18:17
















0















I'm trying to fully understand the following command in Vim:



:exe "grep -R " . shellescape(expand("<cWORD>")) . " ."<cr>


I got the use of expand function (force the expansion of into the actual string
before it gets passed to shellescape) and shellescape command itself ( from Vim help page: Escape {string} for use as a shell command argument).



What I do not understand, from help itself either, is that use of dots, one before and one after shellescape command.



Again, both of the dots are preceeded and followed by an empty space.
And if I use :



:exe "grep -R "shellescape(expand("<cWORD>"))" ."<cr>


which is the same command without those dots, I (apparently) get the same result.



Can anybody give a detailed explanation?



Thank you










share|improve this question























  • . is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

    – Jim Stewart
    Nov 25 '18 at 16:43






  • 1





    expr6 . expr6 .. String concatenation

    – phd
    Nov 25 '18 at 18:17














0












0








0








I'm trying to fully understand the following command in Vim:



:exe "grep -R " . shellescape(expand("<cWORD>")) . " ."<cr>


I got the use of expand function (force the expansion of into the actual string
before it gets passed to shellescape) and shellescape command itself ( from Vim help page: Escape {string} for use as a shell command argument).



What I do not understand, from help itself either, is that use of dots, one before and one after shellescape command.



Again, both of the dots are preceeded and followed by an empty space.
And if I use :



:exe "grep -R "shellescape(expand("<cWORD>"))" ."<cr>


which is the same command without those dots, I (apparently) get the same result.



Can anybody give a detailed explanation?



Thank you










share|improve this question














I'm trying to fully understand the following command in Vim:



:exe "grep -R " . shellescape(expand("<cWORD>")) . " ."<cr>


I got the use of expand function (force the expansion of into the actual string
before it gets passed to shellescape) and shellescape command itself ( from Vim help page: Escape {string} for use as a shell command argument).



What I do not understand, from help itself either, is that use of dots, one before and one after shellescape command.



Again, both of the dots are preceeded and followed by an empty space.
And if I use :



:exe "grep -R "shellescape(expand("<cWORD>"))" ."<cr>


which is the same command without those dots, I (apparently) get the same result.



Can anybody give a detailed explanation?



Thank you







vim






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 16:22









DanieleDaniele

318




318













  • . is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

    – Jim Stewart
    Nov 25 '18 at 16:43






  • 1





    expr6 . expr6 .. String concatenation

    – phd
    Nov 25 '18 at 18:17



















  • . is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

    – Jim Stewart
    Nov 25 '18 at 16:43






  • 1





    expr6 . expr6 .. String concatenation

    – phd
    Nov 25 '18 at 18:17

















. is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

– Jim Stewart
Nov 25 '18 at 16:43





. is the string concatenation operator in Vim. Most other languages use +. It looks like your example command is missing at least one " as well.

– Jim Stewart
Nov 25 '18 at 16:43




1




1





expr6 . expr6 .. String concatenation

– phd
Nov 25 '18 at 18:17





expr6 . expr6 .. String concatenation

– phd
Nov 25 '18 at 18:17












1 Answer
1






active

oldest

votes


















1














:help :execute already explains that.



As you can see from the :exe[cute] {expr1} .. syntax, it takes multiple arguments.




Multiple arguments are concatenated, with a space in
between. To avoid the extra space use the "."
operator to concatenate strings into one argument.




:help expr-. explains that the operator for String concatenation in Vimscript is . (not + like in many other languages; in Vimscript, this solely is for adding numbers or Lists). The empty space around it is optional, but often given for better readability.



In summary, if you need to concatenate Strings with spaces, you can either use . and include the space inside one of the Strings, or pass separate arguments to :execute and let it add the spaces implicitly, or mix both approaches within the same command (readability should be the first priority here).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f53469467%2fescaping-shell-command-arguments-in-vim%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    :help :execute already explains that.



    As you can see from the :exe[cute] {expr1} .. syntax, it takes multiple arguments.




    Multiple arguments are concatenated, with a space in
    between. To avoid the extra space use the "."
    operator to concatenate strings into one argument.




    :help expr-. explains that the operator for String concatenation in Vimscript is . (not + like in many other languages; in Vimscript, this solely is for adding numbers or Lists). The empty space around it is optional, but often given for better readability.



    In summary, if you need to concatenate Strings with spaces, you can either use . and include the space inside one of the Strings, or pass separate arguments to :execute and let it add the spaces implicitly, or mix both approaches within the same command (readability should be the first priority here).






    share|improve this answer




























      1














      :help :execute already explains that.



      As you can see from the :exe[cute] {expr1} .. syntax, it takes multiple arguments.




      Multiple arguments are concatenated, with a space in
      between. To avoid the extra space use the "."
      operator to concatenate strings into one argument.




      :help expr-. explains that the operator for String concatenation in Vimscript is . (not + like in many other languages; in Vimscript, this solely is for adding numbers or Lists). The empty space around it is optional, but often given for better readability.



      In summary, if you need to concatenate Strings with spaces, you can either use . and include the space inside one of the Strings, or pass separate arguments to :execute and let it add the spaces implicitly, or mix both approaches within the same command (readability should be the first priority here).






      share|improve this answer


























        1












        1








        1







        :help :execute already explains that.



        As you can see from the :exe[cute] {expr1} .. syntax, it takes multiple arguments.




        Multiple arguments are concatenated, with a space in
        between. To avoid the extra space use the "."
        operator to concatenate strings into one argument.




        :help expr-. explains that the operator for String concatenation in Vimscript is . (not + like in many other languages; in Vimscript, this solely is for adding numbers or Lists). The empty space around it is optional, but often given for better readability.



        In summary, if you need to concatenate Strings with spaces, you can either use . and include the space inside one of the Strings, or pass separate arguments to :execute and let it add the spaces implicitly, or mix both approaches within the same command (readability should be the first priority here).






        share|improve this answer













        :help :execute already explains that.



        As you can see from the :exe[cute] {expr1} .. syntax, it takes multiple arguments.




        Multiple arguments are concatenated, with a space in
        between. To avoid the extra space use the "."
        operator to concatenate strings into one argument.




        :help expr-. explains that the operator for String concatenation in Vimscript is . (not + like in many other languages; in Vimscript, this solely is for adding numbers or Lists). The empty space around it is optional, but often given for better readability.



        In summary, if you need to concatenate Strings with spaces, you can either use . and include the space inside one of the Strings, or pass separate arguments to :execute and let it add the spaces implicitly, or mix both approaches within the same command (readability should be the first priority here).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 12:37









        Ingo KarkatIngo Karkat

        133k14148199




        133k14148199
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53469467%2fescaping-shell-command-arguments-in-vim%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'