tpope / vim-commentary

commentary.vim: comment stuff out
http://www.vim.org/scripts/script.php?script_id=3695
5.9k stars 214 forks source link

custom keybindings don't override default keybindings #84

Closed nicodebo closed 7 years ago

nicodebo commented 7 years ago

General informations

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Feb  6 2017 16:11:46)
Included patches: 1-314
Compiled by Arch Linux

Minimal vimrc

" Plugin management ------------------------------------------------------- {{{

call plug#begin('~/.vim/plugged')
  Plug 'https://github.com/tpope/vim-commentary'
call plug#end()

" }}}

" Remap vim-commentary plugin --------------------------------------------- {{{

let mapleader = ","
nmap <leader>c <Plug>Commentary
xmap <leader>c <Plug>Commentary
omap <leader>c <Plug>Commentary
nmap <leader>cc <Plug>CommentaryLine

" }}}

Step to reproduce

Expected behavior

Well, I'm not totally sure about whether I did something wrong but I expect the default mapping not to load when I'm defining my own mappings.

Possible fix

Shouldn't the line 94 of the vim-commentary.vim source file be

if !hasmapto('<Plug>Commentary') && maparg('gc','n') ==# ''

instead of

if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''

?

At least, it seems to fix my issue but I would need some advice.

tpope commented 7 years ago

What does the extra map hurt? My philosophy is to always map if it doesn't get in the way, because otherwise you're just being needlessly hostile to any coworker who sits down at your computer without having your custom maps memorized.

nicodebo commented 7 years ago

In fact, I'm using bépo keyboard layout (kind of the french version of dvorak). Thus, to have my {h,j,k,l} vim movements like on a classic azerty layout, I have mapped {h,j,k,l} to {c,t,s,r}. So basically I have these following lines in my vimrc:

" [HJKL] -> {CTSR}
" ————————————————
noremap c h
noremap r l
noremap t j
noremap s k
noremap C H
noremap R L
noremap T J
noremap S K

" {HJKL} <- [CTSR]
" ————————————————
noremap j t
noremap J T
noremap l c
noremap L C
noremap h r
noremap H R
noremap k s
noremap K S

Now, when using this particular layout (which I can agree that it is not very coworker friendly) with vim-commentary, I have a delay when moving left with my c key because of the cgc plug map (nmap cgc <Plug>ChangeCommentary). I have also tried to unmap the vim-commentary mappings directly in the vimrc but they were still loaded and that's why I'm using the abovementionned "fix".

vheon commented 7 years ago

I have also tried to unmap the vim-commentary mappings directly in the vimrc but they were still loaded and that's why I'm using the abovementionned "fix".

That is because the plugin file is sourced after the vimrc. I guess that if you do the unmapping in a function and call it at VimEnter you should be fine; something like:

function UnmapCommentary()
  " unmap here
endfunction

autocmd VimEnter * call UnmapCommentary()

This looks like a very specific issue which could be satisfied by a very specific solution like this 😃

nicodebo commented 7 years ago

Your solution works well and is better than modifiying the source code itself like I was doing until now.

For reference I have the following lines in vimrc to fix the mapping conflict:

function! UnmapCommentary()
  unmap gc
  nunmap gcc
  nunmap cgc
  nunmap gcu
endfunction

xmap <leader>c  <Plug>Commentary
nmap <leader>c  <Plug>Commentary
omap <leader>c  <Plug>Commentary
nmap <leader>cc <Plug>CommentaryLine
nmap l<leader>c <Plug>ChangeCommentary
nmap <leader>cu <Plug>Commentary<Plug>Commentary

augroup bepo_clash
  autocmd!
  autocmd VimEnter * call UnmapCommentary()
augroup END

Thanks to both of you.

tpope commented 7 years ago

I also thinking wrapping the cgc map in a nested check for maparg('c', 'n') would be quite reasonable, if that would help. Certainly if c is remapped, it makes no sense to map cgc.