Vim Tips Wiki

Show current function name in C programs

Revision as of 06:16, July 13, 2012 by JohnBot (Talk | contribs)

1,613pages on
this wiki

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 1267 Printable Monobook Previous Next

created 2006 · complexity intermediate · author do1 · version 6.0

Add following in your vimrc:

fun! ShowFuncName()
  let lnum = line(".")
  let col = col(".")
  echohl ModeMsg
  echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
  echohl None
  call search("\\%" . lnum . "l" . "\\%" . col . "c")
map f :call ShowFuncName() <CR>

Or if you need the "f" key, just map the function to whatever you like. For example, to use ",f" just change the last line to read:

map ,f :call ShowFuncName() <CR>

Now if you have jumped into a lengthy function (via tag or search) you can see its name without any further scrolling – just press f.


:call cursor(line,col)

Try this regexp for Java:

echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW'))
The regexp doesn't seem to work. It just prints the line where the cursor is.
This regexp works fine, at least in Vim 7.0

This works for Perl too. Won't it work for any language in which each line of a function body begins with whitespace (not just for C programmers)? E.g., PHP, shell scripts, Vim scripts, etc.

If you have problems with redraw events after the message is displayed (so the function name can't be read) simply add the following line to the end of the code (before endfun):

let sausage = input("Hit enter...")

Around Wikia's network

Random Wiki