Display current function in vim status line
Asked Answered
I

6

8

I spend 20% of my life writing code in vim, almost exclusively javascript and python. The other 80% of the time I am mostly scrolling up and down my source file, trying to remember which function I'm currently editing and what class the function belongs to.

This may be technically impossible for reasons I don't understand, but are there any vim plugins which allow the vim status line to show the function the cursor is currently in for Python and/or Javascript?

It would look like this:

Vim status line with location

It's possible this already exists in, say, SublimeText. If so, I might finally stop crying and make the switch.

Some Vim plugins which don't offer this functionality:

Update

Since writing this question I've found ctags which does the same thing for C knows this kind of information. But how do I get it to display in the Vim status line?

Impecunious answered 13/11, 2015 at 18:0 Comment(8)
looks like your editor is older than you are... get a new editor with VIM key bindings.Beaty
@Beaty ha ha! Yes, I'm open to suggestions...Impecunious
(exuberant) ctags actually supports JavaScript and Python: ctags.sourceforge.net/languages.html. You may just have to enable the vim plugin explicitly for the other file types.Eloign
@FelixKling yes! So maybe this question should collapse down into: how do you get ctags to show the current function in the status line? This looks promising.Impecunious
@FelixKling Going on a tangent: it would be great if people would stop recommending the unmaintained Exubernt ctags. Use universal ctags instead. It's an actively maintained fork (and successor) of Exubernt ctags.Raimund
@Impecunious ctags plus the tagbar plugin.Raimund
The 'vim-airline' plugin has a tagbar extension that (if both are properly configured) does exactly what you want (i.e. shows current function in the status line); although tagbar by itself may be enough.Airel
@Sato: Good to know, thanks! I was just following the clues from the plugin ;)Eloign
E
7

You should try the Tagbar plugin, which is ctags based. If you check the screenshots on that link you will notice the status lines shows the name of the current method, exactly as you asked.

The plugin documentation explain how you could set your status line; the following is the configuration I'm using on my vimrc:

command! -nargs=0 TagbarToggleStatusline call TagbarToggleStatusline()
nnoremap <silent> <c-F12> :TagbarToggleStatusline<CR>
function! TagbarToggleStatusline()
   let tStatusline = '%{exists(''*tagbar#currenttag'')?
            \tagbar#currenttag(''     [%s] '',''''):''''}'
   if stridx(&statusline, tStatusline) != -1
      let &statusline = substitute(&statusline, '\V'.tStatusline, '', '')
   else
      let &statusline = substitute(&statusline, '\ze%=%-', tStatusline, '')
   endif
endfunction

As sometimes I work with very large source files, and swapping between large files causes a small delay due to ctags execution, I prefer to enable and disable this functionality by using the mapping (Ctrl+F12) or command defined above.

Excrescent answered 13/11, 2015 at 18:50 Comment(1)
Love this. I'm too dumb to work out from the Airline docs how to add a section for the current class. Then this question will be completely answered!Impecunious
S
8

Rather than having the name of the current method/class displayed in your status line, you could simply… jump to the declaration and jump back.

In Python:

?def<Esc>

or the built-in:

[[<C-o>

In JavaScript:

?fun<Esc>

It doesn't need configuration… it doesn't depend on third party tools… it's language-agnostic… it's lightweight…

func

Sector answered 13/11, 2015 at 19:41 Comment(2)
I didn't know about ?, so thanks for that! Also, even if I had known about it, this is a neat usage of it.Impecunious
It doesn't work. ?def goes to the previous function declaration, yes, but since functions are first order objects in Python, it might just land on a lambda.Thetic
E
7

You should try the Tagbar plugin, which is ctags based. If you check the screenshots on that link you will notice the status lines shows the name of the current method, exactly as you asked.

The plugin documentation explain how you could set your status line; the following is the configuration I'm using on my vimrc:

command! -nargs=0 TagbarToggleStatusline call TagbarToggleStatusline()
nnoremap <silent> <c-F12> :TagbarToggleStatusline<CR>
function! TagbarToggleStatusline()
   let tStatusline = '%{exists(''*tagbar#currenttag'')?
            \tagbar#currenttag(''     [%s] '',''''):''''}'
   if stridx(&statusline, tStatusline) != -1
      let &statusline = substitute(&statusline, '\V'.tStatusline, '', '')
   else
      let &statusline = substitute(&statusline, '\ze%=%-', tStatusline, '')
   endif
endfunction

As sometimes I work with very large source files, and swapping between large files causes a small delay due to ctags execution, I prefer to enable and disable this functionality by using the mapping (Ctrl+F12) or command defined above.

Excrescent answered 13/11, 2015 at 18:50 Comment(1)
Love this. I'm too dumb to work out from the Airline docs how to add a section for the current class. Then this question will be completely answered!Impecunious
P
7

lightline.vim and tagbar

based on https://github.com/itchyny/lightline.vim/issues/42:

vimrc

let g:lightline = {
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ], [ 'filename', ], [ 'tagbar' ] ]
      \ },
      \ 'component': {
      \         'tagbar': '%{tagbar#currenttag("%s", "", "f")}',
      \ },
      \ 'component_function': {
      \   'modified': 'LightLineModified',
      \   'readonly': 'LightLineReadonly',
      \   'filename': 'LightLineFilename',
      \   'fileformat': 'LightLineFileformat',
      \   'filetype': 'LightLineFiletype',
      \   'fileencoding': 'LightLineFileencoding',
      \   'mode': 'LightLineMode'}
      \ }
function! LightLineModified()
  return &ft =~ 'help' ? '' : &modified ? '+' : &modifiable ? '' : '-'
endfunction
function! LightLineReadonly()
  return &ft !~? 'help' && &readonly ? 'RO' : ''
endfunction
function! LightLineFilename()
  let fname = expand('%:t')
  return fname == '__Tagbar__' ? g:lightline.fname :
        \ ('' != LightLineReadonly() ? LightLineReadonly() . ' ' : '') .
        \ ('' != fname ? fname : '[No Name]') .
        \ ('' != LightLineModified() ? ' ' . LightLineModified() : '')
endfunction
function! LightLineFileformat()
  return winwidth(0) > 70 ? &fileformat : ''
endfunction
function! LightLineFiletype()
  return winwidth(0) > 70 ? (strlen(&filetype) ? &filetype : 'no ft') : ''
endfunction
function! LightLineFileencoding()
  return winwidth(0) > 70 ? (strlen(&fenc) ? &fenc : &enc) : ''
endfunction
function! LightLineMode()
  let fname = expand('%:t')
  return fname == '__Tagbar__' ? 'Tagbar' :
        \ winwidth(0) > 60 ? lightline#mode() : ''
endfunction
let g:tagbar_status_func = 'TagbarStatusFunc'
function! TagbarStatusFunc(current, sort, fname, ...) abort
    let g:lightline.fname = a:fname
  return lightline#statusline(0)
endfunction
Phosphaturia answered 5/2, 2016 at 9:1 Comment(0)
E
3

Just try [[ to jump to the function's first brace, where you can see the function's name you are editing; Then type CTRL-O to return. :)

Edgewise answered 18/5, 2021 at 3:17 Comment(2)
This is actually a perfectly legimitate suggestion. Although it's already in this answer buried in the middleImpecunious
Oh, I didn't notice that, sorry~ By the way, this is my first answer in stack overflow, thank you for voting me :-)Edgewise
I
2

Metadata about where each function resides in a particular file is gathered and stored by a command-line tool called ctags.

The tagbar plugin for Vim manages ctags calls in order to show a hierarchy of the document currently being edited.

Finally, the airline plugin comes with an extension for tagbar which allows the current tag (i.e. the name of the current function) to be displayed in the Vim status line.

This can be configured to show the whole hierarchy of the tag by adding this line to your .vimrc:

let g:airline#extensions#tagbar#flags = 'f'

which looks like this:

airline plus tagbar

Inspiration for this answer comes from this answer and a comment on this question.

Impecunious answered 18/11, 2015 at 22:56 Comment(1)
but tagbar is too slow on updating the method name.Tenor
P
0

Show context in split window/header line

There are vim/neovim plugins which show you automatically the context of your cursor w.r.t. the function/method/case you are in.

vim plugin

neovim treesitter plugin

Phosphaturia answered 3/6, 2021 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.