【问题标题】:autocommand for filetypes working for setting options but not for mappings用于设置选项但不适用于映射的文件类型的自动命令
【发布时间】:2014-01-28 13:37:30
【问题描述】:

我的 中有以下几行:

set shiftwidth=4
autocmd FileType tex setlocal shiftwidth=2
autocmd FileType tex map xyz doSomething

如果我打开一个文本文件(文件类型设置正确),然后执行set sw,我得到 4。 如果我打开一个 tex 文件(文件类型设置正确),我会为 set sw 得到 2。这部分有效!

现在对于映射,我键入map xyz,我看到两种文件类型的映射都已完成。我做错了什么?

一开始我认为映射有问题,所以我尝试了:

set history=100
autocmd FileType tex setlocal history=123

但是所有文件类型的历史都设置为 123...shiftwidth 怎么能工作,但history 不行?

【问题讨论】:

    标签: vimrc vim


    【解决方案1】:

    :setlocal:set:map <buffer>:map(参见 :h :map-local):

    autocmd FileType tex map <buffer> xyz doSomething
    

    另外,you should use :noremap;它使映射不受重映射和递归的影响。

    我建议将设置和映射放入~/.vim/after/ftplugin/tex.vim,而不是定义大量:autocmd FileType;这要求您拥有:filetype plugin on

    选项范围

    :help 提示哪些选项仅是全局的(如 'history'),哪些支持(bufferwindow)局部值;比较第二行:

    'history' 'hi'      number  (Vim default: 20, Vi default: 0)
                        global
    'shiftwidth' 'sw'   number  (default 8)
                        local to buffer
    

    【讨论】:

    • 谢谢,非常明确的答案,正是我想要的。但是,它没有回答为什么setlocal history 不起作用。这是某种无法在本地更改的全局设置吗?这并不重要,因为我不想这样做,只是好奇。 :-)
    • 我不知何故错过了那部分......我也为此附加了一个解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多