0

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?

Wybo Dekker
  • 132
  • 6

1 Answers1

0

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
    
romainl
  • 186,200
  • 21
  • 280
  • 313
  • My problem is that I want the mappings for /some/ of my .tex files, not for all. The above does not seem to be a solution for that. But I'll have to study the details... – Wybo Dekker Mar 26 '22 at 08:37
  • Well you will have to find a way to discriminate those files. What is described above is the general mechanism, it's up to you, now, to adapt it to your own circumstances. – romainl Mar 26 '22 at 08:49