0

I'd like to define different mappings for files which have the same suffix.

E.g. define a general mapping for all ruby files and a different mapping only for rspec files:

au BufNewFile,BufRead *_spec.rb map <Leader>t :w!<cr>:!rspec %<cr>
au BufNewFile,BufRead *.rb map <Leader>t :w!<cr>:!rspec %:r_spec.rb<cr>

The above solution does not work on my machine, because the second au "overwrites" the first one.

Is it possible to write this kind of au?

Update: just placing the most specific (spec) one below the generic one (rb) works if I have only one buffer opened. As soon as I open a spec file, the *.rb mapping is lost for the regular ruby files.

José Ricardo
  • 1,479
  • 1
  • 14
  • 28

2 Answers2

1

Reversing the autocommands and adding <buffer> should get you the desired behavior in this case, i.e.:

au BufNewFile,BufRead *.rb      map <buffer> ,t action2
au BufNewFile,BufRead *_spec.rb map <buffer> ,t action1

This ordering will achieve the proper per-filename mappings.

But you should note that when opening *_spec.rb files, both map commands will run: action1 and action2. This can be un-desireable for certain commands.

Also, if you've set <Leader> to comma: ,, then your mappings should look like this:

au BufNewFile,BufRead *.rb      map <buffer> <Leader>t action2
au BufNewFile,BufRead *_spec.rb map <buffer> <Leader>t action1
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • First, thank you for the tip about the key. Second, it works only if I have one buffer opened. If I have the spec file and the ruby file, each one in one buffer in a split window, for some reason the spec mapping is overriding the ruby mapping. Any suggestions? – José Ricardo Jul 21 '12 at 00:53
  • It probably depends on what action1 and action2 are. Try using a "local" option. (e.g. `map t action2`). See `:help map-local` – Conner Jul 21 '12 at 00:59
  • I've updated the question using the real mappings I'm trying to define. They basically run the spec file if I have the spec or the subject open. – José Ricardo Jul 21 '12 at 01:04
  • Try `map t etc.` – Conner Jul 21 '12 at 01:05
  • No, they should not look like `t` (and, by the way, there is `` for filetype-specific things). Why obfuscate the reading? `` is used to add basic ability for customizing mappings in plugins (which itself is not enough), it is not for user’s own use: you get used to type `,t`, not `t`. More, I use `,` as leader for my own mappings, but never set `mapleader` to it in order to prevent plugins from spoiling them. – ZyX Jul 21 '12 at 08:32
0

Do the more specific one last. In other words, reverse the order of those two commands.

au BufNewFile,BufRead *.rb map <buffer> ,t action2
au BufNewFile,BufRead *_spec.rb map <buffer> ,t action1

One command is still overriding the other, but this way it achieves the effect you want. Note that you might want to use a more specific map like nmap, or better yet nnoremap, so that you don't run into issues with this map in other modes.

Conner
  • 30,144
  • 8
  • 52
  • 73