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












1














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



























    1














    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

























      1












      1








      1







      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













      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













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 10 at 22:00









      Songcheng Li

      63




      63






















          1 Answer
          1






          active

          oldest

          votes


















          3














          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
            Dec 10 at 23:26






          • 2




            @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
            Dec 11 at 2:01













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


          }
          });














          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









          3














          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
            Dec 10 at 23:26






          • 2




            @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
            Dec 11 at 2:01


















          3














          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
            Dec 10 at 23:26






          • 2




            @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
            Dec 11 at 2:01
















          3












          3








          3






          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 Dec 10 at 23:29

























          answered Dec 10 at 23:25









          Rich

          14.6k11764




          14.6k11764












          • Thank you so much!
            – Songcheng Li
            Dec 10 at 23:26






          • 2




            @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
            Dec 11 at 2:01




















          • Thank you so much!
            – Songcheng Li
            Dec 10 at 23:26






          • 2




            @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
            Dec 11 at 2:01


















          Thank you so much!
          – Songcheng Li
          Dec 10 at 23:26




          Thank you so much!
          – Songcheng Li
          Dec 10 at 23:26




          2




          2




          @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
          Dec 11 at 2:01






          @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
          Dec 11 at 2:01




















          draft saved

          draft discarded




















































          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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]