vim mapping problem in vimrc file but can not find the mapping that I set

Viewed 75

I am using vim-markdown-toc plugin(successfully installed) and want to remapping some hotkey to specific function. I export this code autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked to my .vimrc file. But when I type :verbose imap <C-x>, it shows can not find the mapping.

Can anyone tell me what's the problem about this?

and also I also want to ask how to map one hotkey to multiple function?

1 Answers
autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked

has two obvious errors:

  1. noremapb should be noremap, without the b:

    noremap <silent> <C-x> :GenTocMarked
    
  2. There should be a <CR> at the end:

    noremap <silent> <C-x> :GenTocMarked<CR>
    

    The right-hand-side of a mapping is a macro: since you press <CR> to execute the command :GenTocMarked, it should be present in the RHS.

Then comes the diagnostic mistake: the :map command, and its non-recursive buddy :noremap create mappings for normal, visual, and operator-pending modes, but :imap prints out insert mode mappings so you can't really expect it to find a mapping created with :map.

Then comes the semantic mistake: the re in noremap is part of nore (short for non-recursive), not of remap. <C-x> is not a mapping so you are not "remapping" anything.

Then comes the scoping mistake: :noremap creates mappings for three modes, which is something you probably don't want. You should be more specific:

" normal mode mapping
nnoremap <silent> <C-x> :GenTocMarked<CR>

and, finally, the autocommand abuse mistake: there already is a built-in mechanism for sourcing filetype-specific config so there is no need to reinvent the wheel in your vimrc:

" in after/ftplugin/markdown.vim
nnoremap <buffer> <silent> <C-x> :GenTocMarked<CR>
Related