Fandom

Vim Tips Wiki

Show current function name in C programs

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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")
endfun
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.

CommentsEdit

: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...")

Following works for C++ as well as C:

echo getline(search("^\\( \\{4}\\|\\t\\)\\?\\a\\S\\{-}\\( \\a\\S\\{-}\\)\\+\\s\\?(.*[^;]\\s\\{-}$", 'bW'))

it's not foolproof unfortunately. It will fail with following cases:

false positives:
MyClass obj(int a,
            int b);
MyClass obj(int a, int b); // comment

false negatives:
MyFunction func(int a) // comment ending with semicolon;

and maybe more

Also on Fandom

Random Wiki