5

I started using a plugin that conflicts with my existing maps, but instead of remapping all of it's maps, I just want to add a prefix. I thought I'd be able to do this with LocalLeader.

Vimdoc says:

<LocalLeader> is just like <Leader>, except that it uses "maplocalleader" instead of "mapleader". <LocalLeader> is to be used for mappings which are local to a buffer.

It seems that the only way to set localleader is to set a global variable (the docs don't mention this, but b:maplocalleader didn't work):

let maplocalleader = '\\'

And I don't see how I'd cleanly unset that variable (an autocmd that clears it after plugins are setup!?)

Is there a way to do this? Or is LocalLeader only to give one global prefix and one filetype-specific prefix?

idbrii
  • 10,975
  • 5
  • 66
  • 107

2 Answers2

3

Your last hunch is correct. If the plugin uses <Leader> (and it should unless it's a filetype plugin), there's no use in messing with maplocalleader.

Remapping is canonically done via <Plug> mappings, which the plugin hopefully offers. Some plugins do define a lot of similar mappings, some of those define a g:pluginname_mappingprefix (or so) variable to save you from having to remap all mappings individually. If your plugin doesn't, maybe write a suggestion / patch to the plugin author.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • So in my case, I'd have to ask the plugin author to add the ability to specify a custom prefix? (I guess they could use LocalLeader to do this if they save the current value, set the configured value, and restore the saved value when they're done.) – idbrii Aug 22 '12 at 15:38
  • 1
    If there are `` mappings, the plugin (whose name you didn't tell us) doesn't need to be changed, it's just tedious for many mappings. A custom prefix would rather be something like `c` or `,c`; as I said, LocalLeader is meant for buffer-local mappings. – Ingo Karkat Aug 22 '12 at 15:59
2

While @IngoKarkat solution is a prefered one, there is a hack which lets you do what you want: the SourcePre event:

autocmd SourcePre *               :let maplocalleader='\\'
autocmd SourcePre plugin-name.vim :let maplocalleader='_'

. This works for <Leader> as well. There are lots of cases when this won’t work though. You can as well use SourceCmd for this job, using something like

function s:Source(newmll)
    try
        let oldmll=g:maplocalleader
        let g:maplocalleader=a:newmll
        source <amatch>
    finally
        let g:maplocalleader=oldmll
    endtry
endfunction
let maplocalleader='\\'
autocmd SourceCmd plugin-name.vim :call s:Source('_')

in SourceCmd is the only way I see to restore maplocalleader after plugin was sourced, but SourceCmd event here won’t be launched for any file sourced inside plugin-name.vim. For some poorly written plugins (I mean, those that emit errors while sources) putting :source inside a :try block will break execution at the point where error occurs. Should not happen most of time though. You may also want to use */ftplugin/plugin-name.vim as a pattern instead of plugin-name.vim.

ZyX
  • 52,536
  • 7
  • 114
  • 135