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>
Creating a set of key bindings that is familiar, eases the learning curve of any new editor, and the flexibility vim allows in this configuration makes it easy to not only leverage the power of vim, but also make it feel like a familiar old friend.
As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type> where <type> is something like perl , html , php , etc. There is another mechanism that can be used to control syntax highlighting called filetype , or ft for short.
Understanding the structure of Key binding So that means you can have the same key mapped to different commands depending on the mode. That is really flexible and powerful. Vim allows you to basically map in almost every mode such as normal, insert, visual, command, and any other existing modes.
vim which is used to determine the "type" of a file. For example, while editing example.py the command :set ft? should display filetype=python if :filetype indent plugin on has been used. The file type determines whether any plugins for scripts, indenting rules, or syntax highlighting are loaded.
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> ...
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With