What is the difference of using `au BufNewFile,BufRead *.py` and `au Filetype python` in .vimrc?











up vote
1
down vote

favorite












As I am setting .vimrc, I found these two code blocks have the same functionality.



au Filetype python set
tabstop=4
softtabstop=4
shiftwidth=4
textwidth=79


and



au BufNewFile,BufRead *.py
set tabstop=4
set softtabstop=4
set shiftwidth=4
set textwidth=79


I reckon that there is a very subtle difference between them, but cannot figure it out. Like, vim interpret Filetype when open the file etc.



Can any one explain it to me with mercy?



Thank you very much!










share|improve this question







New contributor




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
























    up vote
    1
    down vote

    favorite












    As I am setting .vimrc, I found these two code blocks have the same functionality.



    au Filetype python set
    tabstop=4
    softtabstop=4
    shiftwidth=4
    textwidth=79


    and



    au BufNewFile,BufRead *.py
    set tabstop=4
    set softtabstop=4
    set shiftwidth=4
    set textwidth=79


    I reckon that there is a very subtle difference between them, but cannot figure it out. Like, vim interpret Filetype when open the file etc.



    Can any one explain it to me with mercy?



    Thank you very much!










    share|improve this question







    New contributor




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






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      As I am setting .vimrc, I found these two code blocks have the same functionality.



      au Filetype python set
      tabstop=4
      softtabstop=4
      shiftwidth=4
      textwidth=79


      and



      au BufNewFile,BufRead *.py
      set tabstop=4
      set softtabstop=4
      set shiftwidth=4
      set textwidth=79


      I reckon that there is a very subtle difference between them, but cannot figure it out. Like, vim interpret Filetype when open the file etc.



      Can any one explain it to me with mercy?



      Thank you very much!










      share|improve this question







      New contributor




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











      As I am setting .vimrc, I found these two code blocks have the same functionality.



      au Filetype python set
      tabstop=4
      softtabstop=4
      shiftwidth=4
      textwidth=79


      and



      au BufNewFile,BufRead *.py
      set tabstop=4
      set softtabstop=4
      set shiftwidth=4
      set textwidth=79


      I reckon that there is a very subtle difference between them, but cannot figure it out. Like, vim interpret Filetype when open the file etc.



      Can any one explain it to me with mercy?



      Thank you very much!







      vimrc vimscript






      share|improve this question







      New contributor




      Songcheng Li 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




      Songcheng Li 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




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









      asked 6 hours ago









      Songcheng Li

      63




      63




      New contributor




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





      New contributor





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






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






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Your intuition is correct: the two autocommands are very similar.



          The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.



          However, try running the following commands with each of the two autocommands in place:



          :new
          :w new.py


          You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.



          However, in my opinion, there is an even better solution. Create a file in the location:



          ~/.vim/after/ftplugin/python.vim


          And enter the contents:



          set tabstop=4
          set softtabstop=4
          set shiftwidth=4
          set textwidth=79


          This will have the same effect, no autocommands required!






          share|improve this answer























          • Thank you so much!
            – Songcheng Li
            5 hours ago






          • 1




            @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
            – muru
            2 hours ago













          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',
          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
          });


          }
          });






          Songcheng Li 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%2f18231%2fwhat-is-the-difference-of-using-au-bufnewfile-bufread-py-and-au-filetype-py%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








          up vote
          3
          down vote













          Your intuition is correct: the two autocommands are very similar.



          The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.



          However, try running the following commands with each of the two autocommands in place:



          :new
          :w new.py


          You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.



          However, in my opinion, there is an even better solution. Create a file in the location:



          ~/.vim/after/ftplugin/python.vim


          And enter the contents:



          set tabstop=4
          set softtabstop=4
          set shiftwidth=4
          set textwidth=79


          This will have the same effect, no autocommands required!






          share|improve this answer























          • Thank you so much!
            – Songcheng Li
            5 hours ago






          • 1




            @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
            – muru
            2 hours ago

















          up vote
          3
          down vote













          Your intuition is correct: the two autocommands are very similar.



          The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.



          However, try running the following commands with each of the two autocommands in place:



          :new
          :w new.py


          You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.



          However, in my opinion, there is an even better solution. Create a file in the location:



          ~/.vim/after/ftplugin/python.vim


          And enter the contents:



          set tabstop=4
          set softtabstop=4
          set shiftwidth=4
          set textwidth=79


          This will have the same effect, no autocommands required!






          share|improve this answer























          • Thank you so much!
            – Songcheng Li
            5 hours ago






          • 1




            @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
            – muru
            2 hours ago















          up vote
          3
          down vote










          up vote
          3
          down vote









          Your intuition is correct: the two autocommands are very similar.



          The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.



          However, try running the following commands with each of the two autocommands in place:



          :new
          :w new.py


          You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.



          However, in my opinion, there is an even better solution. Create a file in the location:



          ~/.vim/after/ftplugin/python.vim


          And enter the contents:



          set tabstop=4
          set softtabstop=4
          set shiftwidth=4
          set textwidth=79


          This will have the same effect, no autocommands required!






          share|improve this answer














          Your intuition is correct: the two autocommands are very similar.



          The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when the file type is set to Python, which Vim will generally do automatically for .py files.



          However, try running the following commands with each of the two autocommands in place:



          :new
          :w new.py


          You will find that the Filetype autocommand runs, but the other does not. For this reason, you should probably use the first version if you want to use an autocommand for this.



          However, in my opinion, there is an even better solution. Create a file in the location:



          ~/.vim/after/ftplugin/python.vim


          And enter the contents:



          set tabstop=4
          set softtabstop=4
          set shiftwidth=4
          set textwidth=79


          This will have the same effect, no autocommands required!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 5 hours ago









          Rich

          14.4k11764




          14.4k11764












          • Thank you so much!
            – Songcheng Li
            5 hours ago






          • 1




            @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
            – muru
            2 hours ago




















          • Thank you so much!
            – Songcheng Li
            5 hours ago






          • 1




            @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
            – muru
            2 hours ago


















          Thank you so much!
          – Songcheng Li
          5 hours ago




          Thank you so much!
          – Songcheng Li
          5 hours ago




          1




          1




          @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
          – muru
          2 hours ago






          @SongchengLi I think Vim's Python ftplugin also reads shebangs (so a file with no extension but #! /usr/bin/python as the shebang gets detected as a Python file).
          – muru
          2 hours ago












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










          draft saved

          draft discarded


















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













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












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





          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%2fvi.stackexchange.com%2fquestions%2f18231%2fwhat-is-the-difference-of-using-au-bufnewfile-bufread-py-and-au-filetype-py%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu