Go to the end of the C++ function in Vim
Asked Answered
A

6

20

If I am in the middle of the function, I would like go to the very end of it in vim. I run into this problem as we sometimes have function of 500+ lines long (don't ask why).

I use vim, gvim.

Ammadas answered 23/3, 2009 at 19:39 Comment(0)
B
22

You can use the "]}" command. You may have to repeat it depending on how nested you are.

Bougainville answered 23/3, 2009 at 19:41 Comment(3)
the OP may also want to investigate the '%' command, which jumps to the opening or closing brace/paren/bracket that is paired with whatever is under the cursor.Stanwinn
Yes, % is useful as well but not for a quick jump out of a large function.Bougainville
You might also be interested in [[ and ]], they take you to the beginning of a function or the start of the next function.Pictograph
C
31

][ goes to the next '}' in the first column.

Read :help 29.3 to find out more about moving through a program.

Codycoe answered 23/3, 2009 at 19:42 Comment(2)
I found your solution more generic than the accepted solution, because the accepted solution didn't work for me in JavaScript.Avlona
Although thinking about it, if it goes to the next '}' in the first column, then it might find difficulty with nested functions.Avlona
B
22

You can use the "]}" command. You may have to repeat it depending on how nested you are.

Bougainville answered 23/3, 2009 at 19:41 Comment(3)
the OP may also want to investigate the '%' command, which jumps to the opening or closing brace/paren/bracket that is paired with whatever is under the cursor.Stanwinn
Yes, % is useful as well but not for a quick jump out of a large function.Bougainville
You might also be interested in [[ and ]], they take you to the beginning of a function or the start of the next function.Pictograph
M
11

][ or [] will do this for you.

Mihrab answered 23/3, 2009 at 19:40 Comment(1)
but only if the function's opening and closing curly braces are the first character on the line.Orthopedic
P
2

Make sure you're using :split and code folding to your advantage - keep a folded view at the top of the screen, unfold a function, split, edit the function, close the split window, collapse the fold.

Pogonia answered 23/3, 2009 at 19:53 Comment(1)
This one is also good, but to be honest, I didn't like folding in Vim so far, I just feel it is hard, probably I need to spend some time reading about it, which I don't have unfortunately. Do you have any quick suggestion to make me like it? :-)Avlona
C
1

SHIFT+5 toggles between { and }. You can use it to identify the top most block of code.

Czech answered 24/3, 2009 at 10:10 Comment(3)
I believe that's % which shifts :-)Deterioration
Yes... but to make it simpler I used this approach :)Czech
@MighMoS, this seems to be better for certain cases, because % only works if you are on the bracket.Avlona
C
0

I found that @Ana Betts's answer is the most helpful after gone through nearly an hour of "research" . I tried to add some buffer mappings to make perfection:

autocmd BufReadPre *.js nnoremap <buffer> [[ []%0
autocmd BufReadPre *.js nnoremap <buffer> ]] ][%0

And this to your .vimrc and type ]] or ]] to see magic happen :)

Censurable answered 5/6, 2021 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.