The initial usage of these mapping is to navigate between functions in C files as said in :h section
(note it's not the same topic as :h 'sections'
that you linked in your question)
The "]" and "[" commands stop at the '{' or '}' in the first column. This is
useful to find the start or end of a function in a C program.
So this is the definition of the section as Vim understands it: An arbitrary number of lines delimited by curvy brackets on the first column of a line.
Note that it is only if the curvy bracket characters are on the first column like in this code:
int main()
{
printf("Hello, World!");
return 0;
}
If your code doesn't follow this convention the help advises to remap the keys so that they look for curvy brackets anywhere in the line:
If your '{' or '}' are not in the first column, and you would like to use "[["
and "]]" anyway, try these mappings:
:map [[ ?{<CR>w99[{
:map ][ /}<CR>b99]}
:map ]] j0[[%/{<CR>
:map [] k$][%?}<CR>
This is better but not ideal so it is sometimes a good idea to remap them to achieve the same purpose (e.g. navigate between functions) but for different filetypes. An example is in the standard vim
ftplugin shipped with vim and neovim:
nnoremap <silent><buffer> [[ m':call search('^\s*fu\%[nction]\>', "bW")<CR>
vnoremap <silent><buffer> [[ m':<C-U>exe "normal! gv"<Bar>call search('^\s*fu\%[nction]\>', "bW")<CR>
nnoremap <silent><buffer> ]] m':call search('^\s*fu\%[nction]\>', "W")<CR>
vnoremap <silent><buffer> ]] m':<C-U>exe "normal! gv"<Bar>call search('^\s*fu\%[nction]\>', "W")<CR>
nnoremap <silent><buffer> [] m':call search('^\s*endf\%[unction]\>', "bW")<CR>
vnoremap <silent><buffer> [] m':<C-U>exe "normal! gv"<Bar>call search('^\s*endf\%[unction]\>', "bW")<CR>
nnoremap <silent><buffer> ][ m':call search('^\s*endf\%[unction]\>', "W")<CR>
vnoremap <silent><buffer> ][ m':<C-U>exe "normal! gv"<Bar>call search('^\s*endf\%[unction]\>', "W")<CR>
In any buffer with a filetype vim
the mappings will navigate through the function thanks to searches based on the keywords function
and endfunction
(and their short version).
Several other plugins do this but you can also do it by yourself for your favorite filetypes.
After a quick search in the standard runtime files I found that Vim remaps these keys for the following filetypes:
- abaqus
- cobol
- context
- eiffel
- hamster
- j
- mf
- mp
- python
- ruby
- rust
- vim
- zimbu