Search code examples
vimautocompletefile-type

Vim bind some hotkeys different to filetype


I want to bind something like this:

  • For CSS, HTML files: <c-space> <c-x><c-n>

  • For Ruby files: <c-space> <c-x><c-u>

How to do this?


Solution

  • The documentation is more complete, precise and correct, but briefly:

    Make a file in your ftplugin folder (create this folder if necessary) starting with the filetype you'd like to use.

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

    and in one of of these files, the python.vim one for example, a line like

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

    will be a bind only defined for this filetype, just in this buffer. Make sure filetype plugin on or similar appears in your vimrc.

    Edit: excellent suggestions by Peter Rincker incorporated: noremap version of map command and <buffer> flag. He also reminds us to use setlocal instead of set in our filetype plugin files.