Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show function name in status line

Tags:

vim

I edit a large C, C++, or Java file, say, about 15000 lines, with pretty long function definitions, say, about 400 lines. When the cursor is in middle of a function definition, it would be cool to see the function name in Vim status line.

When we set :set ls=2 in Vim, we can get the file path (relative to the current directory), line number, etc. It would be really cool if we could see the function name too. Any ideas how to get it?

Currently I use [[ to go to start of the function and Ctrl-O to get back to the line I'm editing.

like image 600
Manikandaraj Srinivasan Avatar asked Nov 29 '12 21:11

Manikandaraj Srinivasan


4 Answers

To show current function name in C programs 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.

like image 94
manav m-n Avatar answered Oct 16 '22 07:10

manav m-n


You can use ctags.vim for this, it will show the current function name in the title or status bar.

SOURCE: https://superuser.com/questions/279651/how-can-i-make-vim-show-the-current-class-and-method-im-editing

like image 38
Wouter J Avatar answered Oct 16 '22 07:10

Wouter J


Based on @manav m-n's answer

The 'n' flag in search() won't move the cursor, so a shorter version of this with the same functionality would be:

fun! ShowFuncName()
  echohl ModeMsg
  echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bWn'))
  echohl None
endfun
map f :call ShowFuncName() <CR>

Reference: run :help search()

like image 10
solidak Avatar answered Oct 16 '22 07:10

solidak


There are several plugins for status line or on-demand with a mapping, e.g.:

  • http://www.vim.org/scripts/script.php?script_id=1094
  • http://www.vim.org/scripts/script.php?script_id=2805
  • http://www.vim.org/scripts/script.php?script_id=1553
like image 1
Ingo Karkat Avatar answered Oct 16 '22 05:10

Ingo Karkat