This is for C coding, but would apply to editing many languages. I often use ctags or lately cscope to jump around, and find I often need to quickly check or learn what function I am in.

A great tool that seems well suited to this is [[ (which goes to next { in column 1) Then you instantly jump back to where you were with '' (two single quotes)

However, you need some customizations to make this work for my purpose. Depending on the size of the function, and where you are on the screen, vim has some rules (that I don't know much about) about when to centre your cursor depending on how far you moved with your last jump or search type of thing. In many cases, I found [[ would end up on the very first line of the screen, with the function name I so desired being just one hidden line above.

The last place I worked, the coding style was

return_type function_name ( type arg1, type arg2, type arg3, ... )

with the meat of it always on one line. So I :set scrolloff=1 and this system worked super-well. Barely noticeable in situations where I didn't need scrolloff set at all.

Now I see

return_type function_name ( type arg1,
                            type arg2,
                            type arg3,
                            ... )

So I don't really want to set scrolloff=5 which would catch most situations, because in all the situations with only 1 or 2 arguments, and all the times I'm not searching for functions, I don't really want to necessarily waste that much screen real estate where my cursor could go without forcing VIM to scroll automatically.

Any ideas? I'd prefer a straight command or setting modification, but if script or function stuff is needed, so be it.

  • Thanks for all the answers so far - they all taught me something, I will have to try playing around with them - hard to pick one. But it is Friday night so might be a few days til I have a chance to try them out :) – Starman Jan 28 '17 at 4:17

zz will center the view on the current line (with some exceptions, it won't do much if you're on the top line), without affecting ''.

You could remap [[:

:nnoremap [[ [[zz

Or simply type zz manually whenever [[ alone is insufficient.

In case centering is not enough, zb will adjust the view to put the current line at the bottom, zt will do the same for the top.

As mentioned here and in the docs, do be aware that ZZ will save the buffer and quit, so be mindful of capslock and shift. In practice, I haven't had much trouble, but YMMV.


Here are some ideas to explore as I don't see any simple and perfect solution.

  • If you're already using ctags, you could use it on-the-fly on the current file to obtain the line numbers where functions start. From there, knowing the name of the current function will be quite easy -- I've already written the related logic in lh-dev
  • Once you've jumped to the open curly bracket, you could search for the closing parenthesis associated, and then jump to its opening one. i.e. [[/)\_s*\%#{<cr>% -- \%# matches the cursor position. This still won't manage the case where the name of the function or its return type are on another line, nor whether the function is annotated with things like const, final.. as it's possible in C++ -- but [[ doesn't work well in C++ either.

How well this works will depend on the coding styles you encounter. It seems to work for the styles used where I work and in the Vim source.

nnoremap <buffer> <silent> [[ :call search('^{', 'bsW')<CR>:call search('^\K', 'bW')<CR>

It assumes that the opening braces of your C functions are always in the first column and that the next line above containing an initial keyword character (\K) in the first column is the first line of the function definition. The search patterns are easily modified to suit different styles.

Alternatively, you could use the original [[ command instead of the first search() above:

nnoremap <buffer> <silent> [[ [[:call search('^\K', 'bW')<CR>

The mapping can be put in your ~/.vim/after/ftplugin/c.vim file.

Note that by using search(), the current search pattern is not disturbed and that the s flag preserves your desired ability to go back to the previous line with ''.

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.