18

Does anyone know if it's possible (or know of an existing vim script or plugin) that can create a "status bar" that shows the name of the current class and method (or function) I'm editing?

I'm imagining that it would plug into the syntax parser for the filetype of the current buffer, and display a breadcrumb trail to show you what you're currently editing. I don't know vimscript well enough to suggest any more than that, but if there aren't any good solutions already, I may begin to hack on one, so suggestions as to where to start are welcome, too!

4 Answers 4

13

This will work if you install both the airline and tagbar plugins. These two plugins integrate automatically and you'll get the current function displayed in the status bar. If you want to have the full object hierarchy (e.g. class + method), you'll have to configure that in your .vimrc:

let g:airline#extensions#tagbar#flags = 'f'  " show full tag hierarchy

You might have to setup the tags file for tagbar to identify the current code position, if you are not using ctags, yet. If you don't know about that yet, here's a short intro to ctags.

7

This script puts the name of the current function in the title bar and/or the status line. It uses ctags to generate a list of tags for the current file and assumes that the tag preceding the cursor location is the name of the current function. Since it uses ctags, it can be customized to work with different languages, even structured text files. It works well in practice, but has some limitations. If nothing else, it might give you a starting point for hacking.

2

I would suggest using https://github.com/wellle/context.vim. It shows current context that you are in. Here is image from project

enter image description here

2

Having recently tried this, I believe the simplest solution is:

  1. Install Universal Ctags https://ctags.io/

  2. Install Tagbar https://github.com/preservim/tagbar

  3. call tagbar#currenttag() when setting your statusline, e.g. in .vimrc:

    :set statusline=%<%f\ %h%m%r%=%{tagbar#currenttag('%s\ ','','f')}%-.(%l,%c%V%)\ %P

Airline completely changes your statusbar and it's non-trivial to get

Note that:

  • Spaces must be slash-escaped in the strings you pass to currenttag()...and it's even different between running the command inside vim and putting it in your .vimrc?? Anyway, spaces can be weird, and they're probably something you want when outputting the function name.

  • It took me some digging but the default statusline is

    :set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .