Insert external file and modify each line from scriptAbsolute script-file line-numbers in Vim-function errorsRead file from vimscript variableCurrent VimL script path from inside a functionEnter insert to insert mode from functionHow do I script Vim to download a thesaurus file and save it?Help to implement script that toggle single and double quotesHow to fake a script file name inside a monolithic vimrc?What is a working setup to use Vim plugins UndoTree and Goyo together?Trim/clear new line character from batch script at Windows7 with gvimThe difference between a plugin script and a sourced script

When is the phrase "j'ai bon" used?

Difference between sizeof(struct name_of_struct) vs sizeof(name_of_struct)?

How many times to repeat an event with known probability before it has occurred a number of times

What is the difference between state-based effects and effects on the stack?

Converting 3x7 to a 1x7. Is it possible with only existing parts?

Is it possible to install Firefox on Ubuntu with no desktop enviroment?

What does the output current rating from an H-Bridge's datasheet really mean?

Why does MAGMA claim that the automorphism group of an elliptic curve is order 24 when it is order 12?

Are there any rules for identifying what spell an opponent is casting?

Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?

How to avoid offending original culture when making conculture inspired from original

Using roof rails to set up hammock

How to remove multiple elements from Set/Map AND knowing which ones were removed?

Creating polygon with exact measurements in QGIS 3

Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?

How to search for Android apps without ads?

Leveling up and Getting Items!

For Saintsbury, which English novelists constituted the "great quartet of the mid-eighteenth century"?

How did the European Union reach the figure of 3% as a maximum allowed deficit?

Print the phrase "And she said, 'But that's his.'" using only the alphabet

How can the US president give an order to a civilian?

newcommand with parameter blank or zero

Why can't we feel the Earth's revolution?

Struggling to present results from long papers in short time slots



Insert external file and modify each line from script


Absolute script-file line-numbers in Vim-function errorsRead file from vimscript variableCurrent VimL script path from inside a functionEnter insert to insert mode from functionHow do I script Vim to download a thesaurus file and save it?Help to implement script that toggle single and double quotesHow to fake a script file name inside a monolithic vimrc?What is a working setup to use Vim plugins UndoTree and Goyo together?Trim/clear new line character from batch script at Windows7 with gvimThe difference between a plugin script and a sourced script






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:



 fun InsertLicense()
if filereadable('LICENSE')
let license = 'LICENSE'
elseif filereadable('LICENSE.txt')
let license = 'LICENSE.txt'
else
let license = ''
endif
if line('$') < 3 && len(license) > 0
echo 'inserting license from ' . license
call append(0, readfile(license))
endif
endfun


It's working fine except one problem: LICENSE file is a plaintext file, e.g.:



Copyright 2019 OrgName

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated
...


but I need to wrap it as Java multiline comment:



/*
* Copyright 2019 OrgName
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
...
*/


Is it possible to insert * before each line of license file using vim scripting language?










share|improve this question







New contributor



g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    3















    I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:



     fun InsertLicense()
    if filereadable('LICENSE')
    let license = 'LICENSE'
    elseif filereadable('LICENSE.txt')
    let license = 'LICENSE.txt'
    else
    let license = ''
    endif
    if line('$') < 3 && len(license) > 0
    echo 'inserting license from ' . license
    call append(0, readfile(license))
    endif
    endfun


    It's working fine except one problem: LICENSE file is a plaintext file, e.g.:



    Copyright 2019 OrgName

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated
    ...


    but I need to wrap it as Java multiline comment:



    /*
    * Copyright 2019 OrgName
    *
    * Permission is hereby granted, free of charge, to any person
    * obtaining a copy of this software and associated
    ...
    */


    Is it possible to insert * before each line of license file using vim scripting language?










    share|improve this question







    New contributor



    g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      3












      3








      3








      I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:



       fun InsertLicense()
      if filereadable('LICENSE')
      let license = 'LICENSE'
      elseif filereadable('LICENSE.txt')
      let license = 'LICENSE.txt'
      else
      let license = ''
      endif
      if line('$') < 3 && len(license) > 0
      echo 'inserting license from ' . license
      call append(0, readfile(license))
      endif
      endfun


      It's working fine except one problem: LICENSE file is a plaintext file, e.g.:



      Copyright 2019 OrgName

      Permission is hereby granted, free of charge, to any person
      obtaining a copy of this software and associated
      ...


      but I need to wrap it as Java multiline comment:



      /*
      * Copyright 2019 OrgName
      *
      * Permission is hereby granted, free of charge, to any person
      * obtaining a copy of this software and associated
      ...
      */


      Is it possible to insert * before each line of license file using vim scripting language?










      share|improve this question







      New contributor



      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm trying to make a function to insert license header from external file on first line in the buffer. The script is:



       fun InsertLicense()
      if filereadable('LICENSE')
      let license = 'LICENSE'
      elseif filereadable('LICENSE.txt')
      let license = 'LICENSE.txt'
      else
      let license = ''
      endif
      if line('$') < 3 && len(license) > 0
      echo 'inserting license from ' . license
      call append(0, readfile(license))
      endif
      endfun


      It's working fine except one problem: LICENSE file is a plaintext file, e.g.:



      Copyright 2019 OrgName

      Permission is hereby granted, free of charge, to any person
      obtaining a copy of this software and associated
      ...


      but I need to wrap it as Java multiline comment:



      /*
      * Copyright 2019 OrgName
      *
      * Permission is hereby granted, free of charge, to any person
      * obtaining a copy of this software and associated
      ...
      */


      Is it possible to insert * before each line of license file using vim scripting language?







      vimscript






      share|improve this question







      New contributor



      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question







      New contributor



      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question






      New contributor



      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked Jun 7 at 12:06









      g4s8g4s8

      1183




      1183




      New contributor



      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      g4s8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          4














          readfile() returns a list, each line being in a separate list item.



          You can therefore make use of map() to change the list inplace:



          let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')


          This makes use of lambda expression and wraps a the comment characters /* and */ around it.



          Alternatively, you can do this:



           :let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']


          which adds only /* at the start of the file and */ at the end of the file and for each line simply prepends the *.






          share|improve this answer


















          • 1





            Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

            – statox
            Jun 7 at 12:21






          • 1





            Upps, Sorry for that ;)

            – Christian Brabandt
            Jun 7 at 12:22


















          2














          Christian’s answer is great—for java files. But it requires some care for different filetypes.



          One alternative would be to lean on the machinery of tpope’s commentary and do something like



          " Go comment a paragraph
          normal gcap


          After you insert the license text.



          Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.






          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "599"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            );



            );






            g4s8 is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f20244%2finsert-external-file-and-modify-each-line-from-script%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









            4














            readfile() returns a list, each line being in a separate list item.



            You can therefore make use of map() to change the list inplace:



            let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')


            This makes use of lambda expression and wraps a the comment characters /* and */ around it.



            Alternatively, you can do this:



             :let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']


            which adds only /* at the start of the file and */ at the end of the file and for each line simply prepends the *.






            share|improve this answer


















            • 1





              Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

              – statox
              Jun 7 at 12:21






            • 1





              Upps, Sorry for that ;)

              – Christian Brabandt
              Jun 7 at 12:22















            4














            readfile() returns a list, each line being in a separate list item.



            You can therefore make use of map() to change the list inplace:



            let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')


            This makes use of lambda expression and wraps a the comment characters /* and */ around it.



            Alternatively, you can do this:



             :let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']


            which adds only /* at the start of the file and */ at the end of the file and for each line simply prepends the *.






            share|improve this answer


















            • 1





              Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

              – statox
              Jun 7 at 12:21






            • 1





              Upps, Sorry for that ;)

              – Christian Brabandt
              Jun 7 at 12:22













            4












            4








            4







            readfile() returns a list, each line being in a separate list item.



            You can therefore make use of map() to change the list inplace:



            let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')


            This makes use of lambda expression and wraps a the comment characters /* and */ around it.



            Alternatively, you can do this:



             :let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']


            which adds only /* at the start of the file and */ at the end of the file and for each line simply prepends the *.






            share|improve this answer













            readfile() returns a list, each line being in a separate list item.



            You can therefore make use of map() to change the list inplace:



            let a=map(readfile('LICENSE'), i,v -> '/* ' . v. ' */')


            This makes use of lambda expression and wraps a the comment characters /* and */ around it.



            Alternatively, you can do this:



             :let a=['/* '] + map(readfile('LICENSE'), i,v -> '* ' . v) + ['*/']


            which adds only /* at the start of the file and */ at the end of the file and for each line simply prepends the *.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 7 at 12:15









            Christian BrabandtChristian Brabandt

            16.5k2848




            16.5k2848







            • 1





              Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

              – statox
              Jun 7 at 12:21






            • 1





              Upps, Sorry for that ;)

              – Christian Brabandt
              Jun 7 at 12:22












            • 1





              Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

              – statox
              Jun 7 at 12:21






            • 1





              Upps, Sorry for that ;)

              – Christian Brabandt
              Jun 7 at 12:22







            1




            1





            Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

            – statox
            Jun 7 at 12:21





            Damn, it took me 4 more minutes to come up with a much less elegant solution! :)

            – statox
            Jun 7 at 12:21




            1




            1





            Upps, Sorry for that ;)

            – Christian Brabandt
            Jun 7 at 12:22





            Upps, Sorry for that ;)

            – Christian Brabandt
            Jun 7 at 12:22













            2














            Christian’s answer is great—for java files. But it requires some care for different filetypes.



            One alternative would be to lean on the machinery of tpope’s commentary and do something like



            " Go comment a paragraph
            normal gcap


            After you insert the license text.



            Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.






            share|improve this answer





























              2














              Christian’s answer is great—for java files. But it requires some care for different filetypes.



              One alternative would be to lean on the machinery of tpope’s commentary and do something like



              " Go comment a paragraph
              normal gcap


              After you insert the license text.



              Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.






              share|improve this answer



























                2












                2








                2







                Christian’s answer is great—for java files. But it requires some care for different filetypes.



                One alternative would be to lean on the machinery of tpope’s commentary and do something like



                " Go comment a paragraph
                normal gcap


                After you insert the license text.



                Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.






                share|improve this answer















                Christian’s answer is great—for java files. But it requires some care for different filetypes.



                One alternative would be to lean on the machinery of tpope’s commentary and do something like



                " Go comment a paragraph
                normal gcap


                After you insert the license text.



                Note that commentary comments each line individually, which can look less pretty for large chunks of text, but makes uncommenting lines (of code) easier in terms of the plugin code. You may or may not be able to live with this.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 7 at 16:43

























                answered Jun 7 at 12:46









                D. Ben KnobleD. Ben Knoble

                3,2331521




                3,2331521




















                    g4s8 is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    g4s8 is a new contributor. Be nice, and check out our Code of Conduct.












                    g4s8 is a new contributor. Be nice, and check out our Code of Conduct.











                    g4s8 is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Vi and Vim Stack Exchange!


                    • 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%2fvi.stackexchange.com%2fquestions%2f20244%2finsert-external-file-and-modify-each-line-from-script%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

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form