【问题标题】:Vim bind some hotkeys different to filetypeVim 绑定一些与文件类型不同的热键
【发布时间】:2016-09-11 04:06:41
【问题描述】:

我想绑定这样的东西:

  • 对于 CSS、HTML 文件:<c-space> <c-x><c-n>

  • 对于 Ruby 文件:<c-space> <c-x><c-u>

如何做到这一点?

【问题讨论】:

    标签: vim autocomplete file-type


    【解决方案1】:

    documentation 更完整、更准确、更正确,但简短:

    在您的 ftplugin 文件夹中创建一个文件(如有必要,创建此文件夹),以您想要使用的文件类型开头。

    ├── vim
    │   ├── after
    │   ├── autoload
    │   ├── bundle
    │   ├── ftplugin
    │   │   ├── python.vim
    │   │   ├── rnoweb.vim
    │   │   └── tex.vim
    │   └── syntax
    

    在其中一个文件中,例如 python.vim 文件中的一行

    noremap! <buffer> <F5> <Esc>:w<CR>:!python % <CR>
    

    将是仅为此文件类型定义的绑定,就在此缓冲区中。确保您的 vimrc 中出现filetype plugin on 或类似名称。

    编辑:合并了 Peter Rincker 的优秀建议:地图命令的 noremap 版本和&lt;buffer&gt; 标志。他还提醒我们在文件类型插件文件中使用setlocal 而不是set

    【讨论】:

    • 您将需要使用&lt;buffer&gt; 标志来使映射位于缓冲区的本地。例如inoremap &lt;buffer&gt; &lt;c-space&gt; &lt;c-x&gt;&lt;c-o&gt;。您还需要注意使用setlocal 作为选项。
    【解决方案2】:

    使用自动命令:

    autocmd filetype css inoremap <buffer> <c-space> <c-x><c-n>  
    autocmd filetype html inoremap <buffer> <c-space> <c-x><c-n>
    autocmd filetype ruby inoremap <buffer> <c-space> <c-x><c-u>
    

    【讨论】:

    • 你也可以合并行,autocmd filetype css,html setlocal...
    • 你必须删除setlocal
    【解决方案3】:

    将以下内容放入您的 .vimrc 中。用您的绑定替换 do stuff 行。

    :if match(@%, ".css") >=0 
    : do css stuff
    :endif
    
    :if match(@%, ".rb") >=0 
    : do ruby stuff
    :endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多