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!
vimrc vimscript
New contributor
add a comment |
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!
vimrc vimscript
New contributor
add a comment |
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!
vimrc vimscript
New contributor
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
vimrc vimscript
New contributor
New contributor
New contributor
asked 6 hours ago
Songcheng Li
63
63
New contributor
New contributor
add a comment |
add a comment |
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!
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
add a comment |
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!
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
add a comment |
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!
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
add a comment |
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!
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!
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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