I'm trying to create a mapping based on the file type. (as explained in this chapter https://learnvimscriptthehardway.stevelosh.com/chapters/12.htm)

:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>

To test this mapping, I open a new buffer (through :vsplit) containing a javascript file but the mapping does not work at all and I don't understand why.

When I hit , and c simultaneously I expect vim to add // in the left of my line.

The relevant settings:

let mapleader=,
set ft? --> filetype=javascript

Any ideas?


This is what I get when I hit :nmap

n  ,c          *@I//<Esc>
n  %             <Plug>(MatchitNormalForward)
n  (             [
n  )             ]
n  ,ts         * :PHPUnitSwitchFile<CR>
n  ,tf         * :PHPUnitRunCurrentFile<CR>
n  ,ta         * :PHPUnitRunAll<CR>
n  ,da         * :call PhpDocAll()<CR>
n  ,cog        * :call PhpCreateGetters()<CR>
n  ,sg         * :call PhpCreateSettersAndGetters()<CR>
n  ,du         * :call PhpDetectUnusedUseStatements()<CR>
n  ,np         * :call PhpCreateProperty()<CR>
n  ,ep         * :call PhpExtractClassProperty()<CR>
n  ,rm         * :call PhpRenameMethod()<CR>
n  ,eu         * :call PhpExtractUse()<CR>
n  ,rcv        * :call PhpRenameClassVariable()<CR>
n  ,rlv        * :call PhpRenameLocalVariable()<CR>
n  ,r            <Plug>(FerretAcks)
n  ,l            <Plug>(FerretLack)
n  ,sv         * :source $MYVIMRC<CR>
n  ,ev         * :vsplit $MYVIMRC<CR>G
n  ,bs           :CtrlPMRU<CR>
n  ,bm           :CtrlPMixed<CR>
n  ,bb           :CtrlPBuffer<CR>
n  ,p            :CtrlP<CR>
n  ,A          * :exec "Rg ".expand("<cword>")<CR>
n  ,a          * :Rg<Space>
n  ,f            :NERDTreeFind<CR>
n  ,t            :NERDTreeToggle<CR>
n  ,s          * :%s/\<<C-R><C-W>\>/
n  <p          & :<C-U>call <SNR>52_putline(v:count1 . ']p', 'Below')<CR><']
n  <P          & :<C-U>call <SNR>52_putline(v:count1 . '[p', 'Above')<CR><']
n  <leader-o>  * :CtrlPTag<CR>
n  =p          & :<C-U>call <SNR>52_putline(v:count1 . ']p', 'Below')<CR>=']
n  =P          & :<C-U>call <SNR>52_putline(v:count1 . '[p', 'Above')<CR>=']
n  =op         * <Nop>
n  =o            <SNR>52_legacy_option_map(nr2char(getchar()))
n  >p          & :<C-U>call <SNR>52_putline(v:count1 . ']p', 'Below')<CR>>']
n  >P          & :<C-U>call <SNR>52_putline(v:count1 . '[p', 'Above')<CR>>']
n  [C            9999[c
n  [c            <Plug>(signify-prev-hunk)
n  [xx           <Plug>unimpaired_line_xml_encode
n  [x            <Plug>unimpaired_xml_encode
n  [uu           <Plug>unimpaired_line_url_encode
n  [u            <Plug>unimpaired_url_encode
n  [yy           <Plug>unimpaired_line_string_encode
n  [y            <Plug>unimpaired_string_encode
n  [P            <Plug>unimpairedPutAbove
-- Plus --
  • 2
    This mapping is using <LocalLeader> so that would be what :let maplocalleader is set to, perhaps ` which is the default? Does \c` work here? Use :nmap to inspect the mappings that are set... Also double check that :set ft? confirms it's ft=javascript.
    – filbranden
    Commented Aug 9, 2020 at 17:38
  • Note that vsplit opens a new window, not a buffer. If that buffer was already opened earlier and stays in memory (including being hidden due to set hidden and such) you won't get another FileType event just because you did a split.
    – Matt
    Commented Aug 9, 2020 at 17:57
  • It's working now!! Actually it has always been working, but I haven't seen it because it takes like a second or two to work, a bit weird.
    – smarber
    Commented Aug 9, 2020 at 18:54
  • 2
    @smarber If it takes a while to work, maybe it's because you have a separate mapping that uses <localleader>c as a prefix? So Vim is waiting to see if you're going to type more characters to trigger the longer mapping. Take a look at the output of :nmap to see if that's the case.
    – filbranden
    Commented Aug 9, 2020 at 19:34
  • 1
    @smarber As a general advice, please edit your question to include more details, and try to avoid posting pictures of text when you could just copy & paste the text instead. By looking at your screenshot, I think I found the root of the issue you're having. I would stll appreciate it if you could edit your question to post those details there, in text format, so that the question and answer stands in a form that is useful for people that stumble upon it in the future. Cheers!
    – filbranden
    Commented Aug 10, 2020 at 21:22

1 Answer 1


The problem you're having happens because you have two mappings that conflict with each other, in that you have a second longer mapping that starts with ,c, so when you type ,c Vim doesn't know right away whether you want to invoke the commenting mapping (which is ,c) or whether you are about to type the additional keys to invoke the longer mapping.

The image you posted with the output to :nmap shows the two mappings (although not next to each other):

n   ,c            *@I//<Esc>
n   ,cog          * call PhpCreateGetters()<CR>

In that case, and assuming that 'timeout' is set (which is the default), then Vim will wait up to 'timeoutlen' (defaults to one second) before it decides to execute the shorter mapping. See :help 'timeout' for more detail on these two settings.

To solve this issue, you can either disable or reconfigure the plug-in that is creating the longer mapping with the shared prefix (looks like a PHP language plug-in.) Reconfiguring that plug-in to use a different leader or prefix or a different mapping altogether for that particular feature would solve the problem.

Or you can pick a different key sequence for your comment mapping. For example, many plug-ins implementing commenting lines and blocks, such as vim-commentary or NERDCommenter will use gcc (with no leader) to comment a single line, and other mappings starting with gc to comment other kinds of blocks. Perhaps picking the same key sequence would make sense here? (Or just adopt one of the plug-ins, if you're more interested in the commenting feature than in learning Vimscript.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.