Gary Johnson a écrit :
On 2007-03-01, kib2 <[EMAIL PROTECTED]> wrote:
Try ':filetype on'

Yakov


filetype on is already inside my mimrc.
In fact, I've made a special function for the different languages I use :


au BufEnter *.py    exe Fpython()

function Fpython()
   au FileType python source C:\Program Files\vim\vim70\scripts\python.vim
   autocmd BufRead,BufNewFile *.py syntax on
   autocmd BufRead,BufNewFile *.py set ai
filetype on
   " les tabs sont des espaces pour Python:
   au BufNewFile,BufRead *.py set expandtab
   au BufNewFile,BufRead *.py set tabstop=4
   au BufNewFile,BufRead *.py set shiftwidth=4

   etc...
endfunction

Where do I go wrong ?

If you want vim to automatically detect the file type, "filetype on" must be executed _before_ any buffer is loaded, that is, before the BufNewFile or BufRead events, and certainly before the BufEnter event. Executing "filetype on" is like arming the file detection mechanism, but the code above arms the detection _after_ the triggering events have occurred.

So first of all, the "filetype on" line should be in the main body of the _vimrc so that it is executed when _vimrc is sourced, not later when an autocommand is triggered.

Secondly, if you want "syntax on", et al. to be executed when you enter a buffer containing a Python file, you should either execute Fpython() in an autocommand and remove all the autocommands from within Fpython(), or you should execute Fpython() unconditionally and leave the autocommands within Fpython() as they are. Otherwise, the settings within Fpython() won't be executed until the second Python file is loaded and vim's behavior will appear to be inconsistent.

Your whole approach is really more complicated and error-prone than it needs to be. I would recommend that you take all the settings now in Fpython() (except "filetype on"), remove the autocommand parts, and put them all in a separate file, C:\Program Files\vim\vim70\after\ftplugin\python.vim. Then change all the "set" commands to "setlocal", like this:

    source "C:/Program Files/vim/vim70/scripts/python.vim"
    syntax on
    setlocal ai

    " les tabs sont des espaces pour Python:
    setlocal expandtab
    setlocal tabstop=4
    setlocal shiftwidth=4

Vim's filetype detection will cause this file to be sourced whenever you open a Python file. Using "setlocal" will cause those settings to apply only to the current buffer, that is, only to buffers containing Python files, rather that to every buffer in the current vim instance.

Lastly, if you execute "syntax on" like this, you enable syntax highlighting for every buffer, regardless of file type. If you want syntax highlighting for only certain file types, I would recommend putting

    syntax manual

in your _vimrc and

    setlocal syntax=ON

in the C:\Program Files\vim\vim70\after\ftplugin\ file for each filetype for which you want highlighting enabled.

HTH,
Gary

Thanks Gary for this long Vim lesson, I'll try to modify my _vimrc now with all your comments in mind.
Cheers,
Kib.

Reply via email to