5

I use Ctags and vim to browse by C source code. Quite often than not I am in middle of a long function and want to quickly check the name of the function that the browsing. I use the "{" to jump to the start of the function that I am in and '' to jump back to location in the function that I came from.

Is there any easier way of doing this? Maybe there is a plugin that shows the function that we are currently in.

2 Answers 2

6

The simplest solution is to find the first unindented line that starts with a letter (by the way, this is exactly what diff -p does).

I'm sure I borrowed this trick somewhere, but I can't remember the origin. Anyway, here it is:

" '\p' to show what function we are in (like 'diff -p')
nmap <leader>p <cmd>echo getline(search('^[[:alpha:]$_]', 'bcnW'))<CR>

Of course, this is neither perfect nor accurate yet often it works right.

1
  • 1
    I wrote a little plugin a few years ago which adds mappings for [[ and ]] for a bunch of filetypes; I added this as a fallback if a filetype doesn't have a pattern defined, thanks! Jul 6, 2022 at 19:52
3

If you don't mind a plugin, context.vim pins current function definition on the first line of the window. (Thanks @Vivian De Smedt, I couldn't find the link myself).

Another suggestion would be something like tagbar, which displays the code structure in a panel.

If you use vim-airline, and tagbar together, you can show the current function in the statusline with:

let g:airline#extensions#tagbar#enabled = 1
0

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.