你能在 Vim 中拥有特定于文件类型的键绑定吗? [英] Can you have file type-specific key bindings in Vim?
问题描述
在我的 .vimrc
文件中,我有一个用于注释的键绑定,它在一行的开头插入双斜杠 (//
):
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>
但是,当我编辑 Python 脚本时,我想将其更改为 #
符号以供评论
However, when I’m editing Python scripts, I want to change that to a #
sign for comments
我的 .vim/ftdetect
文件夹中有一个 Python.vim
文件,该文件还具有选项卡宽度等设置.如果可能,覆盖键绑定的代码是什么,以便我使用 Python:
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>
推荐答案
ftdetect 文件夹用于文件类型检测的脚本.文件类型插件必须在 ftplugin 文件夹内.文件类型必须以以下三种形式之一包含在文件名中:
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/
.vim .../ftplugin/
_foo.vim .../ftplugin/
/foo.vim
例如,您可以将 cpp 文件类型的注释映射到 .../ftplugin/cpp_mine.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>
这篇关于你能在 Vim 中拥有特定于文件类型的键绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!