There are similar mappings for other filetypes.
- For c-style block comments there exist built-in, ft-independent mappings
[*
/]*
(and as a synonym [/
/]/
)
/*
This is the comment body.
Variation One.
*/
This type of block comment is used in many languages:
ActionScript, AutoHotkey, C, C++, C#, D, Go, Java, JavaScript, kotlin, Objective-C, PHP, PL/I, Rust, Scala, SASS, SQL, Swift, Visual Prolog, CSS
(https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)#Block_comments)
Other mappings for certain filetypes in $VIMRUNTIME/ftplugin/
exist:
sql.vim
["
, ]"
(same as vim.vim
)
eiffel.vim
[-
, ]-
hamster.vim
[#
, ]#
(overwrite built-in motions to jump to c-preprocessor #ifdef
which are not used anyway in languages which do not have a c-preprocessor)
You can reuse them for languages with identical commentstrings by copying the code to ~/.vim/after/ftplugin/<language>.vim
.
(https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)#Inline_comments)
For example, consider the language lua
which has the same commentstring as eiffel
:
:e $VIMRUNTIME/ftplugin/eiffel.vim
:45,91w ~/.vim/after/ftplugin/lua.vim
- remove unneeded code and change
g:no_eiffel_maps
to g:no_lua_maps
and you should get something like
if !exists("g:no_plugin_maps") && !exists("g:no_lua_maps")
function! s:DoMotion(pattern, count, flags) abort
normal! m'
for i in range(a:count)
call search(a:pattern, a:flags)
endfor
endfunction
let comment_block_start = '^\%(\s\+--.*\n\)\@<!\s\+--'
let comment_block_end = '^\s\+--.*\n\%(\s\+--\)\@!'
nnoremap <silent> <buffer> ]- :<C-U>call <SID>DoMotion(comment_block_start, 1, 'W')<CR>
xnoremap <silent> <buffer> ]- :<C-U>exe "normal! gv"<Bar>call <SID>DoMotion(comment_block_start, 1, 'W')<CR>
nnoremap <silent> <buffer> [- :<C-U>call <SID>DoMotion(comment_block_end, 1, 'Wb')<CR>
xnoremap <silent> <buffer> [- :<C-U>exe "normal! gv"<Bar>call <SID>DoMotion(comment_block_end, 1, 'Wb')<CR>
let b:undo_ftplugin = b:undo_ftplugin .
\ "| silent! execute 'unmap <buffer> [-' | silent! execute 'unmap <buffer> ]-'"
endif