How to auto insert a template code into a new file of particular extension, that is created in a particular...












0















I have a project directory at ~/project. Under this project directory there are many sub-directories too. What i tried to do is, whenever i create a c++ file (which implies files with extensions .cc,.cpp,.h etc) in a particular directory, auto insert certain code templates into that file.



The template is of the given form :



/* 
* Author : Name
* Date : Sat Jan 19 12:42:56 IST 2019 (:r!date)
*/


Usually the date would be the date of creation of that file, which can be inserted using :r!date.



The idea i have come up with so far is creating a template.vim file, with the following contents :



call setline(1, '/*')
call setline(2, 'Author : ')
" the line below is a blunder. but i hope you get the gist of what im trying.
call setline(3, 'Date : '+ execute "normal! :r!date")
call setline(4, '*/')


then till sourcing the template file on creating a new c++ file, like so:



autocmd BufNewFile *.cc,*.cpp,*.h source ~/.vim/ftplugin/template.vim  


How can i efficiently add conditions for checking creation of a file in my ~/project directory or any of its sub-directories, with the extension of a c++ file and insert the above template with the specific date and format? Also how to insert this only on creation of a new file rather than an existing file?










share|improve this question



























    0















    I have a project directory at ~/project. Under this project directory there are many sub-directories too. What i tried to do is, whenever i create a c++ file (which implies files with extensions .cc,.cpp,.h etc) in a particular directory, auto insert certain code templates into that file.



    The template is of the given form :



    /* 
    * Author : Name
    * Date : Sat Jan 19 12:42:56 IST 2019 (:r!date)
    */


    Usually the date would be the date of creation of that file, which can be inserted using :r!date.



    The idea i have come up with so far is creating a template.vim file, with the following contents :



    call setline(1, '/*')
    call setline(2, 'Author : ')
    " the line below is a blunder. but i hope you get the gist of what im trying.
    call setline(3, 'Date : '+ execute "normal! :r!date")
    call setline(4, '*/')


    then till sourcing the template file on creating a new c++ file, like so:



    autocmd BufNewFile *.cc,*.cpp,*.h source ~/.vim/ftplugin/template.vim  


    How can i efficiently add conditions for checking creation of a file in my ~/project directory or any of its sub-directories, with the extension of a c++ file and insert the above template with the specific date and format? Also how to insert this only on creation of a new file rather than an existing file?










    share|improve this question

























      0












      0








      0








      I have a project directory at ~/project. Under this project directory there are many sub-directories too. What i tried to do is, whenever i create a c++ file (which implies files with extensions .cc,.cpp,.h etc) in a particular directory, auto insert certain code templates into that file.



      The template is of the given form :



      /* 
      * Author : Name
      * Date : Sat Jan 19 12:42:56 IST 2019 (:r!date)
      */


      Usually the date would be the date of creation of that file, which can be inserted using :r!date.



      The idea i have come up with so far is creating a template.vim file, with the following contents :



      call setline(1, '/*')
      call setline(2, 'Author : ')
      " the line below is a blunder. but i hope you get the gist of what im trying.
      call setline(3, 'Date : '+ execute "normal! :r!date")
      call setline(4, '*/')


      then till sourcing the template file on creating a new c++ file, like so:



      autocmd BufNewFile *.cc,*.cpp,*.h source ~/.vim/ftplugin/template.vim  


      How can i efficiently add conditions for checking creation of a file in my ~/project directory or any of its sub-directories, with the extension of a c++ file and insert the above template with the specific date and format? Also how to insert this only on creation of a new file rather than an existing file?










      share|improve this question














      I have a project directory at ~/project. Under this project directory there are many sub-directories too. What i tried to do is, whenever i create a c++ file (which implies files with extensions .cc,.cpp,.h etc) in a particular directory, auto insert certain code templates into that file.



      The template is of the given form :



      /* 
      * Author : Name
      * Date : Sat Jan 19 12:42:56 IST 2019 (:r!date)
      */


      Usually the date would be the date of creation of that file, which can be inserted using :r!date.



      The idea i have come up with so far is creating a template.vim file, with the following contents :



      call setline(1, '/*')
      call setline(2, 'Author : ')
      " the line below is a blunder. but i hope you get the gist of what im trying.
      call setline(3, 'Date : '+ execute "normal! :r!date")
      call setline(4, '*/')


      then till sourcing the template file on creating a new c++ file, like so:



      autocmd BufNewFile *.cc,*.cpp,*.h source ~/.vim/ftplugin/template.vim  


      How can i efficiently add conditions for checking creation of a file in my ~/project directory or any of its sub-directories, with the extension of a c++ file and insert the above template with the specific date and format? Also how to insert this only on creation of a new file rather than an existing file?







      vim neovim






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 14:23









      YedhinYedhin

      58819




      58819
























          1 Answer
          1






          active

          oldest

          votes


















          1














          First of all: There are template plugins, you should search the net for "vim template plugin". Perhaps you find something useful.



          If you want to do it yourself:



          Create your template file like this (I assume ~/tmpl/tmpl.cpp as name):



          /* 
          * Author : <<name>>
          * Date : <<date>>
          */


          and in your vimrc:



          function AddTemplate(tmpl_file)
          exe "0read " . a:tmpl_file
          let substDict = {}
          let substDict["name"] = $USER
          let substDict["date"] = strftime("%Y %b %d %X")
          exe '%s/<<([^>]*)>>/=substDict[submatch(1)]/g'
          set nomodified
          normal G
          endfunction

          autocmd BufNewFile *.c,*.cc,*.cpp,*.h call AddTemplate("~/tmpl/tmpl.cpp")



          The set nomodified tells Vim that the file was not modified. That way you can exit the file with :q as long as you don't add additional text. Usefull if you typed the wrong file name.



          If you only want to act on files within a special directory ~/project, you can add the following at the start of the function AddTemplate:



          let fully_qualified_file = expand('%:p')
          if 0 != match(fully_qualified_file, $HOME . '/project/.*')
          return
          endif





          share|improve this answer


























          • did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

            – Yedhin
            Jan 19 at 15:37











          • Sorry misread your question, updating

            – Ralf
            Jan 19 at 15:47











          • It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

            – Yedhin
            Jan 19 at 15:49











          • Updated, now easy replace of name, date and whatever you want to add.

            – Ralf
            Jan 19 at 15:58











          • It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

            – Yedhin
            Jan 19 at 16:03











          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%2f54268047%2fhow-to-auto-insert-a-template-code-into-a-new-file-of-particular-extension-that%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














          First of all: There are template plugins, you should search the net for "vim template plugin". Perhaps you find something useful.



          If you want to do it yourself:



          Create your template file like this (I assume ~/tmpl/tmpl.cpp as name):



          /* 
          * Author : <<name>>
          * Date : <<date>>
          */


          and in your vimrc:



          function AddTemplate(tmpl_file)
          exe "0read " . a:tmpl_file
          let substDict = {}
          let substDict["name"] = $USER
          let substDict["date"] = strftime("%Y %b %d %X")
          exe '%s/<<([^>]*)>>/=substDict[submatch(1)]/g'
          set nomodified
          normal G
          endfunction

          autocmd BufNewFile *.c,*.cc,*.cpp,*.h call AddTemplate("~/tmpl/tmpl.cpp")



          The set nomodified tells Vim that the file was not modified. That way you can exit the file with :q as long as you don't add additional text. Usefull if you typed the wrong file name.



          If you only want to act on files within a special directory ~/project, you can add the following at the start of the function AddTemplate:



          let fully_qualified_file = expand('%:p')
          if 0 != match(fully_qualified_file, $HOME . '/project/.*')
          return
          endif





          share|improve this answer


























          • did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

            – Yedhin
            Jan 19 at 15:37











          • Sorry misread your question, updating

            – Ralf
            Jan 19 at 15:47











          • It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

            – Yedhin
            Jan 19 at 15:49











          • Updated, now easy replace of name, date and whatever you want to add.

            – Ralf
            Jan 19 at 15:58











          • It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

            – Yedhin
            Jan 19 at 16:03
















          1














          First of all: There are template plugins, you should search the net for "vim template plugin". Perhaps you find something useful.



          If you want to do it yourself:



          Create your template file like this (I assume ~/tmpl/tmpl.cpp as name):



          /* 
          * Author : <<name>>
          * Date : <<date>>
          */


          and in your vimrc:



          function AddTemplate(tmpl_file)
          exe "0read " . a:tmpl_file
          let substDict = {}
          let substDict["name"] = $USER
          let substDict["date"] = strftime("%Y %b %d %X")
          exe '%s/<<([^>]*)>>/=substDict[submatch(1)]/g'
          set nomodified
          normal G
          endfunction

          autocmd BufNewFile *.c,*.cc,*.cpp,*.h call AddTemplate("~/tmpl/tmpl.cpp")



          The set nomodified tells Vim that the file was not modified. That way you can exit the file with :q as long as you don't add additional text. Usefull if you typed the wrong file name.



          If you only want to act on files within a special directory ~/project, you can add the following at the start of the function AddTemplate:



          let fully_qualified_file = expand('%:p')
          if 0 != match(fully_qualified_file, $HOME . '/project/.*')
          return
          endif





          share|improve this answer


























          • did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

            – Yedhin
            Jan 19 at 15:37











          • Sorry misread your question, updating

            – Ralf
            Jan 19 at 15:47











          • It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

            – Yedhin
            Jan 19 at 15:49











          • Updated, now easy replace of name, date and whatever you want to add.

            – Ralf
            Jan 19 at 15:58











          • It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

            – Yedhin
            Jan 19 at 16:03














          1












          1








          1







          First of all: There are template plugins, you should search the net for "vim template plugin". Perhaps you find something useful.



          If you want to do it yourself:



          Create your template file like this (I assume ~/tmpl/tmpl.cpp as name):



          /* 
          * Author : <<name>>
          * Date : <<date>>
          */


          and in your vimrc:



          function AddTemplate(tmpl_file)
          exe "0read " . a:tmpl_file
          let substDict = {}
          let substDict["name"] = $USER
          let substDict["date"] = strftime("%Y %b %d %X")
          exe '%s/<<([^>]*)>>/=substDict[submatch(1)]/g'
          set nomodified
          normal G
          endfunction

          autocmd BufNewFile *.c,*.cc,*.cpp,*.h call AddTemplate("~/tmpl/tmpl.cpp")



          The set nomodified tells Vim that the file was not modified. That way you can exit the file with :q as long as you don't add additional text. Usefull if you typed the wrong file name.



          If you only want to act on files within a special directory ~/project, you can add the following at the start of the function AddTemplate:



          let fully_qualified_file = expand('%:p')
          if 0 != match(fully_qualified_file, $HOME . '/project/.*')
          return
          endif





          share|improve this answer















          First of all: There are template plugins, you should search the net for "vim template plugin". Perhaps you find something useful.



          If you want to do it yourself:



          Create your template file like this (I assume ~/tmpl/tmpl.cpp as name):



          /* 
          * Author : <<name>>
          * Date : <<date>>
          */


          and in your vimrc:



          function AddTemplate(tmpl_file)
          exe "0read " . a:tmpl_file
          let substDict = {}
          let substDict["name"] = $USER
          let substDict["date"] = strftime("%Y %b %d %X")
          exe '%s/<<([^>]*)>>/=substDict[submatch(1)]/g'
          set nomodified
          normal G
          endfunction

          autocmd BufNewFile *.c,*.cc,*.cpp,*.h call AddTemplate("~/tmpl/tmpl.cpp")



          The set nomodified tells Vim that the file was not modified. That way you can exit the file with :q as long as you don't add additional text. Usefull if you typed the wrong file name.



          If you only want to act on files within a special directory ~/project, you can add the following at the start of the function AddTemplate:



          let fully_qualified_file = expand('%:p')
          if 0 != match(fully_qualified_file, $HOME . '/project/.*')
          return
          endif






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 17:16

























          answered Jan 19 at 15:26









          RalfRalf

          1,135311




          1,135311













          • did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

            – Yedhin
            Jan 19 at 15:37











          • Sorry misread your question, updating

            – Ralf
            Jan 19 at 15:47











          • It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

            – Yedhin
            Jan 19 at 15:49











          • Updated, now easy replace of name, date and whatever you want to add.

            – Ralf
            Jan 19 at 15:58











          • It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

            – Yedhin
            Jan 19 at 16:03



















          • did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

            – Yedhin
            Jan 19 at 15:37











          • Sorry misread your question, updating

            – Ralf
            Jan 19 at 15:47











          • It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

            – Yedhin
            Jan 19 at 15:49











          • Updated, now easy replace of name, date and whatever you want to add.

            – Ralf
            Jan 19 at 15:58











          • It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

            – Yedhin
            Jan 19 at 16:03

















          did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

          – Yedhin
          Jan 19 at 15:37





          did you just repeat back all that i had typed in my post? :D I had clearly mentioned that i have figured out the part of creating a template file and adding it to an autcmd.

          – Yedhin
          Jan 19 at 15:37













          Sorry misread your question, updating

          – Ralf
          Jan 19 at 15:47





          Sorry misread your question, updating

          – Ralf
          Jan 19 at 15:47













          It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

          – Yedhin
          Jan 19 at 15:49





          It's okay. Can happen with anyone. By the way you made remember to include 0 with the read command. Thanks for that. :)

          – Yedhin
          Jan 19 at 15:49













          Updated, now easy replace of name, date and whatever you want to add.

          – Ralf
          Jan 19 at 15:58





          Updated, now easy replace of name, date and whatever you want to add.

          – Ralf
          Jan 19 at 15:58













          It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

          – Yedhin
          Jan 19 at 16:03





          It should work. But would you be able to give me an idea on how to run the autocmd only when creating the new file under a particular directory, like say ~/project, or any of its sub-directories?

          – Yedhin
          Jan 19 at 16:03


















          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%2f54268047%2fhow-to-auto-insert-a-template-code-into-a-new-file-of-particular-extension-that%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

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre