Can you have file type-specific key bindings in Vim?
Asked Answered
T

6

83

In my .vimrc file, I have a key binding for commenting out that inserts double slashes (//) at the start of a line:

" the mappings below are for commenting blocks of text
:map <C-G> :s/^/\/\//<Esc><Esc>
:map <C-T> :s/\/\/// <Esc><Esc>

However, when I’m editing Python scripts, I want to change that to a # sign for comments

I have a Python.vim file in my .vim/ftdetect folder that also has settings for tab widths, etc. What is the code to override the keybindings if possible, so that I have Python use:

" the mappings below are for commenting blocks of text
:map <C-G> :s/^/#/<Esc><Esc>
:map <C-T> :s/#/ <Esc><Esc>
Tory answered 26/5, 2011 at 3:22 Comment(0)
P
67

The ftdetect folder is for scripts of filetype detection. Filetype plugins must be inside the ftplugin folder. The filetype must be included in the file name in one of the following three forms:

  • .../ftplugin/<filetype>.vim
  • .../ftplugin/<filetype>_foo.vim
  • .../ftplugin/<filetype>/foo.vim

For instance, you can map comments for the cpp filetype putting the following inside the .../ftplugin/cpp_mine.vim:

:map <buffer> <C-G> :s/^/\/\//<Esc><Esc>
:map <buffer> <C-T> :s/\/\/// <Esc><Esc>
Pinelli answered 26/5, 2011 at 3:45 Comment(2)
You forgot the <buffer> part.Woof
Your answer solve two of my problems, mappings per filetype and how to comment current line with a mapping. Great!Olli
P
88

You can use :map <buffer> ... to make a local mapping just for the active buffer. This requires that your Vim was compiled with +localmap.

So you can do something like

autocmd FileType python map <buffer> <C-G> ...
Pixilated answered 26/5, 2011 at 3:32 Comment(0)
P
67

The ftdetect folder is for scripts of filetype detection. Filetype plugins must be inside the ftplugin folder. The filetype must be included in the file name in one of the following three forms:

  • .../ftplugin/<filetype>.vim
  • .../ftplugin/<filetype>_foo.vim
  • .../ftplugin/<filetype>/foo.vim

For instance, you can map comments for the cpp filetype putting the following inside the .../ftplugin/cpp_mine.vim:

:map <buffer> <C-G> :s/^/\/\//<Esc><Esc>
:map <buffer> <C-T> :s/\/\/// <Esc><Esc>
Pinelli answered 26/5, 2011 at 3:45 Comment(2)
You forgot the <buffer> part.Woof
Your answer solve two of my problems, mappings per filetype and how to comment current line with a mapping. Great!Olli
C
11

I prefer to have my configuration in a single file so I use the autocmd approach.

augroup pscbindings
  autocmd! pscbindings
  autocmd Filetype purescript nmap <buffer> <silent> K :Ptype<CR>
  autocmd Filetype purescript nmap <buffer> <silent> <leader>pr :Prebuild!<CR>
augroup end

Vim doesn't clear set autocmds when you source your vimrc, so starting vim, changing something in your vimrc and running :so ~/.vimrc would define autocmds twice. That's why the bindings are grouped and cleared with autocmd! group_name. You can read more here.

Since mappings are applied to every buffer by default, and you want to change them for buffers matching the filetype only, the <buffer> modifier is in there, limiting the mappings to the local buffer.

Cichlid answered 9/1, 2019 at 10:28 Comment(0)
P
4

Btw... if your primary problem is about commenting... you should check out 'nerdcommenter' plugin, its the fastest way to comment/uncomment your code in java/c/c++/python/dos_batch_file/etc etc.

Potash answered 24/5, 2014 at 8:41 Comment(1)
I've been very very happy with YAATPP* github.com/tpope/vim-commentary * -> Yet Another Amazing Tim Pope PluginPhraseograph
T
0

This is only a partial answer for people coming here having difficulties getting any ftplugin scripts working, but remember that your .vimrc (or a file that it sources) should contain

filetype plugin on

or

:filetype plugin on

for filetype-plugins to be executed when a file of a given type is loaded.

Tangram answered 27/6, 2018 at 23:57 Comment(1)
Just noticed some downvotes. Happy to edit or delete accordingly, but I need to know the reason first :/Tangram
K
0

I recommend the .../ftplugin/<filetype>.vim approach that freitass suggests, but in your specific case Vim Commentary will solve all of this for you.

Kersten answered 26/5, 2020 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.