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.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65

2 Answers2

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.

Matt
  • 20,685
  • 1
  • 11
  • 24
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
Biggybi
  • 2,740
  • 9
  • 29