Search code examples
vim

how to keybind shortcuts with vim autocmd FileType?


I have these lines in my vimrc file:

autocmd FileType js nnoremap <F5> :! node % <CR>
autocmd FileType cpp nnoremap <F5> :! g++ % -o %.out && ./%.out <CR>
autocmd FileType py nnoremap <F5> :! python % <CR>

Problem: compiling and running cpp files with F5 works normally, but js and py files can't run, I think the shortcut is not being executed at all.


Solution

  • The name of vim's filetype for python is "python", not "py". See also this answer. Similarily, it's "javascript", not "js". However, "cpp" is fine.

    So the first and third lines should be:

    autocmd FileType javascript nnoremap :! node %
    autocmd FileType python nnoremap :! python %