2

So, I'm pretty happy with my .vimrc config for Python - except I started leaving off the .py suffix for scripts and now vim has no idea that the file I'm editing is a Python file.

Clarification: I've confirmed that vim knows I'm editing a python file using :echo &filetype which reports back 'python'.

So, the real question is how to assign python-specific behavior when I'm currently using file extensions for this purpose (see below). Can that be driven with filetype? Does one need both?

Regarding the lack of suffixes - my code is only intended for unix hosts, where this is commonplace, and I like hiding the implementation details so I could hypothetically change the implementation later.

Here's a fraction of my .vimrc:

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

1 Answer 1

1

The key to enable python-specific settings for files with no extension whatsoever, is to enable filetype plugin and, optionally, to teach Vim how Python file looks like.

Modify those files:

$HOME/.vimrc

Enable filetype plugin in Vim. This alllows to put language-specific settings into $HOME/.vim/ftplugin/ directory. Those settings are based on file type, not file extension.

filetype plugin on
syntax on

$HOME/.vim/scripts.vim

Teach Vim how Python file looks like. In this example Vim treats file as a python script if it contains python string somewhere in its first line. You can easily adapt regular expression here to incorporate more advanced checks.

Please note that did_filetype() ensures, that files with .py extensions without python in the first line are still considered to be python scripts.

if did_filetype()
    finish
endif
if getline(1) =~ '.*python.*'
    setfiletype python
endif

$HOME/.vim/ftplugin/python.vim

Finally, put all the python-specific settings here:

setlocal tabstop=4
setlocal shiftwidth=4
setlocal expandtab
setlocal softtabstop=4
setlocal textwidth=79

More info:

Modyfing of scripts.vim is not necessary if file has some standard python header, on example #!/usr/bin/python or #!/usr/bin/env python.

This heuristic is being performed in /usr/share/vim/vim73/scripts.vim (your path may be different, depending on Vim version and install location).

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you - it doesn't address the problem at hand, but looks interesting.
No problem. I've expanded my answer with additional options - hope those help.
Thanks - it looks like I need option #1. Can you show how that would look in a .vimrc file. I think I've got to turn that detection on and then set various feature on using both filetype & file extension(?)
I'm surprised it isn't working for you by default. What's your Vim version? Is it compiled with python support? (see :version). If all else fails, you can always use option #2 with 'if getline(1) =~ '^#!/usr/bin/python$'' or similar.
BTW, for option #1 Vim's implementation of file type detection is in 'scripts.vim' located under /usr/share/vim (path may differ). My Vim installation detects file language by matching the first line of a file, so it's in fact option #2 we discussed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.