8

I'm trying to improve my ability to move around inside files in vim, especially using built-in mappings/commands. I edit a lot of different types, including shell, markdown, Python, etc. I'm familiar with the { and } keys, which move between paragraphs (in fact, anything with a blank lines separating them, which is useful even when writing code), as well as other keys of course.

However, I'm reading about the [[, [], ][, and ]] keys, which seem designed for moving between 'sections'. However, reading about sections in the vim documentation doesn't really help much; it seems to be written in terms of 'nroff', which I'm vaguely familiar with but isn't a file format I really know much about, or use. None of the filetypes I deal with seem to recognise those mappings/keys.

This makes me think that it would be useful to repurpose/rebind them. However, is there some non-nroff-specific meaning to a section? If so, how do I exploit/use it/turn it on?

7

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
| improve this answer | |
  • 3
    Hm i thought only [{ and friends handled curly braces. [[ also seemed more for .SH macros in nroff. Learned something today. – D. Ben Knoble Jun 4 at 20:00
  • This is great, thanks, much clearer! This is very helpful. Probably worth pointing out that [ and ] are not commands, though, as that help page says. [[ and ]] are the actual keymappings. [ and ] are prefix keys for a family of keybindings, where [ and ] move by sections. – Andrew Ferrier Jun 5 at 20:15
  • 1
    I wrote a little plugin to map [[/]] for a bunch of filetypes (and also make it behave more consistent) which may be of interest @AndrewFerrier – Martin Tournoij Jun 6 at 1:25
  • @MartinTournoij thanks, that looks great! – Andrew Ferrier Jun 7 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.