3

I'm trying to utilize section movement via [[, ]] and friends, but I'm not seeing the behavior I'm expecting given my understanding of the documentation.

A section begins after a form-feed () in the first column and at each of a set of section macros, specified by the pairs of characters in the a set of section macros, specified by the pairs of characters in the 'sections' option. The default is "SHNHH HUnhsh", which defines a section to start at the nroff macros ".SH", ".NH", ".H", ".HU", ".nh" and ".sh".

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. Note that the first character of the command determines the search direction and the second character the type of brace found.

Granted, I don't understand nroff macros, so maybe that's the gap, but the second paragraph mentions moving to the next '{' or '}' character in the first column. However, when I type ]] or [[, I move to the bottom or top of the file, instead.

I tested in JS, CSS, HTML, and PHP files, and since vim's defaults are mostly for C, I tried again in a C file, but all with the same result.

Is there something I'm missing about this?

Tested in vim and nvim with -u NONE.

Thanks in advance!

EDIT:

Here's the C file (which I expect to work the best) that I'm testing against.

#include <stdio.h>
// My comment

void main() {
   void a, b;

   a = 11;
   b = 99;

   printf("Values before swapping - \n a = %d, b = %d \n\n", a, b);

   a = a + b;  // ( 11 + 99 = 110)
   b = a - b;  // ( 110 - 99 = 11)
   a = a - b;  // ( 110 - 11 = 99)

   // another comment
   printf("Values after swapping - \n a = %d, b = %d \n", a, b);
}

int yo() {
    printf("hi there");
}
6
  • 1
    You're not the first to be trippied up by this bit of the documentation, but the nroff macros thing is a red herring: that bit of the documentation just means that if you have e.g. a line that begins .SH in it, the section motions will move to it. If you're not editing an nroff file, then it's irrelevant to your problem.
    – Rich
    Jan 10, 2020 at 15:52
  • 2
    Does it work correctly if you start vim with vim --clean? It's juuust about possible that your problem is caused by a rogue mapping, even if you tested with vim -u NONE.
    – Rich
    Jan 10, 2020 at 15:56
  • 1
    Thanks for the nroff explanation! That makes sense; it only applies in an nroff file, although I'm sad I can't change those options to make the sections work differently. I tried with vim --clean with the same result :( Jan 10, 2020 at 16:08
  • 1
    It's only useful in an nroff file. It applies everywhere ;) Not sure about your actual problem: maybe trying paste the content of a short file where it's not working for you into your question?
    – Rich
    Jan 10, 2020 at 16:10
  • 1
    Ah, weird. Well, thanks for the clarification! I took your advice and posted the C file I'm testing against in my example. Thanks again! Jan 10, 2020 at 19:20

2 Answers 2

2

You’re misunderstanding the motions. [[ and ]] both only move to a { character in the first column (backwards and forwards, respectively), which your file does not have. To jump to a } you need to use [] and ][.

I used to find this hard to remember until someone on this site suggested that the way to remember it is that the first bracket tells you the direction of travel and then you press it again for the start of the function or press the other bracket for the end.

1

[[ and ]] for Egyptian style of curly braces

vim help (:h section) suggests following remapping if you add the opening curly brace to the end of the function header and not on a separate line:

map [[ ?{<CR>w99[{
map ]] j0[[%/{<CR>

They are not perfect, e.g. they mess up your search history.

Btw: following ftplugins redefine [[ and ]] for you with this style of coding in mind:

For c/c++ there is lh-cpp which also redefines [[ and ]]. However, it depends IMHO on too many plugins (lh-vim-lib, lh-style, lh-brackets, mu-template, lh-dev, alternate-lite).

I am still looking for a more lightweight solution. Maybe the inclusion of treesitter in neovim can help here too. Other relevant projects are:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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