vim:filetype plugin conflict

2019-09-19 13:12发布

I need to do python and js program at the same time.

And also, I have some language specific seetings located at the ~/.vim/after/ftplugin/ directory

Here I'll show you the content of these files:

In python.vim

    set tabstop=4
    set shiftwidth=4
    set expandtab
    set softtabstop=4
    nnoremap Y :Autoformat<CR>

In my javascript.vim

    nnoremap Y :call JsBeautify()<CR>

Imagine this scenery:

  • Open a python file using vim
  • Then open a js file in a new windown using :split
  • Now whenever I press Y
  • Vim will call JsBeautify even if I'm editing the python file

That's not what I want

Now I'm wondering if there is a way to make vim work like this:

  • Judge the filetype of the current window
  • And source the specific *.vim file in accordance with the filetype from the ~/.vim/after/ftplugin/ directory
  • Then in the above-mentioned scenery
  • It will call Autoformat when I am editing the python file and JsBeautify() when I am editing the js file
    ~

标签: vim plugins
1条回答
疯言疯语
2楼-- · 2019-09-19 13:54

You must make your options and mappings as local as possible.

In ~/.vim/after/ftplugin/python.vim:

setlocal tabstop=4
setlocal shiftwidth=4
setlocal expandtab
setlocal softtabstop=4
nnoremap <buffer> Y :Autoformat<CR>

In ~/.vim/after/ftplugin/javascript.vim:

nnoremap <buffer> Y :call JsBeautify()<CR>

As FDinoff wrote in his comment:

:help :setlocal
:help :map-local
查看更多
登录 后发表回答