how to load a .vim file on a mode line

166 views Asked by At

For a few specific files I need a set of mappings, which I don't want to be active in my ~/.vimrc file. So I defined them in a file XXX.vim. How can I load XXX.vim on a mode line and where should it be placed in my .vim directory?

1

There are 1 answers

2
romainl On

You can't source a script in a modeline for obvious reason. All you can do is set some options.

The canonical way to have some options/mappings/commands available for some files and not others is to use an :help autocommand like this one:

augroup mystuff
    autocmd!
    autocmd BufNewFile,BufRead *.foo nnoremap <buffer> <key> bar
augroup END

You can use the pattern above for all files with the foo extension or something like foo/bar/* for all file under foo/bar/, etc.

If you want those mappings only for a specific filetype, say ruby, then it is best to use the built-in ftplugin mechanism:

  1. Make sure you have either of the following lines in your vimrc:

    filetype plugin on
    filetype plugin indent on
    filetype indent plugin on
    
  2. Create this file (and the necessary directories) if it doesn't exist:

    " Unix-like system
    ~/.vim/after/ftplugin/ruby.vim
    
    " Windows
    %userprofile%\vimfiles\after\ftplugin\ruby.vim
    
  3. Add your buffer-local mappings to that file:

    nnoremap <buffer> <key> something