Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Show current function name in C programs
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Show current function name in C programs
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{Duplicate|945|1087|1296|1454}} {{TipImported |id=1267 |previous=1266 |next=1268 |created=2006 |complexity=intermediate |author=do1 |version=6.0 |rating=56/25 |category1= |category2= }} Add following in your vimrc: <pre> 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> </pre> 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: <pre> map ,f :call ShowFuncName() <CR> </pre> Now if you have jumped into a lengthy function (via tag or search) you can see its name without any further scrolling – just press <code>f</code>. If you have very large functions, or small terminal, you may add [:winwidth('%')-3] as follow <pre> echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))[:winwidth('%')-3] </pre> ==Comments== <pre> :call cursor(line,col) </pre> ---- Try this regexp for Java: <pre> echo getline(search("\\h\\+\\s\\+\\h\\+\\s*(.*)", 'bW')) </pre> :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): <pre> let sausage = input("Hit enter...") </pre> ---- Following works for C++ as well as C: <pre> echo getline(search("^\\( \\{4}\\|\\t\\)\\?\\a\\S\\{-}\\( \\a\\S\\{-}\\)\\+\\s\\?(.*[^;]\\s\\{-}$", 'bW')) </pre> it's not foolproof unfortunately. It will fail with following cases: <pre> 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 </pre>
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Duplicate
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab