Question

In Vim, it is possible to attach syntax highlighter to a file extension.

Is it possible to do the same with keyboard shortcuts?

I.e. the shortcuts would switch on only if a file with particular extension is being edited.

Was it helpful?

Solution

The association is not directly to file extensions, but filetypes (which can be detected based on file extensions, but also other file patterns or even its contents). :setl filetype? shows you the current buffer's.

To define a filetype-specific mapping, just append the <buffer> attribute after the :map command. (Same for custom commands, use -buffer after :command.)

You can define that for certain filetypes by prepending :autocmd Filetype {filetype} ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/{filetype}_mappings.vim. (This requires that you have :filetype plugin on.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top